Automation

 View Only
  • 1.  Invoke-VMScript Could not find VirtualMachine with name 'APP01'

    Posted Jul 29, 2019 01:02 PM

    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


  • 2.  RE: Invoke-VMScript Could not find VirtualMachine with name 'APP01'

    Posted Jul 29, 2019 01:05 PM

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



  • 3.  RE: Invoke-VMScript Could not find VirtualMachine with name 'APP01'

    Posted Jul 29, 2019 03:29 PM

    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



  • 4.  RE: Invoke-VMScript Could not find VirtualMachine with name 'APP01'

    Posted Jul 29, 2019 03:50 PM

    I suspect there might be something wrong in your .TXT file.

    Does this produce any errors?


    Get-VM -Name (Get-Content .\VM.txt)



  • 5.  RE: Invoke-VMScript Could not find VirtualMachine with name 'APP01'

    Posted Jul 29, 2019 04:02 PM

    LucD,

    I dont see any errors in the output

    also attached the script for reference



  • 6.  RE: Invoke-VMScript Could not find VirtualMachine with name 'APP01'

    Posted Jul 29, 2019 04:23 PM

    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



  • 7.  RE: Invoke-VMScript Could not find VirtualMachine with name 'APP01'

    Posted Jul 29, 2019 05:03 PM

    LucD,

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



  • 8.  RE: Invoke-VMScript Could not find VirtualMachine with name 'APP01'
    Best Answer

    Posted Jul 29, 2019 05:10 PM

    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.



  • 9.  RE: Invoke-VMScript Could not find VirtualMachine with name 'APP01'

    Posted Jul 29, 2019 05:19 PM

    Thank you very much :smileyhappy: