The problem is here:
$MachineDirectory = $SubDirectoryInterim.split(' ')[1]
But as I don’t have any data to actually look at (that is, the format of
$SubDirectoryInterim), I can’t tell you how to fix it – except that you’ll need
to use something smarter than split().
From: [email protected] [mailto:[email protected]] On
Behalf Of Kurt Buff
Sent: Wednesday, January 10, 2018 1:45 PM
To: [email protected]
Subject: [powershell] PowerShell, and spaces in directory names on a PS Drive
for VMware
All,
Trying to copy vmware.log for all the VMs in a cluster. This script works, as
long as the directory name for the VMDK file doesn't have any spaces:
$VMList = get-vm -location LabCluster | sort name
ForEach ($VM in $VMList)
{
$Target = New-Item -ItemType Directory -Force -Path c:\temp\vm-logs\$VM
$Store = Get-VM $VM | Get-DataStore
$Disk = Get-VM $VM | Get-HardDisk
$DirectoryRoot = $Disk.FileName.split('[]')[1]
$SubDirectoryInterim = $Disk.FileName.split('/')[0]
$MachineDirectory = $SubDirectoryInterim.split(' ')[1]
New-PSDrive -Location $Store -Name ds -PSProvider VimDatastore -Root "\"
Set-Location ds:\
Set-Location $MachineDirectory
Copy-DatastoreItem –Item vmware.log -Destination $Target
Set-Location C:
Remove-PSDrive -Name ds -Confirm:$false
}
If the directory for the VMDKs has spaces in it (e.g., where the directory name
is "New VM"), I get the following error:
Set-Location : Cannot find path 'ds:\New' because it does not exist.
At C:\BatchFiles\test-VMware.ps1:19 char:4
+ cd $MachineDirectory
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (ds:\New:String) [Set-Location],
ItemNotFoundException
+ FullyQualifiedErrorId :
PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
Stepping through the lines of code above, it all works fine - but not in a
script (and I'm not using ISE to execute).
Among other approaches, I've tried the following, and get a different error
(and yes, I do see in the error output that there's only an initial double
quote for '"ds' - but when I execute each of the lines manually there is a
matching end double quote):
$VMList = get-vm -location LabCluster | sort name
ForEach ($VM in $VMList)
{
$Target = New-Item -ItemType Directory -Force -Path c:\temp\vm-logs\$VM
$Store = Get-VM $VM | Get-DataStore
$Disk = Get-VM $VM | Get-HardDisk
$SubDirectoryInterim = $Disk.FileName.split('/')[0]
$Directory = $SubDirectoryInterim.split(' ')[1]
New-PSDrive -Location $Store -Name ds -PSProvider VimDatastore -Root "\"
Set-Location ds:\
$ChangeD = "$([char]34)ds:\$Directory$([char]34)"
# Neither of these work, even though the path (ds:\<pathname>) is surrounded by
double quotes
# Set-Location -LiteralPath $ChangeD
Set-Location $ChangeD
Copy-DatastoreItem –Item vmware.log -Destination $Target
Set-Location C:
Remove-PSDrive -Name ds -Confirm:$false
}
Set-Location : Cannot find drive. A drive with the name '"ds' does not exist.
At C:\BatchFiles\get-vmwarelogs.ps1:20 char:4
+ Set-Location -LiteralPath $ChangeD
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ("ds:String) [Set-Location],
DriveNotFoundException
+ FullyQualifiedErrorId :
DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand