我正在开发一个PowerCLI(VMware)脚本,该脚本将从每个VM的数据存储区复制VMX。其中一个最初的问题是使用VM名称中的空格。在解决了这个问题后,还有另一个障碍,我还在努力。我正在做一个Test-Path来检查以确保在尝试复制文件之前可以访问VMX文件所在的数据存储区和文件夹。但是,Test-Path失败并显示以下消息:
设置位置:找不到驱动器。一个名为' vmstore'的驱动器不存在。
最终的问题是' (单引号)就在vmstore被发送到Test-Path cmdlet之前。但是包含完整路径的变量的输出不包含该附加引用。此外,当使用直接输入的路径并使用单引号执行测试路径时,它可以正常工作。
这里是获取VMX文件位置和测试路径的完整脚本:
$gstrdatastorepath = "vmstore:\Cluster_Name\"
$lobjPrimaryDisk = Get-HardDisk -VM $VM | where {$_.name -EQ "Hard disk 1"} #looks for the first hard disk listed in VM definition. This is typically the primary source for the VMX file
$lstrDiskText = $lobjPrimaryDisk.filename #convert the filename to a string for manipulation.
#The string is typically as follows (without the quotes): "[datastore_name] folder_name/filename.vmdk
#Only the datastore_name and the folder_name are needed. The following now splits those from the vmdk file
$lstrDiskSplitInfo = $lstrDiskText.split("\/") #splits the text into two sections. First section is the primary information needed, the second is not used
#Now the datastore_name and the folder_name need to be split
$lstrDiskFolderSplitInfo = $lstrDiskSplitInfo[0] -split " ", 2 #splits the text into the datastore and folder name on the datastore. option 2 used in the event spaces are in VM name.
#Put the Disk and Folder information into separate variables
$lstrdatastorename = $lstrDiskFolderSplitInfo[0] -replace '[[\]]','' #removes the square brackets from the datastore name
$lstrfoldername = $lstrDiskFolderSplitInfo[1] #gets the folder name from the previous split
$completeDataStorePath = $gstrdatastorepath + $lstrdatastorename + "\" + $lstrfoldername #combines the information to return the full path of the VMX file
$completeVMXPathQuoted = "`'$completeDataStorePath`'" #put the path in a single quote for VM's with spaces in the name
$ completeVMXPathQuoted的值是' vmstore:\ Cluster_Name \ Data_Store \ VM_Name'其中VM_Name可能包含也可能不包含空格。
使用$ completeVMXPathQuoted变量执行Test-Path时,它会失败并显示上述消息。
但是在做Test-Path -Path' vmstore:\ Cluster_Name \ Data_Store \ VM_Name'它工作正常。
我错过了什么?第二个单引号来自何处?
提前感谢你的帮助。