Showing posts with label Script. Show all posts
Showing posts with label Script. Show all posts

Wednesday, September 25, 2013

Powershell: Ping List of Computers and Export the results to CSV

This script will ping a list of computers and then output the results into CSV.

Get-Content c:\test\computers.txt | ForEach-Object
    {
        if(Test-Connection -ComputerName $_ -Quiet -Count 1) {
            New-Object -TypeName PSCustomObject -Property @{
                ComputerName = $_
                'Ping Status' = 'Ok'
                }
    }
    else
    {
        New-Object -TypeName PSCustomObject -Property @{
            ComputerName = $_
            'Ping Status' = 'Failed'
        }  
    }
} | Export-Csv -Path c:\test\PingStatus.csv -NoTypeInformation

If you find this article useful please leave me a comment or click on an ad to show your support.

Thursday, September 19, 2013

Powershell: Restart service on a list of computers

#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.