Automation

 View Only
Expand all | Collapse all

invoke-command_usage

  • 1.  invoke-command_usage

    Posted Feb 27, 2020 06:08 PM

    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 .



  • 2.  RE: invoke-command_usage

    Posted Feb 27, 2020 06:37 PM

    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.



  • 3.  RE: invoke-command_usage

    Posted Feb 27, 2020 07:05 PM

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



  • 4.  RE: invoke-command_usage

    Posted Feb 28, 2020 02:41 PM

    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



  • 5.  RE: invoke-command_usage

    Posted Feb 28, 2020 02:59 PM

    That looks like a decent training series.



  • 6.  RE: invoke-command_usage

    Posted Mar 03, 2020 10:49 AM

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



  • 7.  RE: invoke-command_usage

    Posted Mar 03, 2020 11:25 AM

    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)

    }



  • 8.  RE: invoke-command_usage

    Posted Mar 03, 2020 11:51 AM

    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}



  • 9.  RE: invoke-command_usage

    Posted Mar 03, 2020 11:56 AM

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



  • 10.  RE: invoke-command_usage

    Posted Mar 03, 2020 12:32 PM

    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




  • 11.  RE: invoke-command_usage

    Posted Mar 03, 2020 12:35 PM

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



  • 12.  RE: invoke-command_usage

    Posted Mar 03, 2020 01:50 PM

    $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) .



  • 13.  RE: invoke-command_usage

    Posted Mar 03, 2020 01:58 PM

    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



  • 14.  RE: invoke-command_usage

    Posted Mar 03, 2020 02:11 PM

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

    is there any thing which is fundamentally wrong??



  • 15.  RE: invoke-command_usage

    Posted Mar 03, 2020 02:13 PM

    Not sure what you mean?

    What is a "NE block"?



  • 16.  RE: invoke-command_usage

    Posted Mar 03, 2020 02:16 PM

    sorry i mean

    below kind of expression

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



  • 17.  RE: invoke-command_usage
    Best Answer

    Posted Mar 03, 2020 02:21 PM

    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}}

    }



  • 18.  RE: invoke-command_usage

    Posted Mar 03, 2020 02:41 PM

    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}}



  • 19.  RE: invoke-command_usage

    Posted Mar 03, 2020 02:43 PM

    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:)



  • 20.  RE: invoke-command_usage

    Posted Mar 03, 2020 02:47 PM

    Thanks i will be adding memory also .



  • 21.  RE: invoke-command_usage

    Posted Mar 03, 2020 06:34 PM

    just thought of checking about pscustomobject as per yur previous code .

    found it is modern approach and give cleaner output so following shud work for multiple computers .

    since sessions might run parallely so iam using $env:computername for corresponding session .

    $sessions=new-pssession -ComputerName com1,com2,com3
    Invoke-Command -Session $sessions -ScriptBlock {


    $computerName = $env:COMPUTERNAME

    $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|Measure-Object -Average).average
        Mem = [math]::Round(($mem.TotalVisibleMemorySize -$mem.FreePhysicalMemory)/$mem.TotalVisibleMemorySize * 100)

        }

        }