powershell version

Glenn Maxwell 10,146 Reputation points
2020-09-28T16:43:29.68+00:00

Hi All
i am local admin on all the servers, i have servers in csv file in the below format, i want to get PowerShell version on all the servers using the below PowerShell script but i am getting RPC error
hostnames
server01.contos.com
server02.contoso.com

$hostnames = get-content c:\servers.csv
foreach ($hostname in $hostnames)
{
$os = Get-WmiObject -ComputerName $hostname -Class Win32_OperatingSystem | select-object CSName,Caption,BuildNumber
$psver = Invoke-Command -ComputerName $hostname -Scriptblock {$PSVersionTable.psversion.Build} -ErrorAction SilentlyContinue
Export-CSV C:\output.csv -NoTypeInformation -Append
}

i am getting the below error

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\script.ps1:5 char:7

  • $os = Get-WmiObject -ComputerName $hostname -Class Win32_OperatingSys ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
  • FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,370 questions
Windows Server 2012
Windows Server 2012
A Microsoft server operating system that supports enterprise-level management, data storage, applications, and communications.
1,528 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,113 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2020-09-28T18:24:18.06+00:00

    Is your input file really a CSV? If it is, the first line will be a "header" that contains the column names and not the name of a computer.

    Assuming you have a CSV and that there's a column named "Hostname", try this:

    Import-CSV c:\servers.csv |
    ForEach-Object{
        $os = Get-WmiObject -ComputerName $_.Hostname -Class Win32_OperatingSystem | 
            select-object CSName,Caption,BuildNumber
        Try{
            $psver = Invoke-Command -ComputerName $_.Hostname -Scriptblock {$PSVersionTable.psversion.Build} -ErrorAction STOP
        }
        Catch{
            $psver = "Not Found:  $_"
        }
        Add-Member -InputObject $os -MemberType NoteProperty -Name PSVersion -Value $psver
        $os
    } |
        Export-CSV C:\output.csv -NoTypeInformation
    

    Also, your code won't export anything because you aren't feeding the Export-CSV any data.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Leon Laude 85,651 Reputation points
    2020-09-28T16:59:07.753+00:00

    Hi @Glenn Maxwell ,

    The error below is normally the result you get when a machine is either not reachable, it could be for example offline, or when the firewall is blocking you from querying the Windows Management Instrumentation (WMI):

    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

    I'd check that the "Windows Management Instrumentation (WMI-In)" rule is enabled in the firewall for each remote machine.

    ----------

    (If the reply was helpful please don't forget to upvote or accept as answer, thank you)

    Best regards,
    Leon

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2020-09-29T09:35:43.903+00:00

    Hi,
    Are these servers all available?If yes, please use Import-Csv to read CSV files as Get-Content doesn't handle the column headers correctly.

    Best Regards,
    Ian

    0 comments No comments

  3. Glenn Maxwell 10,146 Reputation points
    2020-09-29T12:40:31.767+00:00

    Import-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "C:\servers.csv"
    to type "System.Char". Error: "String must be exactly one character long."
    At C:\script.ps1:1 char:24

    • ... ort-CSV get-content C:\script.ps1 |
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidArgument: (:) [Import-Csv], ParameterBindingException
    • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ImportCsvCommand