Skip to content

Instantly share code, notes, and snippets.

@aaronk1
Last active November 22, 2016 22:40
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 aaronk1/77bb4831f303bdd6c52ec318253b22ea to your computer and use it in GitHub Desktop.
Save aaronk1/77bb4831f303bdd6c52ec318253b22ea to your computer and use it in GitHub Desktop.
get_powershell_version.ps1
# Supports Windows 2012 R2 and up. Ensure you have Local Administrator access and network connectivity to the target servers for whatever account runs this.
Function ConfirmState
{
param($confirm_feature,$truetext,$falsetext)
if($truetext -eq $null){$truetext = "$confirm_feature was successfully installed!"}
if($falsetext -eq $null){$falsetext = "ERROR!!! $confirm_feature was NOT INSTALLED!"}
$installstate = Get-WindowsOptionalFeature -Online -FeatureName $confirm_feature
if($installstate.state -Like "Enabled"){$truetext| %{ Write-Host -foreground "green" $_; $_ } | Out-File $log_file -Append}else{$falsetext | %{ Write-Host -foreground "red" $_; $_ } | Out-File $log_file -Append}
}
$ErrorActionPreference = "Stop"
$log_file = "ad_powershell.log"
$winfeatures = "ActiveDirectory-Powershell"
foreach ($feature in $winfeatures)
{
"...Installing $feature" | Out-File $log_file -Append
Enable-WindowsOptionalFeature -Online -FeatureName $feature -norestart -source https://windowsupdate.microsoft.com | Out-File $log_file -Append
ConfirmState $feature
}
Import-Module ActiveDirectory
#Enumerate all Computers in Active Directory
Get-ADComputer -Filter * -ResultPageSize 3084 |
ForEach-Object {
$ping = Test-Connection -CN $_.dnshostname -Count 2 -BufferSize 16 -Quiet
Write-Host -ForegroundColor green $_.dnshostname
Add-Content -Path "C:\Windows\Temp\Servers.txt" -Value $_.dnshostname
}
$computers = Get-Content "C:\Windows\Temp\Servers.txt"
#object to collect all information
$infoColl = @()
foreach ($computer in $computers) {
$psversion=Invoke-Command -ComputerName $computer -ScriptBlock {$PSVersionTable.PSVersion}
$infoObject = New-Object PSObject
Add-Member -inputObject $infoObject -memberType NoteProperty -name "ServerFQDN" -value $computer
Add-Member -inputObject $infoObject -memberType NoteProperty -name "PSVersion" -value $psversion
$infoObject #Output to the screen for a visual feedback.
$infoColl += $infoObject
}
# Export the results in csv file. The file is in the same path as the ps1 and txt script
$infoColl | Export-Csv -path .\$((Get-Date).ToString('MM-dd-yyyy'))_Server_Inventory.csv -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment