Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active September 12, 2016 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markwragg/0967da2b612bc690f764bd23318aa508 to your computer and use it in GitHub Desktop.
Save markwragg/0967da2b612bc690f764bd23318aa508 to your computer and use it in GitHub Desktop.
Powershell script to get the installed version of Powershell from multiple servers in an Active Directory domain, optionally filtered by part of an OU name (e.g a location).
#Requires -version 2.0
[CmdletBinding()]
param (
[ValidateSet("Dublin","London”,"Tokyo","Sydney","")][string]$location
)
Import-Module ActiveDirectory -Cmdlet get-adcomputer
$servers = get-adcomputer -filter {Enabled -eq $true -and OperatingSystem -Like "Windows*"} -property Enabled,OperatingSystem | ?{$_.DistinguishedName.contains($location)}
$servers | ForEach-Object{
$computer = $_;
if(test-connection $computer.name -TimeToLive 10 -Quiet -Count 1 -ea 0){
$PSVersion = "$(Invoke-Command -Computer $computer.Name -ScriptBlock { $PSVersionTable.PSVersion.Major })"
Write-Verbose "$($computer.name) : $($computer.operatingsystem) : $PSVersion"
new-object -TypeName PSObject -Property @{Name=$computer.name;OS=$computer.operatingsystem;PSVersion=$PSVersion}
}else{
Write-Verbose "$($computer.name) : Unable to connect";
}
} | export-csv "PSVersion-$location-$(get-date -format yyyy-MM-dd).csv" -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment