PowerShell Get Windows OS Version Fast and Do Different Things

1.4k views Asked by At

Is there a faster way to get a specific registry value from a list of servers? I'm selecting a text file of computers with different flavors of windows and getting the OS product name. I'm finding that it's taking a couple seconds per computer to retrieve.

Current script:

Clear-Host

# Prompt for file containing list of target
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$myDialog = New-Object System.Windows.Forms.OpenFileDialog
$myDialog.Title = "Select File of Target Systems"
$myDialog.InitialDirectory = $PSScriptRoot
$myDialog.Filter = "TXT (*.txt) | *.txt"
$result = $myDialog.ShowDialog()

If ($result -eq "OK") {
    $Computers = Get-Content $myDialog.FileName
}
Else {
    Write-Host "`nCancelled by User`n"
}

$Array = @()
  
# Loop Through Computers
ForEach ($Computer in $Computers) {
    Write-Warning "Processing $Computer"
       
    # Get Registry Values
    Try {      
        $OSVersion = Invoke-Command -ComputerName $Computer -ScriptBlock { (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName }
 
        # Create a custom object 
        $ComplexObject = New-Object PSCustomObject
        $ComplexObject | Add-Member -MemberType NoteProperty -Name "Server name" -Value $Computer
        $ComplexObject | Add-Member -MemberType NoteProperty -Name "OS Version" -Value $OSVersion
 
        # Add custom object to our array
        $Array += $ComplexObject
    }
    Catch {
        $_.Exception.Message
        Break
    }

}
 
# Results
If ($Array) {
    # Display results in new window
    $Array | Out-GridView -Title "OS Version Results"
 
    # Display results in PS console
    $Array
}

My end goal later on in the script is to do different things based on the OS version so I want to separate them into independent lists:

If (We have Win2008 servers) {
    "Do This"
}
If (We have Win2012R2 servers) {
    "Do This"
}
2

There are 2 answers

0
Wasif On BEST ANSWER

You can use Get-AdComputer like:

Get-ADComputer -Filter {(OperatingSystem -like "*windows*server*") -and (Enabled -eq "True")} -Properties OperatingSystem | Select -ExpandProperty OperatingSystem | ForEach {
  If($_ -match "Windows Server 2008.*"){
    # Server 2008
  }
  If($_ -match "Windows Server 2012.*"){
    # Server 2012
  }
  # Add more like 2016,2019
}
0
Doug Maurer On
Clear-Host

# Prompt for file containing list of target
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$myDialog = [System.Windows.Forms.OpenFileDialog]::new()
$myDialog.Title = "Select File of Target Systems"
$myDialog.InitialDirectory = $PSScriptRoot
$myDialog.Filter = "TXT (*.txt) | *.txt"
$result = $myDialog.ShowDialog()

If ($result -eq "OK") {
    $Computers = Get-Content $myDialog.FileName
}
Else {
    Write-Host "`nCancelled by User`n"
}

# Get Registry Values
$Array = Try {      
        Invoke-Command -ComputerName $Computers -ScriptBlock {
            (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
    } -ErrorAction stop | Select-Object @{n="Server Name";e={$_.pscomputername}},
                                        @{n="OS Version";e={$_}}
}
Catch {
    write-warning $_.Exception.Message
    break
}

# Results
If ($Array) {
    # Display results in new window
    $Array | Out-GridView -Title "OS Version Results"

    # Display results in PS console
    $Array
}