using parallel invoke for publishing cpu usage from a serverlist

This topic contains 15 replies, has 2 voices, and was last updated by Profile photo of Mikhail Kramnik Mikhail Kramnik 9 months, 4 weeks ago.

  • Author
    Posts
  • #35192
    Profile photo of Mikhail Kramnik
    Mikhail Kramnik
    Participant

    I am new to PS. Having trouble to understand scriptblock with the invoke-command. Hope someone can help me debug this. Thanks. I am trying to do the following.

    $Servers = get-content c:\serverlist.txt

    invoke-command – throttlelimit 3 -Scriptblock

    { Get-Wmiobject -computername $Servers win32processor | Measure-Object – property LoadPercentage – Average | Select Average}

  • #35193
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Ok, so what's the problem?

  • #35195
    Profile photo of Mikhail Kramnik
    Mikhail Kramnik
    Participant

    missing an argument for parameter 'Scriptblock' :-(. can't run

  • #35196
    Profile photo of Don Jones
    Don Jones
    Keymaster

    So, it's tough to diagnose because you didn't format your code here, but when you run this for real do you have the carriage returns before the opening brace? It needs to be

    ... -Scriptblock {
    

    With no carriage returns. Additionally, your $servers variable has no meaning in the remote machine. That variable only exists locally. I suspect you meant to use the -computername parameter of Invoke-Command, but you're using it on Get-WmiObject instead.

    In this context you don't actually need Invoke-Command. Just use Get-Wmiobject exactly as you've typed it. Add the -AsJob parameter. Good to go

  • #35199
    Profile photo of Mikhail Kramnik
    Mikhail Kramnik
    Participant

    Thanks for helping, Don, Appreciate. All I wanted was to have PS read the serverlist I keep locally, then remote each server in the serverlist in groups of like 5-10 in parallell and ask them to report back in parallel their cpu usage, hence I have to use invoke-command parallel with Get-Wmiobject and throttle to 5. But it seems that it cannot work due to my poor scripting in the scriptblock. I am not sure how to add -AsJob. Thanks.

  • #35218
    Profile photo of Don Jones
    Don Jones
    Keymaster

    OK.

    So, Invoke-Command when used without -ComputerName isn't really useful, because it's just running the command on your local machine. But if your goal is to parallelize, jobs are one technique:

    Get-Wmiobject -computername $Servers win32processor -AsJob
    

    You could then use Receive-Job to receive the job results. Let's say the above returned job ID number 1:

    Receive-Job -Id 1 | Measure-Object –property LoadPercentage –Average | Select Average
    

    Get-WmiObject doesn't support any kind of throttle, so you'd get the system default of 32. Alternately, if Remoting is enabled on all the remote machines, you could use Invoke-Command.

    invoke-command  -computername $Servers –throttlelimit 3 -Scriptblock { Get-Wmiobject win32processor } | Measure-Object – property LoadPercentage – Average | Select Average
    

    Notice that in both examples I've given, what you're going to get is a SINGLE AVERAGE FOR ALL COMPUTERS QUERIED. You're not getting a per-computer average. I'm not sure what the goal was. Compare that to this:

    invoke-command  -computername $Servers –throttlelimit 3 -Scriptblock { Get-Wmiobject win32processor | Measure-Object – property LoadPercentage – Average | Select Average }
    

    What's different is that the Measure-Object is being run on the remote computers, so each computer is measuring its own average, and it's that per-computer average coming back to your machine. PowerShell will add a PSComputerName property to the output, so you can tell which computer's output is which. But in both of my Invoke-Command examples, notice where -computername is put. It's my local computer that processes Invoke-Command, sending the -ScriptBlock off to the machines specified in -ComputerName.

    Happy to answer questions – I want to make sure you understand the differences and what's happening where.

  • #35243
    Profile photo of Mikhail Kramnik
    Mikhail Kramnik
    Participant

    Thanks, Don but the first one could not be run. invalid argument. think we have syntax issue.

    invoke-command -computername $Servers –throttlelimit 3 -Scriptblock { Get-Wmiobject win32processor } | Measure-Object – property LoadPercentage – Average | Select Average

    Measure-Object : A positional parameter cannot be found that accepts argument 'property'. At line: 1 char:119.

    As for the 2nd one, I ran it and I have errors too. 🙁

    OpenError: (:) [], PSRemotingTransportException
    PSSessionStateBroken

    what do I need to do to use invoke-command remotely? many thanks. appreciate if you have a procedure to follow.

  • #35244
    Profile photo of Mikhail Kramnik
    Mikhail Kramnik
    Participant

    Don,

    I tried these too:

    Get-Wmiobject -computername $Servers win32processor -AsJob
    Receive-Job -Id 1 | Measure-Object –property LoadPercentage –Average | Select Average

    Futile 🙁

    I got: Received-Job: Access is denied. (Exception from Hresult: 0x80070005 (E_Accessdenied). *sigh* what should i do?

  • #35246
    Profile photo of Don Jones
    Don Jones
    Keymaster

    That means you don't have remote access to WMI. I can't fix that :).

  • #35247
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Are you running the shell as Admin?

  • #35250
    Profile photo of Mikhail Kramnik
    Mikhail Kramnik
    Participant

    Yes, I ran PS as Admin. But computer that I am using is on VPN connection to the network. The computer that I used to run PS is on a domain and the servers which I tried to list usage are just on Workgroup (not in my office domain). How do I make invoke-command works and how do I get remote access to WMI on a non-domain server? Sorry it sounds complicated. Is this related to second hop issue? Is there a possible workaround?

  • #35252
    Profile photo of Mikhail Kramnik
    Mikhail Kramnik
    Participant

    Error messages for diagnostics: (Thanks, Don)

    PS C:\Windows\system32> invoke-command -computername Server1 -Scriptblock { Get-Wmiobject win32processor | Measure-Object – property LoadPercentage – Average | Select Average }

    [Server1] Connecting to remote server Server1 failed with the following error message

    : WinRM cannot process the request. The following error occurred while using Kerberos authentication: Cannot find the computer Server1. Verify that the computer exists on the network and that the name provided is spelled correctly. For more information, see the about_Remote_Troubleshooting Help topic.

    + CategoryInfo : OpenError: (Server1:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : NetworkPathNotFound,PSSessionStateBroken

    PS C:\Windows\system32> invoke-command -computername 123.12.1.2 -Scriptblock { Get-Wmiobject win32processor | Measure-Object – property LoadPercentage – Average | Select Average }

    [123.12.1.2] Connecting to remote server 123.12.1.2 failed with the following error message : The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use
    winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. For more information on how to set TrustedHosts run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.

    + CategoryInfo : OpenError: (123.12.1.2:String) [], PSRemotingTransportException

    + FullyQualifiedErrorId : CannotUseIPAddress,PSSessionStateBroken

  • #35253
    Profile photo of Mikhail Kramnik
    Mikhail Kramnik
    Participant

    Don, how do I put the ps command line into the box like what you did here earlier? Would be great to learn that to be neat. 🙂 Thanks.

  • #35269
    Profile photo of Don Jones
    Don Jones
    Keymaster

    "Don, how do I put the ps command line into the box like what you did here earlier? Would be great to learn that to be neat."

    See the bullet point immediately above the posting text box for instructions – enclose code blocks in PRE tags.

    Regarding your error, you should really read "Secrets of PowerShell Remoting," which is free here on the "Resources / eBooks" menu item. Your problem is that, by default, only Kerberos is enabled for authentication in Remoting. Kerberos requires you to connect to a computer's canonical name from Active Directory – not a nickname, and not an IP address. If you want to use an IP address, you need to (a) enable another authentication mechanism, probably Basic, and (b) either add the computer to a Trusted Hosts list on the initiating side or install an SSL certificate. Using Basic has security concerns as it passes credentials in clear text. But rather than just re-type all of that here, I'd really prefer you read the ebook where it's all already typed out ;).

    You can also, and should, read the help topics referenced in the error messages. This isn't just a case of "push this button to make it work" – you have decisions to make, and those decisions impact security and functionality. It's more important you know WHY you are making those decisions than to have someone like me just tell you what will make the error go away.

  • #35288
    Profile photo of Mikhail Kramnik
    Mikhail Kramnik
    Participant

    Don, thanks a lot for your help so far. I have actually enable PSRemoting but it still does not work due to Kerberos thing. I browse through "Secrets of PowerShell Remoting" but nothing on Kerberos stuff. Where can I find this stuff to read and fix my problem? When you say Kerberos requires me to connect to a computer's canonical name from Active Directory, what do you exactly mean? Does it mean I need to connect my PC to its LAN and connect/login to its office domain? Which means I can't remote desktop and run Powershell then invoke-command? Sorry, but I don't get what you actually mean. This remote computer is a blade on a rack and there is no VDU, keyboard etc and my only access is through RDP. Also, there are hundreds of these blades that I need to monitor cpu usage and that's why parallel invoke-command would help. But, this time, it seems that it cannot work because of kerberos 🙁 bummer

  • #35289
    Profile photo of Mikhail Kramnik
    Mikhail Kramnik
    Participant

    pardon me, Don.I think I was reading off the pdf format which does not have everything. I am looking at the online version now.

You must be logged in to reply to this topic.