first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

PowerShell 1.x script: Toggling Servers on and off in Windows Server 2003

Function StopService { $s = Get-WmiObject -Class Win32_Service -Filter $args[0] if ( $s ) { Write-Host "Service"$s.Name"found." if ( $s.State -eq "Running" ) { Write-Host "Service is running. Stopping service…n" StopServiceReturnValue $s.StopService().ReturnValue Write-Host "Waiting 5 seconds…n" Sleep -seconds 5 } } else { Write-Host "Service not found.`n" } }

Function StopServiceReturnValue { $v = $args[0]

$msg = "Unknown return value:$v"

switch($v)
{
    0 	{ $msg = "Success.`n" }
    1 	{ $msg = "Not Supported.`n" }
    2 	{ $msg = "Access Denied.`n" }
    3 	{ $msg = "Dependent Services Running.`n" }
    4 	{ $msg = "Invalid Service Control.`n" }
    5 	{ $msg = "Service Cannot Accept Control.`n" }
    6 	{ $msg = "Service Not Active.`n" }
    7 	{ $msg = "Service Request timeout.`n" }
    8 	{ $msg = "Unknown Failure.`n" }
    9 	{ $msg = "Path Not Found.`n" }
    10 	{ $msg = "Service Already Stopped.`n" }
    11 	{ $msg = "Service Database Locked.`n" }
    12 	{ $msg = "Service Dependency Deleted.`n" }
    13 	{ $msg = "Service Dependency Failure.`n" }
    14 	{ $msg = "Service Disabled.`n" }
    15 	{ $msg = "Service Logon Failed.`n" }
    16 	{ $msg = "Service Marked For Deletion.`n" }
    17 	{ $msg = "Service No Thread`n" }
    18 	{ $msg = "Status: Circular Dependency`n" }
    19 	{ $msg = "Status: Duplicate Name`n" }
    20 	{ $msg = "Status: Invalid Name`n" }
    21 	{ $msg = "Status: Invalid Parameter`n" }
    22 	{ $msg = "Status: Invalid Service Account`n" }
    23 	{ $msg = "Status: Service Exists`n" }
    24 	{ $msg = "Service Already Paused.`n" }
}

Write-Host $msg

}

Function ToggleService {

$s = Get-WmiObject -Class Win32_Service -Filter $args[0]

if ( $s )
{
    Write-Host "Service"$s.Name"found."
    if ( $s.State -eq "Stopped" )
    {
        Write-Host "Service is stopped. Starting service…`n"
        $s.StartService() | Out-Null
    }
    else
    {
        Write-Host "Service is not stopped. Stopping service…`n"
        StopServiceReturnValue $s.StopService().ReturnValue

        if ( $s.Name -eq "W3SVC" )
        {
            Write-Host "Waiting 5 seconds…`n"
            Sleep -seconds 5

            Write-Host "Looking for dependencies for"$s.Name"…`n"

            Write-Host "Looking for HTTP SSL Service…"
            StopService 'Name = "HTTPFilter"'

            Write-Host "Looking for IIS Admin Service…"
            StopService 'Name = "IISADMIN"'
        }
    }
}
else
{
    Write-Host "Service not found.`n"
}

}

Clear-Host

Write-Host "Looking for WWW Publishing Service…" ToggleService 'Name = "W3SVC"'

"Looking for default instance of SQL Server 2000…" ToggleService 'Name = "MSSQLSERVER"'

"Looking for conventional instance of SQL Server 2005 Express…" ToggleService 'Name = "MSSQL$MYDB"'

mod date: 2007-08-29T19:17:43.000Z