none
powershell to get remote server information using WinRM

    Question

  • Apologies for my insufficient informaiton in earlier post.

    I am looking to collect server information like Name, Domainname, Operating system version etc for all the servers in the network using script. i have used WMI script to get connect remotely to the server and fetch information put them in a report. its working for some servers and failing on many servers becuase these servers are behind the firewall and Firewall blocks WMI traffic.

    i was thinking to use WinRM to collect the information as WinRM sends HTTP Traffice instead of WMI queries. i tried searching for examples but i could not find any examples. i am wondering if any one can help me with a simple example to query server information using WinRM instead of WMI. any help would greatly be appreciated. 

    Script:

    $wmi = [wmisearcher]""
    $query = "select * from win32_computersystem"
    $wmi.query = $query
    $wmi.Scope.Path = "\\$HostName\root\cimv2"
    $wmi.Scope.Options.Authentication = "PacketPrivacy"
    $wmi.Scope.Options.Impersonation = "Impersonate"
    $wmi.Scope.Options.Username = $domain + "\" + $username
    $wmi.Scope.Options.Password = $password
    $wmiresults = $wmi.Get()
    foreach ($wmioutput in $wmiresults){
      write-host $wmioutput.DNSHostName
    write-host $wmioutput.Domain
    }

    Error for the server behind the firewall:

    Exception calling "Get" with "0" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"
    At C:\work\Script\ps\Checkout\SiteCheckout.ps1:133 char:24
    +     $wmiresults = $wmi.Get <<<< ()
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException


    Thank You

    Vamsi.


    • Edited by vamsinm Thursday, June 13, 2013 3:18 PM insufficient info in the post
    Thursday, June 13, 2013 2:29 PM

Answers

  • Get-ciminstance Win32_ComputerSystem -computername $TargetComputer | select DNSHostName,Domain

    Use get-ciminstance if you're on PowerShell V3

    If you're on V2 use PSRemoting:

    Invoke-command -computername $TargetComputer -scriptblock {

    Get-WMIObject Win32_ComputerSystem | select DNSHostName,Domain

    }



    Hope that helps! Jason

    • Marked as answer by vamsinm Thursday, June 13, 2013 8:44 PM
    Thursday, June 13, 2013 5:27 PM

All replies

  • Hi Bill, not sure whats wrong with the post. i am not asking some one to do work for me. i did my search to find examples of scripts that uses Winrm instead of WMI to collect server information. i am just looking for any script examples that uses winrm to collect any server information so i can understand the calling method and do what i needed

    thank you

    vamsi.

    Thursday, June 13, 2013 2:51 PM
  • Here's some sample information from one of the guidelines:

    When you set out to ask a question, you should...
    • Describe what's happening in sufficient detail that we can follow along. Provide the necessary background for us to understand what's going on, even if we aren't experts in your particular area.
    • Tell us why you need to know the answer. What led you here? Is it idle curiosity or somehow blocking you on a project? We don't require your whole life story, just give us some basic context for the problem.
    • Share any research you did towards solving your problem, and what you found, if anything. And if you didn't do any research – should you even be asking?
    • Ultimately, this is about fairness: if you're going to ask us to spend our valuable time helping you, it's only fair that you put in a reasonable amount of your valuable time into crafting a decent question. Help us help you!

    Bill

    Thursday, June 13, 2013 3:04 PM
  • thank you i have edited  and submit my question. 
    Thursday, June 13, 2013 3:21 PM
  • Get-ciminstance Win32_ComputerSystem -computername $TargetComputer | select DNSHostName,Domain

    Use get-ciminstance if you're on PowerShell V3

    If you're on V2 use PSRemoting:

    Invoke-command -computername $TargetComputer -scriptblock {

    Get-WMIObject Win32_ComputerSystem | select DNSHostName,Domain

    }



    Hope that helps! Jason

    • Marked as answer by vamsinm Thursday, June 13, 2013 8:44 PM
    Thursday, June 13, 2013 5:27 PM
  • Both commands have a -credential parameter you can use if you need to specify credentials

    Hope that helps! Jason

    Thursday, June 13, 2013 5:28 PM
  • Thank you Jason! thats really helpful.

    is invoke-command output different from actual get-wmiobject object? i am using to get iis settings on the server and i could not read bindings information. it works fine when i just use get-wmiobject with out invoke-command on the local computer but throws error as it could not convert the value if i use invoke-command and get information from remote server. any help greatly appreciated. 

    code:

    $wmiresults= Invoke-command  -computername "REMOTESERVER"  -credential $cred -scriptblock { get-wmiobject -class Site -namespace "root\WebAdministration" | select id, Name, Bindings}
    [array]$ArrSitenames = @()
    foreach ($wmioutput in $wmiresults){
    $bindings =[System.Management.ManagementBaseObject[]]$wmioutput.Bindings
    #write-host $bindings.count
    foreach ($binding in $bindings){
    write-host $binding.protocol
    }

    Error:

    Cannot convert value "System.Collections.ArrayList" to type "System.Management.ManagementBaseObject[]". Error: "Cannot convert the "System.Management.ManagementBaseObject" value of type "Deserialized.System.Management.ManagementBaseObject#\BindingElement" to type "System.Management.ManagementBaseObject"."

    At C:\winrmtest.ps1:136 char:68
    +          $bindings =[System.Management.ManagementBaseObject[]]$wmioutput. <<<< Bindings
        + CategoryInfo          : NotSpecified: (:) [], RuntimeException
        + FullyQualifiedErrorId : RuntimeException

    Than kyou

    vamsi.

    Thursday, June 13, 2013 8:53 PM
  • You'll want to read about PSRemoting... The short answer is yes, the objects returned are different, specifically the object returned from invoke-command has been stripped of all it's methods.

    If you want to operate on the object you'll need to do it within the context of invoke-command

    Invoke-command -computername computer -Scriptblock {
        $wmi = get-wmiobject Win32_computersystem
        $wmi.shutdown()
    }
    
    #call the method you want to use inside the scriptblock


    Hope that helps! Jason

    Thursday, June 13, 2013 9:39 PM
  • Also do yourself a favor and get out of the habit of using write-host...  It's the worst and it angers the PowerShell gods when you use it.  You want to output objects, not strings.

    Hope that helps! Jason

    Thursday, June 13, 2013 9:41 PM