PowerCLI

 View Only
  • 1.  Changing Output Color Depending on Process

    Posted Apr 28, 2011 03:45 AM

    Hello everyone,

    I was trying to create a PowerCli script to alter the output of the Get-process cmdlet depending on how much memory the process was using.  Here's the script that I attempted:

    $a=Get-VM  | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Guest.OSFullName -like "*Windows*"} | Invoke-vmscript -scripttext

    "get-process"-guestuser administrator -guestpassword password | Write-output


    foreach ($i in $a){
        if ($i.WorkingSet -gt 10000) {
        write-host $i.ProcessName, $i.workingset -foregroundcolor "red"}
        else {write-host $i.processName, $i.workingset}
    }

    I'm using the Invoke-VMScript because for some reason or another I'm not able to Get-Process remotely to another machine and receive the "Get-Process: Couldn't connect to remote machine" error.  I'm able to execute Get-WMIObject cmdlets remotely without any issue so I wasn't sure what kind of configuration troubles were to be had. After a bit of research I wasn't able to fix it and so I settled with this.

    The script runs successfully but then ends without any output at all.  When I execute only:

    Get-VM  | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Guest.OSFullName -like "*Windows*"} | Invoke-vmscript -scripttext

    "get-process"-guestuser administrator -guestpassword password | Write-output

    it's able to display the process lists of all VMs on the Host.  I'm guessing there might be something wrong with the latter part of the script.

    Any feedback would be greatly appreciated and please feel free to  comment if you might know of any tips or tricks at all.

    Best regards



  • 2.  RE: Changing Output Color Depending on Process
    Best Answer

    Posted Apr 28, 2011 05:34 AM

    The reason this doesn't work with Invoke-VMScript is because that cmdlet returns all the output of the cmdlet, you execute inside the guest, in 1 string.

    It does not the Process objects you get when you execute Get-Process locally.

    So the test on the WorkingSet property will not work because there is no WorkingSet property.

    You will have to analyse the string with the results yourself.

    The following script shows how you can do this with a RegEx expression.

    $regex = [regex]"\s*(\d+)\s*(\d+)\s*(\d+)\s*(\d+)\s*(\d+)\s*(\d+)\s*([\d\.]+)\s*(\d+)\s*(\w+)"
    foreach($vm in (Get-VM | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Guest.OSFullName -like "*Windows*"})){     Write-Host $vm.Name -ForegroundColor "yellow"
       
    Invoke-vmscript -VM $vm -scripttext "get-process"-guestuser user -guestpassword passwd | %{         $lines = $_.ScriptOutput.Split("`r")         3..($lines.Count) | %{             $lines[$_] -match $regex | Out-Null
               
    if([int]$matches[4] -gt 10000) {                 write-host "`t" $matches[9], $matches[4] -foregroundcolor "red"
                }             else {                 write-host "`t" $matches[9], $matches[4]             }         }     } }


  • 3.  RE: Changing Output Color Depending on Process

    Posted May 12, 2011 06:40 PM

    Hello LucD,

    Thanks so much for your response.  I was able to tweak your script and made it fit exactly what I needed.  Kudos sir.

    Best regards