Hi,
I am getting the below error when using invoke-vmscript, please help
Script :
connect-viserver 10.10.10.19
$machines = Get-Content .\VM.txt
$script = @'
$report = @()
$object = @()
foreach($machine in $machines)
{
$machine
$object = gwmi win32_operatingsystem -ComputerName $machine | select csname, @{N='LastBootUpTime';E={$_.ConverttoDateTime($_.lastbootuptime)}}
$report += $object
}
$report | Export-csv -NoTypeInformation -Path .\Uptime.csv
'@
Invoke-VMScript -VM $machines -ScriptText $Script -ScriptType Powershell -GuestUser localhost\admin -GuestPassword "password123"
Error:
Invoke-VMScript : 07/29/2019 7:58:23 AM Invoke-VMScript | Could not find VirtualMachine with name 'APP01'. |
At D:\myreports\Uptime\uptime.ps1:15 char:1
+ Invoke-VMScript -VM $machines -ScriptText $Script -GuestUser localhos ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo | : ObjectNotFound: (APP01:String) [Invoke-VMScript], VimException | |
+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript |
Try like this
$script = @'
Get-WmiObject win32_operatingsystem | %{
$_.ConverttoDateTime($_.lastbootuptime)
}
'@
$machines = Get-Content .\CSG_VM1.txt
Invoke-VMScript -VM $machines -ScriptText $Script -GuestUser "admin" -GuestPassword "password" |
Select @{N='VM';E={$_.VM.Name}},
@{N='LastBootDaysAgo';E={[math]::Round((New-TimeSpan -Start ([DateTime]$_.ScriptOutput) -End (Get-Date)).TotalDays,0)}} |
Export-csv -NoTypeInformation -Path .\CSG_Uptime.csv
disconnect-viserver -Server * -confirm:$false
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Did you check if Get-VM -Name APP01 returns a VM?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
yes, App01 exists and also present in the file.
Now, after I exited the session and opened new session, I am getting the below error in the output file
"VM","ExitCode","ScriptOutput","Uid","Length"
"APP01","0","Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum
ent is null or empty. Supply an argument that is not null or empty and then try
the command again.
At line:6 char:51
+ $object = gwmi win32_operatingsystem -ComputerName <<<< $machine | select cs
name, @{N='LastBootUpTime';E={$_.ConverttoDateTime($_.lastbootuptime)}}
+ CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindi
ngValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
Shell.Commands.GetWmiObjectCommand
I suspect there might be something wrong in your .TXT file.
Does this produce any errors?
Get-VM -Name (Get-Content .\VM.txt)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You are using inside the code you sent to the guest OS of the VM, a variable that you defined in the calling program ($machines).
That will not work I'm afraid.
Give it a try like this
$script = @'
Get-WmiObject win32_operatingsystem | %{
$_.ConverttoDateTime($_.lastbootuptime)
}
'@
$machines = Get-Content .\CSG_VM1.txt
Invoke-VMScript -VM $machines -ScriptText $Script -GuestUser "admin" -GuestPassword "password" |
Select @{N = 'VM'; E = { $_.VM.Name } }, @{N = 'LastBootUpTime'; E = { [DateTime]$_.ScriptOutput } } |
Export-csv -NoTypeInformation -Path .\CSG_Uptime.csv
disconnect-viserver -Server * -confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Perfect, that worked, one last thing, how can get the uptime in days ?
Try like this
$script = @'
Get-WmiObject win32_operatingsystem | %{
$_.ConverttoDateTime($_.lastbootuptime)
}
'@
$machines = Get-Content .\CSG_VM1.txt
Invoke-VMScript -VM $machines -ScriptText $Script -GuestUser "admin" -GuestPassword "password" |
Select @{N='VM';E={$_.VM.Name}},
@{N='LastBootDaysAgo';E={[math]::Round((New-TimeSpan -Start ([DateTime]$_.ScriptOutput) -End (Get-Date)).TotalDays,0)}} |
Export-csv -NoTypeInformation -Path .\CSG_Uptime.csv
disconnect-viserver -Server * -confirm:$false
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you very much