简体   繁体   中英

Optimizing Powershell Script to Query Remote Server OS Version

I want optimize a simple task: pull server OS version into a neat table. However, some servers in our environment have Powershell disabled. Below you fill find my script, which works, However, it takes about 20 seconds or so per server. since it waits for the server to return the results of the invoked command before moving onto the next server in the list, I know there's a way to asynchronously pull the results from a PS command, but is this possible when I need to resort to cmd line syntax for servers that can't handle PS? as shown in the catch statement?

$referencefile = "ps_servers_to_query.csv"
$export_location = "ps_server_os_export.csv"
$Array = @()
$servers = get-content $referencefile

foreach ($server in $servers){
        #attempt to query the server with Powershell. 
        try{
        
            $os_version = invoke-command -ComputerName $server -ScriptBlock {Get-ComputerInfo -Property WindowsProductName} -ErrorAction stop
            $os_version = $os_version.WindowsProductName

        } # If server doesnt have PS installed/or is disabled, then we will resort to CMD Prompt, this takes longer however.. also we will need to convert a string to an object.  
        catch {
            $os_version = invoke-command -ComputerName $server -ScriptBlock {systeminfo | find "OS Name:"} # this returns a string that represents the datetime of reboot
            $os_version = $os_version.replace('OS Name: ', '') # Remove the leading text
            $os_version = $os_version.replace('  ','') # Remove leading spaces 
            $os_version = $os_version.replace('Microsoft ','') # Removes Microsoft for data standardization 

     }  
      # Output each iteration of the loop into an array
        $Row = "" | Select ServerName, OSVersion
        $Row.ServerName = $Server
        $Row.OSVersion = $os_version
        $Array += $Row
    }

# Export results to csv. 
$Array | Export-Csv -Path $export_location -Force 

Edit: Here's what I'd like to accomplish. Send the command out to all the servers (less than 30) at once, and have them all process the command at the same time rather than doing it one-by-one. I know I can do this if they all could take PowerShell commands, but since they can't I'm struggling. This script takes about 6 minutes to run in total.

Thank you in advance!

If I got it right something like this should be all you need:

$referencefile = "ps_servers_to_query.csv"
$export_location = "ps_server_os_export.csv"
$ComputerName = Get-Content -Path $referencefile
$Result = 
Get-CimInstance -ClassName CIM_OperatingSystem -ComputerName $ComputerName | 
Select-Object -Property Caption,PSComputerName
$Result 
| Export-Csv -Path $export_location -NoTypeInformation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM