#Gets a list of machines and then restarts the specified service on each computer.
#Get list of computers
$Computers = Get-Content C:\myscript\serverlist2.txt
#what service
$service = "spooler"
#Function to create object
function OutData($computer,$service,$Object) {
$out = New-Object psobject
$out | add-member -type noteproperty -name Computer $computer
$out | add-member -type noteproperty -name Service $service
$out | add-member -type noteproperty -name Result $Object
$out
}
#create empty variable
$result = @()
#main
foreach ($computer in $computers)
{
if (Test-Connection -ComputerName $computer -Quiet -count 1)
{
$Object = "Restarted $($service) service"
Restart-Service -InputObject $(Get-Service -Computer $computer -Name $service)
$result += Outdata $computer $service $object
}
else
{
$object = "$($computer) not online"
$result += Outdata $computer $service $object
}
}
#export the results
$result | Export-Csv C:\myscript\RestartServiceResults.csv -NoTypeInformation
$Result
If you find this article useful please leave me a comment or click on an ad to show your support.
Great script, since Im new to powershell this was big help!!!
ReplyDeleteThanks.