Hi
I am unable to get output from the below script as I getting ProductVersion output as System.String[]
Name ProductVersion FileVersion FileName
---- -------------- ----------- --------
APP162 System.String[] 87.0.4280.8... C:\Program Files\Google\Chrome\Application\chrome.ex...
Please help
$code = @'
"### Output ###"
(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo | Select -expandproperty ProductVersion
"### Output ###"
(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo | Select -expandproperty FileVersion
"### Output ###"
(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo | Select -expandproperty FileName
'@
$report1 = @()
Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |
ForEach-Object -Process {
$sInvoke = @{
VM = $_.Name
GuestCredential = $Creds
ScriptTYpe = 'powershell'
ScriptText = $code
}
$result = Invoke-VMScript @sInvoke
$dummy, $out1, $out2, $out3 = $result.ScriptOutput -split '### Output ###'
$out1 = $out1.TrimStart("`n`r") -split "`n`r"
$out2 = $out2.TrimStart("`n`r")
$out3 = $out3.TrimStart("`n`r")
$report1 += $row | Add-Member -MemberType NoteProperty -Name 'ProductVersion' -Value ([System.Convert]::ToString($out1)) -PassThru | Add-Member -MemberType NoteProperty -Name 'FileVersion' -Value ($out2) -PassThru |
Add-Member -MemberType NoteProperty -Name 'FileName' -Value ($out3) -PassThru
}
$report1 | ft -auto
$report1 | Export-Csv -Path $reportlocation1 -UseCulture -NoTypeInformation
Import-CSV $reportlocation1 | Export-Excel -Path 'D:\reports\report.xlsx' -AutoFilter -AutoSize -BoldTopRow -FreezeTopRow -WorksheetName 'Chrome_Info'
The safest, and shortest way, to impose an order on the properties, is to use a Select-Object cmdlet.
$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 = $cred
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
In this specific case you don't need the $dummy variable on the split.
But the code can be simplified, something like this for example
$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 = $cred
ScriptTYpe = 'powershell'
ScriptText = $code
}
$result = Invoke-VMScript @sInvoke
$report1 += $result.ScriptOutput | ConvertFrom-Csv
}
$report1
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
In the output, I am missing the server name, which I mentioned the input file.I would like to get the server name in the output file or appended the same input file.
Also how to get the VM Name printed on the screen, where there is a error as I am getting the below error for few VMs
Invoke-VMScript : 01/15/2021 12:27:57 PM Invoke-VMScript Timeout error while waiting for VMware Tools to start in the guest.
At D:\myreports\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\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
Then you just do an Add-Member
$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 = $cred
ScriptTYpe = 'powershell'
ScriptText = $code
}
$result = Invoke-VMScript @sInvoke
$report1 += $result.ScriptOutput | ConvertFrom-Csv |
Add-Member -MemberType NoteProperty -Name 'VM' -Value $row.Name -PassThru
}
$report1
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I am getting the hostname at the last, I tried adding the hostname as below but getting the error
$result = Invoke-VMScript @sInvoke
$report1 += Add-Member -MemberType NoteProperty -Name 'Name' -Value $row.Name -PassThru
$report1 += $result.ScriptOutput | ConvertFrom-Csv
output
cmdlet Add-Member at command pipeline position 1
Supply values for the following parameters:
InputObject:
Also there are few VMs which are throwing the error, Is there a way to get the name of the VM which has error on the screen and VM info should be captured as blank in the output ?
Invoke-VMScript : 01/15/2021 12:37:26 PM Invoke-VMScript Timeout error while waiting for VMware Tools to start in the guest.
At D:\myreports\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\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
The safest, and shortest way, to impose an order on the properties, is to use a Select-Object cmdlet.
$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 = $cred
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That worked 🙂 Thank you very much.