1

On a Windows Server 2008 R2, 64 bit-machine I am running the following code:

$global:arrServer = @("ph1", "ph2", "ph3")

$global:arrDienste = @("W3SVC", "WAS", "IISADMIN")

$global:strPfad = "D:\WASLogs\"
$global:strLogTime = Get-Date -Format "yyyy-MM-dd--hh-mm-ss"    
$global:strLogDatei = $global:strPfad + "WARTUNG--" + $global:strLogTime + ".log"     

Log_Abfrage_und_Generierung

Dienste_Stop

Function Dienste_Stop
{
  echo "Stop of the services successful?"  | Out-File $global:strLogDatei -Append -Force

  foreach($strServer in $global:arrServer)
  {
    $strInterim2 = $strServer + " (" + $global:appServerNamen + ")"
    echo  "       " $strInterim2 | Out-File $global:strLogDatei -Append -Force

    foreach($strDienst in $global:arrDienste)
    {
        $objWmiService = Get-Wmiobject -Class "win32_service" -computer $strServer -filter "name = '$strDienst'"

        if( $objWmiService.State )            
        {
          $rtnWert = $objWmiService.stopService()
          Switch ($rtnWert.returnvalue)
            {
               0 { echo "$strDienst stopped!" | Out-File $global:strLogDatei -Append -Force }
               2 { echo "$strDienst throws: 'Access denied!'" | Out-File $global:strLogDatei -Append -Force }
               3 { echo "Service $strDienst is not existing on $strServer!" | Out-File $global:strLogDatei -Append -Force }
               5 { echo "$strDienst already stopped!" | Out-File $global:strLogDatei -Append -Force }
               DEFAULT { echo "$strDienst service reports ERROR $($rtnWert.returnValue)" | Out-File $global:strLogDatei -Append -Force }
            }
        }
        else
        {
            echo "Service $strDienst is not existing on $strServer!" | Out-File $global:strLogDatei -Append -Force 
        }
    }
 }
}

Function Log_Abfrage_und_Generierung
{
    if([IO.Directory]::Exists($global:strPfad))
{
    echo "Nothing happening here."
}

else
{
    New-Item -ItemType directory -path $global:strPfad
}
}

This can be reproduced on all computers ph1, ph2 and ph3. However with some other code, WAS can be started, respectively the status can be seen.

Also to note:

  1. All other services can be stopped? Does it has to do with the fact that the path for the WAS is like this? C:\Windows\system32\svchost.exe -k iissvcs
  2. I use WMI on purpose.

What is going on here?

Tia

Joey
  • 511
  • 6
  • 20

1 Answers1

0

The problem could be that there are multiple services that depend on WAS which need to be stopped first. The StopService() method does not have an overload to stop dependent services. If this doesn't solve the issue check the response code from StopService to determine the problem in the link above.

It looks like you are handling the code 3 as 'service does not exist'. The docs show this code actually means 'The service cannot be stopped because other services that are running are dependent on it.'

Not sure why you're determined to use WMI when this capability is fully baked into powershell

Stop-Service WAS -Force

WAS Service Properties

Avner
  • 4,286
  • 2
  • 35
  • 42