I wrote a script that manages services on the server and starts/stops etc...depending on the Task in the csv file, as well as changes the startuptype, again depending on the start up type in the csv file.
CSV file:
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
Script
$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
I am getting the following errors:
Unexpected token '$ServiceState' in expression or statement. + if ($row.Task -eq "stop" -and $row.Server $ServiceState $row.Serv ...
Missing closing ')' after expression in 'if' statement. + ... if ($row.Task -eq "stop" -and $row.Server $ServiceState $row.Service ...
Missing closing '}' in statement block or type definition. + ... sk -eq "stop" -and $row.Server $ServiceState $row.Service -ne "stop") +
Unexpected token ')' in expression or statement. + ... elseif ($row.Task -eq "start" -and $row.Server $ServiceState $row.Se ...
~Unexpected token '$ServiceState' in expression or statement. + elseif ($row.Task -eq "start" -and $row.Server $ServiceState $row ... +
Unexpected token '$row' in expression or statement. + ... -eq "start" -and $row.Server $ServiceState $row.Service -ne "start") + Unexpected token ')' in expression or statement. + ... elseif ($row.Task -eq "pause" -and $row.Server $ServiceState $row.Se ... +
~~~~~ Unexpected token '$ServiceState' in expression or statement. Not all parse errors were reported. Correct the reported errors and try again. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken ~