pixel0815
Goto Top

Dienst Stoppen und deaktivieren per Powershell und WMI auf remote Server

Moin,

gibt es da eine einfache möglichkeit einen gewissen Dienst zu deaktivieren und vorher zu stoppen?
Die Computernamen sollen aus dem AD ausgelesen werden anhand des Kürzels SRV*.

Gruß
Pixel

Content-ID: 309191

Url: https://administrator.de/forum/dienst-stoppen-und-deaktivieren-per-powershell-und-wmi-auf-remote-server-309191.html

Ausgedruckt am: 22.12.2024 um 12:12 Uhr

pixel0815
pixel0815 07.07.2016 um 12:54:19 Uhr
Goto Top
OK ist erledigt. Wen es interessiert.
Ist ganz nett. Mit der Änderung der zwei Variablen ist das genau das was ich will.

http://techibee.com/powershell/disable-list-of-services-on-remote-compu ...

$computername = Get-ADComputer -Filter "Name -like 'adm*'" -Properties * | Where-Object OperatingSystem -Like '*Server*' | Select-Object -expand Name          
$ServiceName = "Wsearch"  
            
foreach($Computer in $ComputerName) {            
 Write-Host "Working on $Computer"              
 if(!(Test-Connection -ComputerName $Computer -Count 1 -quiet)) {            
  Write-Warning "$computer : Offline"              
  Continue            
 }            
             
 foreach($service in $ServiceName) {            
  try {            
   $ServiceObject = Get-WMIObject -Class Win32_Service -ComputerName $Computer -Filter "Name='$service'" -EA Stop              
   if(!$ServiceObject) {            
    Write-Warning "$Computer : No service found with the name $service"              
    Continue            
   }            
   if($ServiceObject.StartMode -eq "Disabled") {              
    Write-Warning "$Computer : Service with the name $service already in disabled state"              
    Continue            
   }            
               
   Set-Service -ComputerName $Computer -Name $service -EA Stop -StartMode Disabled            
   Write-Host "$Computer : Successfully disabled the service $service. Trying to stop it"              
   if($ServiceObject.Status -eq "Running") {              
    Write-Warning "$Computer : $service already in stopped state"              
    Continue            
   }            
   $retval = $ServiceObject.StopService()            
            
   if($retval.ReturnValue -ne 0) {            
    Write-Warning "$Computer : Failed to stop service. Return value is $($retval.ReturnValue)"              
    Continue            
   }            
               
   Write-Host "$Computer : Stopped service successfully"              
               
  } catch {            
   Write-Warning "$computer : Failed to query $service. Details : $_"              
   Continue            
  }            
             
 }            
            
}