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.
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:
Hat jemand eine Idee?
Gruß, Sascha
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"
}
$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: ( , ParseException
FullyQualifiedErrorId : UnexpectedToken
Wird anstatt der Variable ServiceAction das Command Stop-Service verwendet nimmt der Sauburschi ohne murren alles 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: ( , ParseException
FullyQualifiedErrorId : UnexpectedToken
Hat jemand eine Idee?
Gruß, Sascha
Please also mark the comments that contributed to the solution of the article
Content-Key: 294496
Url: https://administrator.de/contentid/294496
Printed on: April 25, 2024 at 17:04 o'clock
2 Comments
Latest comment
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
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