我写了一个脚本来管理服务器上的服务,并根据csv文件中的Task来启动/停止等,并根据csv文件中的启动类型来更改启动类型。
CSV文件:
Server,Service,Startup Type,Task
server1,SQL Server Analysis Services (MSSQLSERVER),automatic,start
server2,"SQL Server Analysis Services (MSSQLSERVER), SQL Server Analysis Services (MSSQLSERVER) CEIP",Manual,stop
脚本
$csvFile = Import-CSV .\SSAS_services.csv
$ServiceState = Get-Service -Name
$ServiceStartupType = Get-Service | select -property name,starttype
ForEach ($row in $csvFile)
{
#checking if service in csv file exists on server
if (Get-Service $row.Service | Select-Object Name, StartType, Status -ComputerName $row.Server -ErrorAction SilentlyContinue)
{
"$row.Service not found on $row.Server!" | out-file .\SSAS_services.txt -Append
}
else
{
Get-Service $row.Service | Select-Object Name, StartType, Status -ComputerName $row.Server | select machinename,name | format-table -AutoSize
}
# Change the service on the server
if ($row.Task -eq "stop" -and $row.Server $ServiceState $row.Service -ne "stop")
{
Invoke-Command -ScriptBlock { Stop-Service $using:row.Service } -ComputerName $row.Server -ArgumentList $row
Start-Sleep 60
}
elseif ($row.Task -eq "start" -and $row.Server $ServiceState $row.Service -ne "start")
{
Invoke-Command -ScriptBlock { Start-Service $using:row.Service } -ComputerName $row.Server -ArgumentList $row
Start-Sleep 60
}
elseif ($row.Task -eq "pause" -and $row.Server $ServiceState $row.Service -ne "pause")
{
Invoke-Command -ScriptBlock { Suspend-Service $using:row.Service } -ComputerName $row.Server -ArgumentList $row
Start-Sleep 60
}
elseif ($row.Task -eq "Restart")
{
Invoke-Command -ScriptBlock { Restart-Service $using:row.Service } -ComputerName $row.Server -ArgumentList $row
Start-Sleep 60
}
#changing startup type if different
if ($row."Startup Type" -eq $ServiceStartupType -ComputerName $row.Server)
{
"Changing Startup Type from '$ServiceStartupType' to $row.'Startup Type'"
Invoke-Command -ScriptBlock { Set-Service $using:row.Service -StartupType $using:row."Startup Type" } -ComputerName $row.Server -ArgumentList $row
}
} | Tee-object .\SSAS_services.txt -Append
我遇到以下错误:
表达式或语句中的意外标记'$ ServiceState'。 + if($ row.Task -eq“ stop”-和$ row.Server $ ServiceState $ row.Serv ...
在'if'语句中的表达式后,缺少结尾')'。 + ... if($ row.Task -eq“ stop”-和$ row.Server $ ServiceState $ row.Service ...
在语句块或类型定义中缺少结束符'}'。 + ... sk -eq“ stop”-和$ row.Server $ ServiceState $ row.Service -ne“ stop”) +
表达式或语句中的意外标记')'。 + ... elseif($ row.Task -eq“ start”-和$ row.Server $ ServiceState $ row.Se ...
〜表达式或语句中的意外标记'$ ServiceState'。 + elseif($ row.Task -eq“ start”-和$ row.Server $ ServiceState $ row ... +
表达式或语句中的意外标记'$ row'。 + ... -eq“ start”-和$ row.Server $ ServiceState $ row.Service -ne“ start”) +表达式或语句中出现意外的标记')'。 + ... elseif($ row.Task -eq“ pause”-和$ row.Server $ ServiceState $ row.Se ... +
~~~~~~~~~~~~~~表达式或语句中的意外标记'$ ServiceState'。并非所有解析错误都已报告。正确 报告的错误,然后重试。 + CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException + FullyQualifiedErrorId:UnexpectedToken〜
答案 0 :(得分:0)
带有-and $row.Server $ServiceState $row.Service -ne "stop"
的if语句不是有效的Powershell语法(该表达式的计算结果应为布尔值)。您已编写函数或使用invoke-expression
评估服务状态。
例如
$serviceName = "WSearch"
$command = "get-service $serviceName"
if ((invoke-expression $command).Status -eq "Running") {
# do something...
}