I know its not pretty, but it saves me alot of work trying to figure to execute a script on a huge environment
#sets working directory to where the script is run $scriptpath = $MyInvocation.MyCommand.Path $dir = Split-Path $scriptpath #sets error handling $ErrorActionPreference='SilentlyContinue' #imports csv list $server=Import-Csv $dirconnections.csv #the $i=0 #runs the below script for each computer in list $display=foreach ($s in $server.connectionname.Trim()) { #checks server for compatibility $version=$null $version=Get-ADComputer -filter {name -like $s} -Properties * #ticking up Write-host $i $i=$i+1 #pings each server $connection=Test-Connection $s -Count 1 -ErrorAction SilentlyContinue if($connection){} else{$connection=Test-Connection $s -Count 1 -ErrorAction SilentlyContinue} if($connection){} else{$connection=Test-Connection $s -Count 1 -ErrorAction SilentlyContinue} $connection=$connection.ipv4address.ipaddresstostring | select -First 1 #if windows version is incompatible with remote scripts if($version| ?{ $_.OperatingSystem -notlike "Windows Server® 2008*"}){ #if the computer is not online it skips to the bottom if($connection){ #Calls out to the remote computer to run commands Invoke-Command -ComputerName $s -ScriptBlock { ######## # #Put what ever you want to run here # ######### #assembles the data New-Object psobject -Property @{ server=$env:computername IP= $using:connection OS=$using:version.OperatingSystem fail="none" } } } else{ #if the computer is offline is puts out null fields New-Object psobject -Property @{ server=$s IP=$connection.ipv4address.ipaddresstostring | select -First 1 os=$version.OperatingSystem fail="offline" } } } #if computer is 2008 it sends this into file Else{ New-Object psobject -Property @{ Server=$s IP=$connection.ipv4address.ipaddresstostring | select -First 1 os=$version.OperatingSystem fail="2008 or less" } } } #this converts it to a CSV file and orders the output $display | select server,IP,os,netversion,fail | Format-Table
submitted by /u/mimic751
[link] [comments]
The post This checks if a computer is online and running what OS its running. If it passes the tests it runs a remote script appeared first on How to Code .NET.