saschard
Goto Top

Powershell: Dienst - Starten, Stoppen und Status Abfragen

Hallo zusammen,

habe das Starten, Stoppen und die Abfrage des Status (inkl. Error-Handling) eines Dienstes zusammengelegt, jedoch hängt er sich jetzt an den Parametern zum Abfragen der ErrorVariable $ServiceAction $WhichService -ErrorVariable getServiceError -ErrorAction SilentlyContinue auf.
function StartAndStopServerServices($ServiceAction, $WhichService, $ServiceOutput){
	$ServiceCheck = Get-Service $WhichService -ErrorVariable getServiceError -ErrorAction SilentlyContinue
		IF ($getServiceError -and ($getServiceError | foreach {$_.FullyQualifiedErrorId -like "*NoServiceFoundForGivenName*"})){  
			Output -Output "Exception Message: There is no service named $WhichService"  
		} ELSE {
			Output -Output "$WhichService is now $($ServiceCheck.status)"  
		        $ServiceAction $WhichService -ErrorVariable getServiceError -ErrorAction SilentlyContinue
			IF ($getServiceError -and ($getServiceError | foreach {$_.FullyQualifiedErrorId -like "*NoServiceFoundForGivenName*"})){  
				Output -Output "Failed to $ServiceOutput the service named $WhichService"  
			}
			Sleep -Seconds 10
			$ServiceAfter = Get-Service $WhichService
			Output -Output "$WhichService is now $($ServiceAfter.status)"  
		}
}
function StartServer {
	StartServerService -ServiceAction "Start-Service" -WhichService "XXX" -ServiceOutput "start"  
}
function StartWebServer {
	StartServerService -ServiceAction "Start-Service" -WhichService "XYZ" -ServiceOutput "start"  
}
function StopServer {
	StartServerService -ServiceAction "Stop-Service" -WhichService "XXX" -ServiceOutput "stop"  
}
function StopWebServer {
	StartServerService -ServiceAction "Stop-Service" -WhichService "XYZ" -ServiceOutput "stop"  
}
Beim Ausführen des Skripts ist zu dem Zeitpunkt, wo er an die Stelle kommt (siehe oben rot markiert) ihm das Command noch nicht bekannt. Was dann auch zu der Fehlermeldung führt, dass die Ausgabe liefert:
$ServiceAction $WhichService -ErrorVariable getServiceErr ...
Unexpected token '$WhichService' in expression or statement.
$ServiceAction $WhichService -ErrorVariable getServiceErr ...
Unexpected token '-ErrorVariable' in expression or statement.
$ServiceAction $WhichService -ErrorVariable getServiceError -ErrorA ...
Unexpected token 'getServiceError' in expression or statement.
CategoryInfo : ParserError: (face-smile , ParseException
FullyQualifiedErrorId : UnexpectedToken
Wird anstatt der Variable ServiceAction das Command Stop-Service verwendet nimmt der Sauburschi ohne murren alles face-sad

Hat jemand eine Idee?

Gruß, Sascha

Content-Key: 294496

Url: https://administrator.de/contentid/294496

Printed on: April 25, 2024 at 17:04 o'clock

Mitglied: 122990
Solution 122990 Jan 28, 2016 updated at 14:34:13 (UTC)
Goto Top
Moin,
du übergibst einen String, aber ein String ist kein Befehl oder CMDLet!
Um aus einem String einen Befehl zu machen benutzt man Invoke-Expression.

Gruß grexit
Member: SaschaRD
SaschaRD Jan 28, 2016 updated at 15:15:50 (UTC)
Goto Top
Hallo grexit,

nicht nachgedacht face-sad
Klar ohne Invoke-Expression wird, wie Du bereits geschrieben hast, das Ganze als String gelesen und nicht als Command.

Danke und Gruß, Sascha

Lösung:
Invoke-Command -ScriptBlock {& $ServiceAction $WhichService -ErrorVariable getServiceError -ErrorAction SilentlyContinue}