HI Friends,
I am wondering if it is possible to check the specific entry in VMX file through powercli script. Actually i wanted to search set of VMs which has "scsi0:1.writeThrough = "true" in VMX file. Can you guys please help me.
Sure, try something like this
$vmName = "VM*"
$tgtFolder = "C:\Temp\VMX\"
$tgtString = 'scsi0:1.writeThrough = "true"'
Get-VM -Name $vmName | %{
$dsName,$vmxPath = $_.ExtensionData.Config.Files.VmPathName.Split()
$dsName = $dsName.Trim('[]')
$ds = Get-Datastore -Name $dsName
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null
Copy-DatastoreItem -Item "DS:$vmxPath" -Destination $tgtFolder
Remove-PSDrive -Name DS -Confirm:$false
}
Get-ChildItem -Path $tgtFolder -Filter "*.vmx" |
Where {Get-Content -Path $_.FullName | Select-String -Pattern $tgtString} |
Select Name
It will display the name of each VMX file in which the string 'scsi0:1.writeThrough="True"' is found.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can always check the content of a VMX file by copying it to local storage on your client with the Copy-DatastoreItem cmdlet.
Once you have the file on local storage, use Get-Content to "read" the content.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
All those VMs are running, is it possible to copy vmx file even VM is running? there are more than 100 VMs that I need to check for the scsi0:1.writeThrough = "true" entry. Is it possible to check this entry in vmx file using script like we check for advance config entry of VM?
Yes, you can copy the VMX file while the VM is running.
With a script it will not matter if you do it for 1 or 100 VMs.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Issue is, because of scsi0:1.writeThrough = "true" in vmx file for 2 HDD, whenever VM migrated to ESXi 5.1 2nd HDD disappear within OS and we have to migrate it back to ESXi 4.1 to resolve the issue. after investigation we found that those VMs were deployed using the BMC orchestrator automation and 2 HDD was added at the time of VM deployment. Now if we remove and re-add the 2nd HDD above entry goes away from vmx file and then VM works fine on ESXi 5.1.
Now Issue is I need to find out the VMs those have scsi0:1.writeThrough = "true" entry in VMX file so that we can remove this entry from vmx file and upgrade the HOSTS to ESXi 5.1. Our upgrade project is stuck because of this issue.
Thanks Luc for the help... could you please show me how to use Copy-DatastoreItem to get the VMX files
Sure, try something like this
$vmName = "VM*"
$tgtFolder = "C:\Temp\VMX\"
$tgtString = 'scsi0:1.writeThrough = "true"'
Get-VM -Name $vmName | %{
$dsName,$vmxPath = $_.ExtensionData.Config.Files.VmPathName.Split()
$dsName = $dsName.Trim('[]')
$ds = Get-Datastore -Name $dsName
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null
Copy-DatastoreItem -Item "DS:$vmxPath" -Destination $tgtFolder
Remove-PSDrive -Name DS -Confirm:$false
}
Get-ChildItem -Path $tgtFolder -Filter "*.vmx" |
Where {Get-Content -Path $_.FullName | Select-String -Pattern $tgtString} |
Select Name
It will display the name of each VMX file in which the string 'scsi0:1.writeThrough="True"' is found.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks a lot Luc....it worked perfectly... Thanks a lot...
I know this is a really old post but hopefully my reply will alert someone. Also, fair warning, I'm very bad with PowerCLI/Powershell/Scripting in general.
I'm trying to do the same thing as OP did, but with a different target string and across my entire vSphere environment. I'm not sure if I'm just doing something wrong or if the versions of PowerCLI/PowerShell are different enough to where some parameters/variables/cmdlets are different from the original posting.
The specific error(s) I'm running into are during the 'Copy-DatastoreItem' and 'Get-ChildItem' pieces, and both are presenting errors that my path to my datastore doesn't exist. It seems to be defaulting to a local mapped drive on my workstation, and then looking for the datastore there.
I modified the original script to target only a single VM for testing reasons, as well as a target folder of a VSAN datastore where the VM in question currently resides.
$vmName = "VM"
$tgtFolder = "datastore"
$tgtString = 'sound.virtualDev = "hdaudio"'
In the following error message, "X:\" is the mapped drive on my workstation.
Copy-DatastoreItem : The specified destination folder 'X:\datastore' does not exists. Use the Force parameter to force
operation.
At line:10 char:3
+ Copy-DatastoreItem -Item "DS:$vmxPath" -Destination $tgtFolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-DatastoreItem], PathException
+ FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Cmdlets.Provider.Exceptions.PathException,VMware.VimAutomation.ViCore.Cmdl
ets.Commands.CopyDatastoreItem
Get-ChildItem : Cannot find path 'X:\d_tst_cl1_vsan\VM' because it does not exist.
At line:14 char:1
+ Get-ChildItem -Path $tgtFolder -Filter "*.vmx" |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (X:\datastore\VM:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Hopefully my explanation isn't too confusing. Thanks in advance for any help!
The problem seems to be with what you have in $tgtFolder.
Is that a local, full path?
In other words something like <Drive-letter>:\<FolderName>
In that location the script will offload the VMX files.
The 2nd error is probably a result of the 1st error.
Since the files was not copied, there is nothing there.
You would need to provide a bit more information on the location where you want to store the VMX files.
And also on the DisplayName of the VM you are testing with.
Easiest would be to provide the full script as you are using it (including your changes)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I realize my first mistake. I thought that $tgtFolder would be the datastore that the VM(s) resided on.
Now that I have corrected that, i'm getting two different errors that you see below.
Copy-DatastoreItem : 11/14/2019 2:32:12 PM VimDatastore Download of file
'https://phdvc01/folder%2fdpacsorat01%2fdpacsorat01.vmx?dcPath=DBD+DC&dsName=dxi0827_tst_cl2_lun017' failed. Error message: The
remote server returned an error: (404) Not Found.
At line:10 char:3
+ Copy-DatastoreItem -Item "DS:$vmxPath" -Destination $tgtFolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-DatastoreItem], VimException
+ FullyQualifiedErrorId : Client20_StorageServiceImpl_DownloadFile_DownloadFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.C
opyDatastoreItem
Copy-DatastoreItem : Object reference not set to an instance of an object.
At line:10 char:3
+ Copy-DatastoreItem -Item "DS:$vmxPath" -Destination $tgtFolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-DatastoreItem], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyDatastoreItem
Full script is below. All I did was change $tgtFolder, $tgtString, and I specified a single VM as to not run it against the entire environment (yet)
$vmName = "dpacsorat02"
$tgtFolder = "D:\TEMP\VMX\"
$tgtString = 'sound.virtualDev = "hdaudio"'
Get-VM -Name $vmName | %{
$dsName,$vmxPath = $_.ExtensionData.Config.Files.VmPathName.Split()
$dsName = $dsName.Trim('[]')
$ds = Get-Datastore -Name $dsName
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null
Copy-DatastoreItem -Item "DS:$vmxPath" -Destination $tgtFolder
Remove-PSDrive -Name DS -Confirm:$false
}
Get-ChildItem -Path $tgtFolder -Filter "*.vmx" |
Where {Get-Content -Path $_.FullName | Select-String -Pattern $tgtString} |
Select Name
Do you actually have a '+' character in your datacentername, as the URI in the 1st error message seems to say?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No, but there is a space in the name.
Ok, that is probably where the '+' is coming from.
You could have a look in the vpxd log to check if there any more clues why the Copy-DatastoreItem fails.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This particular VM is on a VSAN datastore. Not sure if that would make a difference or not.
Just tested this with a VM on a VSAN datastore, works as well for me.
Does the script work for you when you test with a VM that is not on a VSAN datastore?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Nope. Same error. vpxd.log shows the following. Looking into this now.
2019-11-15T15:50:04.283Z info vpxd[04810] [Originator@6876 sub=Default opID=req=00007f84182d4e30-af] [VpxLRO] -- ERROR session[52fa64f6-1fdf-c947-9c69-6f5f997c1070]521aafb6-a7a4-bc0f-232e-6c7ac778cc09 -- datastoreBrowser-datastore-1525140 -- vim.host.DatastoreBrowser.search: vim.fault.FileNotFound:
--> Result:
--> (vim.fault.FileNotFound) {
--> faultCause = (vmodl.MethodFault) null,
--> faultMessage = <unset>,
--> file = "[dxi0827_prd_cl1_lun035]2fdpacsdicmp01"
--> msg = "Received SOAP response fault from [<cs p:00007f840c101220, TCP:dprd01esx08.txhealth.org:443>]: searchDatastore
--> "
--> }
--> Args:
-->
--> Arg datastorePath:
--> "[dxi0827_prd_cl1_lun035]2fdpacsdicmp01"
--> Arg searchSpec:
--> (vim.host.DatastoreBrowser.SearchSpec) {
--> query = (vim.host.DatastoreBrowser.Query) [
--> (vim.host.DatastoreBrowser.FolderQuery) {
--> },
--> (vim.host.DatastoreBrowser.Query) {
--> }
--> ],
--> details = (vim.host.DatastoreBrowser.FileInfo.Details) {
--> fileType = true,
--> fileSize = false,
--> modification = false,
--> fileOwner = false
--> },
--> searchCaseInsensitive = <unset>,
--> matchPattern = (string) [
--> "dpacsdicmp01\.vmx"
--> ],
--> sortFoldersFirst = <unset>
--> }
This is strange (and I didn't expect it in the vpxd log).
--> matchPattern = (string) [
--> "dpacsdicmp01\.vmx"
--> ],
It looks as if you are doing the Get-ChildItem on the datastore.
That should be on the folder where the VMX was downloaded.
Can you check if the following downloads the VMX file?
$tgtFolder = "C:\Temp\VMX\"
Get-VM -Name $vmName | %{
$dsName,$vmxPath = $_.ExtensionData.Config.Files.VmPathName.Split()
$dsName = $dsName.Trim('[]')
$ds = Get-Datastore -Name $dsName
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null
Copy-DatastoreItem -Item "DS:$vmxPath" -Destination $tgtFolder
Remove-PSDrive -Name DS -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It's something to do with the "%2f" getting added in there. Powershell error:
Copy-DatastoreItem : 11/15/2019 10:00:25 AM VimDatastore Download of file
'https://phdvc01/folder%2fdpacsdicmp01%2fdpacsdicmp01.vmx?dcPath=DBD+DC&dsName=dxi0827_prd_cl1_lun035' failed. Error message: The remote server
returned an error: (404) Not Found.
At D:\Scripts\SearchVMX.ps1:10 char:3
+ Copy-DatastoreItem -Item "DS:$vmxPath" -Destination $tgtFolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-DatastoreItem], VimException
+ FullyQualifiedErrorId : Client20_StorageServiceImpl_DownloadFile_DownloadFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyDatastoreItem
vpxd.log message:
2019-11-15T15:50:04.283Z info vpxd[04810] [Originator@6876 sub=Default opID=req=00007f84182d4e30-af] [VpxLRO] -- ERROR session[52fa64f6-1fdf-c947-9c69-6f5f997c1070]521aafb6-a7a4-bc0f-232e-6c7ac778cc09 -- datastoreBrowser-datastore-1525140 -- vim.host.DatastoreBrowser.search: vim.fault.FileNotFound:
--> Result:
--> (vim.fault.FileNotFound) {
--> faultCause = (vmodl.MethodFault) null,
--> faultMessage = <unset>,
--> file = "[dxi0827_prd_cl1_lun035]2fdpacsdicmp01"
--> msg = "Received SOAP response fault from [<cs p:00007f840c101220, TCP:dprd01esx08.txhealth.org:443>]: searchDatastore
--> "
--> }
--> Args:
I don't think so.
The 0x2F is just a way to encode the '/' character in the URI
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference