Advertisement
opexxx

Restart-Service.ps1

Mar 8th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     .Synopsis
  3.         Restart a service on list of remote computers.
  4.        
  5.     .Description
  6.         This script helps in restarting a service remotely on list of remote computers.
  7.  
  8.     .Parameter ComputerName    
  9.         Computer name(s) for which you want to get the disk space details.
  10.        
  11.     .Example
  12.         Restart-Service.ps1 -ComputerName Comp1, Comp2 -ServiceName dnscache
  13.        
  14.         Restart DNSCache service on Comp1 and Comp2 computers and report the status
  15.        
  16.     .Notes
  17.         NAME:      Restart-Service.ps1
  18.         AUTHOR:    Sitaram Pamarthi
  19.         WEBSITE:   http://techibee.com
  20.  
  21. #>
  22.  
  23. [cmdletbinding()]
  24. param(
  25.     [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
  26.     [string[]]$ComputerName = $env:computername,
  27.    
  28.     [parameter(Mandatory=$true)]
  29.     [string]$ServiceName,
  30.    
  31.     [string]$OutputDir = "C:\"
  32. )
  33.  
  34. begin {
  35. }
  36. process{
  37.  
  38.     $SuccessComputers  = Join-Path $OutputDir "SuccessComputers.txt"
  39.     $FailedComputers   = join-path $OutputDir "FailedComputers.txt"
  40.     $OutputArray = @()
  41.     foreach($Computer in $ComputerName) {
  42.         $OutputObj  = New-Object -TypeName PSobject
  43.         $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.TOUpper()
  44.         Write-Verbose "Working on $Computer"
  45.         $Status = "Failed"
  46.         $IsOnline=$false
  47.         if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
  48.             $IsOnline = $true
  49.             try {
  50.                 $ServiceObj = Get-Service -Name $ServiceName -ComputerName $Computer -ErrorAction Stop
  51.                 Restart-Service -InputObj $ServiceObj -erroraction stop
  52.                 $Status="Success"
  53.                
  54.             } catch {
  55.                 Write-Verbose "Failed to restart $Service on $Computer. Error: $_"
  56.                 $Status="Failed"
  57.             }
  58.            
  59.            
  60.         }
  61.         else {
  62.             Write-Verbose "$Computer is not reachable"
  63.             $IsOnline = $false
  64.            
  65.         }
  66.         $OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value $Status
  67.         $OutputObj | Add-Member -MemberType NoteProperty -Name IsOnline -Value $IsOnline
  68.         $OutputObj
  69.         $OutputArray += $OutputObj
  70.     }
  71.  
  72.     $OutputArray | ? {$_.Status -eq "Failed" -or $_.IsOnline -eq $false} | Out-File -FilePath $FailedComputers
  73.     $OutputArray | ? {$_.Status -eq "Success"} | Out-File -FilePath $SuccessComputers
  74. }
  75. end {
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement