VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Invoke-VMScript Could not find VirtualMachine with name 'APP01'

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
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

connect-viserver 10.10.10.19

$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

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Did you check if Get-VM -Name APP01 returns a VM?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I dont see any errors in the output

pastedImage_0.png

also attached the script for reference

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

connect-viserver 10.10.10.19

$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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

Perfect, that worked, one last thing, how can get the uptime in days ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

connect-viserver 10.10.10.19

$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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much Smiley Happy

0 Kudos