Hi,
I am unable to capture the VM information which are not working in the output file. I dont see the VM Name which are invalid getting captured
$code = @'
$versionInfo = (Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo
$versionInfo | Select ProductVersion,FileVersion,FileName | ConvertTo-Csv
'@
$report = @()
$reportNotFound = @()
Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |
ForEach-Object -Process {
$sInvoke = @{
VM = $_.Name
GuestCredential = $Creds
ScriptTYpe = 'powershell'
ScriptText = $code
}
try{
Invoke-VMScript @sInvoke
"Successfully Gathered Chrome Info for $($row.Name)"
$report += $($row.Name)
}
catch{
Throw "Failed Gathered Chrome Info for $($row.Name)"
$reportNotFound += $($row.Name)
}
$result
$result = Invoke-VMScript @sInvoke
$report += $result.ScriptOutput | ConvertFrom-Csv |
Add-Member -MemberType NoteProperty -Name 'VM' -Value $row.Name -PassThru |
Select VM,ProductVersion,FileVersion,FileName
}
$report | ft -auto
$report | Export-Csv -Path $reportlocation1 -UseCulture -NoTypeInformation
Import-CSV $reportlocation1 | Export-Excel -Path 'D:\reports\report2.xlsx' -AutoFilter -AutoSize -BoldTopRow -FreezeTopRow -WorksheetName 'Chrome_Info'
$reportNotFound | Export-Excel -Path 'D:\reports\report2.xlsx' -AutoFilter -AutoSize -BoldTopRow -FreezeTopRow -WorksheetName 'No_Chrome_Info'
Rename-Item -Path 'D:\reports\report2.xlsx' -NewName $reportlocation
Error
Invoke-VMScript : 01/18/2021 10:25:31 AM Invoke-VMScript Timeout error while waiting for VMware Tools to start in the guest.
At D:\myreports\Chrome_List_Install_Uninstall\get_chrome_versions_2.0.ps1:52 char:19
+ $result = Invoke-VMScript @sInvoke
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationTimeout: (:) [Invoke-VMScript], VimException
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_WaitProcessInGuest_OperationTimeout,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript
ConvertFrom-Csv : Cannot validate argument on parameter 'InputObject'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At D:\myreports\Chrome_List_Install_Uninstall\get_chrome_versions_2.0.ps1:53 char:43
+ $report += $result.ScriptOutput | ConvertFrom-Csv |
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertFrom-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ConvertFromCsvCommand
That timeout causes an exception.
If you used the ErrorAction that would make the code to goto the Catch block.
And in there you add the VM's name to the $notFoundReport variable.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That is because the Catch block is never reached.
The error on Invoke-VMScript ( a timeout) is a non-terminating exception.
So the code just continues.
You can make the exception terminating, and thus terminating by using the ErrorAction parameter.
$sInvoke = @{
VM = $_.Name
GuestCredential = $Creds
ScriptTYpe = 'powershell'
ScriptText = $code
ErrorAction = 'stop'
}
try {
Invoke-VMScript @sInvoke
"Successfully Gathered Chrome Info for $($row.Name)"
$report += $($row.Name)
} catch {
Throw "Failed Gathered Chrome Info for $($row.Name)"
$reportNotFound += $($row.Name)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
After making the change, I am getting this error
Failed Gathered Chrome Info for
At D:\myreports\Chrome_List_Install_Uninstall\get_chrome_versions_2.0.ps1:48 char:4
+ Throw "Failed Gathered Chrome Info for $($row.Name)"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Failed Gathered Chrome Info for :String) [], RuntimeException
+ FullyQualifiedErrorId : Failed Gathered Chrome Info for
That is not an error, that is what you asked the script to do with the Throw command.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I am sorry, If my question was confusing.
I am able to capture the output of correct VM information in the output excel file using the below script.
$code = @'
$versionInfo = (Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo
$versionInfo | Select ProductVersion,FileVersion,FileName | ConvertTo-Csv
'@
$report1 = @()
Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |
ForEach-Object -Process {
$sInvoke = @{
VM = $_.Name
GuestCredential = $Creds
ScriptTYpe = 'powershell'
ScriptText = $code
}
$result = Invoke-VMScript @sInvoke
$report1 += $result.ScriptOutput | ConvertFrom-Csv |
Add-Member -MemberType NoteProperty -Name 'VM' -Value $row.Name -PassThru |
Select VM,ProductVersion,FileVersion,FileName
}
$report1 | ft -auto
$report1 | Export-Csv -Path $reportlocation1 -UseCulture -NoTypeInformation
I am unable to get the output from few of the VMs and I am unable to know which VM failed. I would like to capture the VM Name in the output file where the script was unable to get the requested information.
Invoke-VMScript : 01/18/2021 10:20:49 AM Invoke-VMScript Timeout error while waiting for VMware Tools to start in the guest.
At D:\myreports\Chrome_List_Install_Uninstall\get_chrome_versions_1.0.ps1:41 char:19
+ $result = Invoke-VMScript @sInvoke
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationTimeout: (:) [Invoke-VMScript], VimException
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_WaitProcessInGuest_OperationTimeout,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript
ConvertFrom-Csv : Cannot validate argument on parameter 'InputObject'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At D:\myreports\Chrome_List_Install_Uninstall\get_chrome_versions_1.0.ps1:42 char:44
+ $report1 += $result.ScriptOutput | ConvertFrom-Csv |
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertFrom-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ConvertFromCsvCommand
I just gave you the solution for that (ErrorAction).
As proof, the Catch block is now executed, but since you placed a Throw in there, the script stops.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I removed the ErrorAction from the script.
But I am unable to capture the VMName in the output file whose VMware Tools are not running or VM dont exists.
Below is the output of two VMs where APP161 failed because of VMware Tools and but in the output it says "Successfully Gathered Chrome Info for APP161" and this name is not captured in the output file
Another VM APP900 dont exists but I am not able to capture their names in the output file
Invoke-VMScript : 01/18/2021 11:18:33 AM Invoke-VMScript Timeout error while waiting for VMware Tools to start in the guest.
At D:\myreports\Chrome_List_Install_Uninstall\get_chrome_versions_2.0.ps1:43 char:19
+ $result = Invoke-VMScript @sInvoke
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationTimeout: (:) [Invoke-VMScript], VimException
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_WaitProcessInGuest_OperationTimeout,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript
ConvertFrom-Csv : Cannot validate argument on parameter 'InputObject'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At D:\myreports\Chrome_List_Install_Uninstall\get_chrome_versions_2.0.ps1:44 char:43
+ $report += $result.ScriptOutput | ConvertFrom-Csv |
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertFrom-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ConvertFromCsvCommand
Successfully Gathered Chrome Info for APP161
Invoke-VMScript : 01/18/2021 11:18:53 AM Invoke-VMScript Could not find VirtualMachine with name 'APP900'.
At D:\myreports\Chrome_List_Install_Uninstall\get_chrome_versions_2.0.ps1:43 char:19
+ $result = Invoke-VMScript @sInvoke
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (APP900:String) [Invoke-VMScript], VimException
+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript
Failed Gathered Chrome Info for APP900
At D:\myreports\Chrome_List_Install_Uninstall\get_chrome_versions_2.0.ps1:49 char:2
+ Throw "Failed Gathered Chrome Info for $($row.Name)"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Failed Gathered...APP900:String) [], RuntimeException
+ FullyQualifiedErrorId : Failed Gathered Chrome Info for APP900
I'm not sure what your problem with ErrorAction is?
That is the only way to change a non-terminating exception into a terminating exception and thus enter the Catch block.
What is your problem?
Don't you understand exceptions or what the Throw statement is doing?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Thanks for guiding me. After removing the throw from the script, I was able to capture the VM Name which is not present in vCenter in the output file.
I know, Invoke-VMScript is dependent on VMware Tools. Only one last issue that currently I see is, there are few VMs where VMTools is not running, I would like to capture the VM name in the output file where script is unable to run the invoke-vmscript code.
But you are capturing that info in the $reportNotFound array.
And you are saving that to a file.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes, $reportNotFound is working if the VM is not found in the vCenter.
But there are few VMs that are present in the vCenter but VMware Tools are not running for those I am unable to capture VMName in the output file.
Here is the output of one of the VM where VMTools is not running but it shows,
Invoke-VMScript : 01/18/2021 11:55:40 AM Invoke-VMScript Timeout error while waiting for VMware Tools to start in the guest.
At D:\myreports\Chrome_List_Install_Uninstall\get_chrome_versions_2.0.ps1:43 char:19
+ $result = Invoke-VMScript @sInvoke
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationTimeout: (:) [Invoke-VMScript], VimException
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_WaitProcessInGuest_OperationTimeout,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript
ConvertFrom-Csv : Cannot validate argument on parameter 'InputObject'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At D:\myreports\Chrome_List_Install_Uninstall\get_chrome_versions_2.0.ps1:44 char:43
+ $report += $result.ScriptOutput | ConvertFrom-Csv |
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertFrom-Csv], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ConvertFromCsvCommand
Successfully Gathered Chrome Info for APP161
That timeout causes an exception.
If you used the ErrorAction that would make the code to goto the Catch block.
And in there you add the VM's name to the $notFoundReport variable.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you very much LucD for your support.
Finally the script is working as below.
$report = @()
$reportNotFound = @()
Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |
ForEach-Object -Process {
$sInvoke = @{
VM = $_.Name
GuestCredential = $Creds
ScriptTYpe = 'powershell'
ScriptText = $code
}
try {
$result = Invoke-VMScript @sInvoke -ErrorAction STOP
$report += $result.ScriptOutput | ConvertFrom-Csv |
Add-Member -MemberType NoteProperty -Name 'VM' -Value $row.Name -PassThru |
Select VM,ProductVersion,FileVersion,FileName
"Successfully Gathered Chrome Info for $($row.Name)"
} catch {
"Failed Gathered Chrome Info for $($row.Name)"
$reportNotFound += $($row.Name)
}
}
$report | ft -auto