Using Powershell to copy files to every server
##################################################################################
#
# Copy files to all servers with AD accounts that respond to PING
#
# Requires admin permission on every server
#
##################################################################################
$file1="Windows6.1-KB2520155-x64.msu"
$file2="Windows6.1-KB2520155-x86.msu"
$ServerList = ".\SUCCESS.TXT"
$ErrorList = ".\ERRORS.TXT"
$ListFile = ".\SERVERS.TXT"
New-Item $ListFile -Type file -Force >$nul
New-Item $ServerList -Type file -Force >$nul
New-Item $ErrorList -Type file -Force >$nul
$List = ""
"Execution in progress..."
# Create $list of AD machine accounts for Windows Servers
$strCategory = "computer"
$strOS = "Windows*Server*"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("OperatingSystem=$strOS")
$colProplist = "dnshostname"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults) {
$objComputer = $objResult.Properties;
$Server = $objComputer.dnshostname
$Server = $Server -replace "\.usa\.domain\.com", ""
$Server = $Server -replace "\s{2,}", ""
if ($Server) {#skip null
$Server
if (Test-Connection -ComputerName $Server -quiet -count 1) {#PING OK
" Responds to PING"
$Server | out-file -encoding ASCII -filepath $ListFile -append
#Copy Files
copy-item c:\dns-msu -destination ("\\\\"+$Server+"\\C$") -recurse
#Check File1
if (-not(Test-path ("\\\\"+$Server+"\\C$\\dns-msu\\$file1"))) {
" FAIL: $file1"
write-output "$Server - MISSING $file1" | out-file -encoding ASCII -filepath $ErrorList -append
}
else {
" SUCCESS: $file1"
write-output "$Server - OK $file1" | out-file -encoding ASCII -filepath $ServerList -append
}
#Check File2
if (-not(Test-path ("\\\\"+$Server+"\\C$\\dns-msu\\$file2"))) {
" FAIL: $file2"
write-output "$Server - MISSING $file2" | out-file -encoding ASCII -filepath $ErrorList -append
}
else {
" SUCCESS: $file2"
write-output "$Server - OK $file2" | out-file -encoding ASCII -filepath $ServerList -append
}
}#end if PING OK
else {#PING FAIL
" Does not respond to PING"
write-output "$Server - PING Failure" | out-file -encoding ASCII -filepath $ErrorList -append
}#end else PING FAIL
}#if null
}#foreach
No comments:
Post a Comment