PowerCLI

 View Only
  • 1.  Can powercli report the status of a windows service?

    Posted May 11, 2018 12:39 AM

    We found out that we need to reinstall some software, if a particular service is not running. Can powercli be used to report on services?
    (Windows server VM's)



  • 2.  RE: Can powercli report the status of a windows service?

    Posted May 11, 2018 06:27 AM

    If you have the VMware Tools installed on those VM, and you have credentials for the guest OS on the VM, you can use the Invoke-VMScript cmdlet.

    Something like this

    $script = @"

    Get-Service -Name $serviceName

    "@

    $VMName = 'srv1'

    $serviceName = 'WinRM'

    Invoke-VMScript -VM $vmName -ScriptText $script -ScriptType Powershell



  • 3.  RE: Can powercli report the status of a windows service?

    Posted May 11, 2018 01:30 PM

    Thank you for the quick answer.
    I got an error initially
    <code>

      Get-Service : Missing an argument for parameter 'Name'. Specify a parameter of

    |  type 'System.String[]' and try again.

    |  At line:2 char:13

    |  + Get-Service -Name

    |  +             ~~~~~

    |      + CategoryInfo          : InvalidArgument: (:) [Get-Service], ParameterBin

    |     dingException

    |      + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.Ge

    |     tServiceCommand

    </code>


    So  I commented out Servicename = 'winrm' and made the script block specific  for winrm and it worked for me.

    <code>

    $script = @"

    Get-Service -Name 'WinRm'

    "@

    $VMName = 'srv1'

    #$serviceName = 'WinRM'

    Invoke-VMScript -VM $vmName -ScriptText $script -ScriptType Powershell

    </code>

    What is the best approach to run this against all of the Windows server vm's?

    I'd like the output as one thing, a csv, or in notepad, without all the lines that are around it.



  • 4.  RE: Can powercli report the status of a windows service?
    Best Answer

    Posted May 11, 2018 02:06 PM

    You could do something like this

    $script = @"

    Get-Service -Name 'WinRm' | Select -ExpandProperty Status

    "@


    $report = Get-VM | where{$_.Guest.OSFullName -match "Window"} | %{

       Invoke-VMScript -VM $_ -ScriptText $script -ScriptType Powershell |

      select @{N='VM';E={$_.VM.Name}},@{N='Status';E={$_.ScriptOutput.Trim()}}

    }


    $report | Export-Csv .\report.csv -NoTypeInformation -UseCulture



  • 5.  RE: Can powercli report the status of a windows service?

    Posted May 11, 2018 07:23 PM

    This mostly worked, the end output was just what I wanted.
    2 things though. Our network security alarmed on this activity with this showing up in their console:

    powershell -NonInteractive -EncodedCommand cABvAHcAZQByAHMAaABlAGwAbAAuAGUAeABlACAALQBPAHUAdABwAHUAdABGAG8AcgBtAGEAdAAgAHQAZQB4AHQAIAAtAE4AbwBuAEkAbgB0AGUAcgBhAGMAdABpAHYAZQAgAC0AQwBvAG0AbQBhAG4AZAAgACcAJgAgAHsARwBlAHQALQBTAGUAcgB2AGkAYwBlACAALQBOAGEAbQBlACAAJwAnAFgAYQBnAHQAJwAnACAAfAAgAFMAZQBsAGUAYwB0ACAALQBFAH=

    which decoded was something like this:powershell.exe -OutputFormat text -NonInteractive -Command '& {Get-Service -Name ''WinRm'' | Select -ExpandProperty Status}' .....

    Can this work be done without the encoding part?

    If I wanted to run it against a known set of server names, would this be it?

    $script = @"

    Get-Service -Name 'WinRm' | Select -ExpandProperty Status

    "@

    $report = read-host = .\server.txt| %{

       Invoke-VMScript -VM $_ -ScriptText $script -ScriptType Powershell |

      select @{N='VM';E={$_.VM.Name}},@{N='Status';E={$_.ScriptOutput.Trim()}}

    }

    $report | Export-Csv .\report.csv -NoTypeInformation -UseCulture



  • 6.  RE: Can powercli report the status of a windows service?
    Best Answer

    Posted May 11, 2018 07:41 PM

    I'm afraid not, the Invoke-VMScript internals do this encoding.

    If that .txt file contains 1 VM name per line, you could do

    $script = @"

    Get-Service -Name 'WinRm' | Select -ExpandProperty Status

    "@


    $report = Get-VM -Name (Get-Content -Path .\server.txt) | %{

       Invoke-VMScript -VM $_ -ScriptText $script -ScriptType Powershell |

      select @{N='VM';E={$_.VM.Name}},@{N='Status';E={$_.ScriptOutput.Trim()}}

    }


    $report | Export-Csv .\report.csv -NoTypeInformation -UseCulture



  • 7.  RE: Can powercli report the status of a windows service?

    Posted Jan 07, 2021 04:44 PM

    Sorry LucD, I was looking aback at old questions and saw I did not choose a correct answer.