VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

invoke-command_usage

pastedImage_0.png

hi Luc ,

can you suggest if we can reslolve above error if the destination server has port 5986 open (for https communication)

right now only http(5985)is open .

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Yes, that is another option, but not in the way you used it.

Like this, it should work

$sessions=new-pssession -ComputerName com1,com2,com3

invoke-command -Session $sessions -ScriptBlock {

    $cpu=Get-WmiObject -Class win32_processor

    $avgcpu=($cpu.loadpercentage | Measure-Object -Average).average

    "" | select @{N='Host';E={$env:COMPUTERNAME}},@{N = 'cpu percentage usage';E={$avgcpu}}

}


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

View solution in original post

0 Kudos
20 Replies
LucD
Leadership
Leadership
Jump to solution

I'm not sure that will be enough.
Have a look at https://serverfault.com/questions/657918/remote-ps-session-fails-on-non-domain-server  for a more extensive procedure.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

tx luc ,i m going to check this it seems this should fix .

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

i found this video which has almost everthing covered .thought of sharing .

i will check on some test servers if i find any...

https://www.youtube.com/watch?v=qvJRaYlxI1w

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That looks like a decent training series.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

if you can tell me how to get following from powershell .iam planning to use invoke-command and the script block to get following .

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Why Invoke-Command?

You can also do

$computerName = 'MyServer'


$cpu = Get-WmiObject -ComputerName $computerName -Class win32_processor -ErrorAction Stop

$mem = Get-WmiObject -ComputerName $computerName -Class win32_operatingsystem -ErrorAction Stop


[PSCustomObject]@{

    Host = $computerName

    CPU = $cpu.LoadPercentage

    Mem = [math]::Round(($mem.TotalVisibleMemorySize -$mem.FreePhysicalMemory)/$mem.TotalVisibleMemorySize * 100)

}


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

this is to fetch information from remote systems .

so i want to put code in scriptblock and use invoke-command for multiple computers sessions

something like below .

$multisession=new-pssession -computername com1,com2,com3 -credential $cred

$multisesion will have session to multiple computers here com1,com2,com3

now we can use

invoke-command -session $multisession -scriptblock{code}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you just use the Get-WmiObject in the scriptblock, minus the CompterName.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

for the time being let me concentrate on cpu part . i think average part will give us the result which are comparable to the data given by most of the tools used to monitor the cpu usage .

is that correct??

$cpu = Get-WmiObject -ComputerName $computerName -Class win32_processor

pastedImage_0.png


pastedImage_1.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, the average would be the single value you see in the Task Manager


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

$sessions=new-pssession -ComputerName com1,com2,com3

invoke-command -Session $sessions -ScriptBlock {

$cpu=Get-WmiObject -Class win32_processor

#$cpu.loadpercentage

$avgcpu=($cpu.loadpercentage | Measure-Object -Average).average

$env:COMPUTERNAME|select @{N = 'cpu percentage usage';E={$avgcpu}}

}

if yu can check the above for cpu (if yu have multiple windows machines) .

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would run it like this

$sessions = New-PSSession -ComputerName com1,com2,com3

Invoke-Command -Session $sessions -ScriptBlock {

    [PSCustomObject]@{

        CPU = ((Get-WmiObject -Class win32_processor).LoadPercentage |

            Measure-Object -Average).average

    }

} | Select PSComputerName,CPU


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

just thought of asking cant we run NE block inside script block.the one i posted ...

is there any thing which is fundamentally wrong??

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure what you mean?

What is a "NE block"?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

sorry i mean

below kind of expression

$env:COMPUTERNAME|select @{N = 'cpu percentage usage';E={$avgcpu}}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, that is another option, but not in the way you used it.

Like this, it should work

$sessions=new-pssession -ComputerName com1,com2,com3

invoke-command -Session $sessions -ScriptBlock {

    $cpu=Get-WmiObject -Class win32_processor

    $avgcpu=($cpu.loadpercentage | Measure-Object -Average).average

    "" | select @{N='Host';E={$env:COMPUTERNAME}},@{N = 'cpu percentage usage';E={$avgcpu}}

}


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

can you please tell me what is "" before pipeline .are we using it because there is no direct property (i.e computername and avgcpu)

""| select @{N='Host';E={$env:COMPUTERNAME}},@{N = 'cpu percentage usage';E={$avgcpu}}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You have to provide an input object (in this an empty string) to the Select-Object.

And then on the Select-Object you can use calculated properties (your NE block :smileygrin:)


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks i will be adding memory also .

0 Kudos