检查服务是否为 运行 或已停止并相应地输出

Check if Service is Running or Stopped and Output accordingly

我刚开始使用 Powershell,想要 运行 一些基本脚本。

我首先编写一个脚本来查看 MSSQLSERVER 是 运行ning 还是已停止并相应地输出消息。

这是我目前所拥有的...

Write-Host "Checking if SQL Server is running.."

function FuncCheckService{
param($MSSQLSERVER)
$arrService = Get-Service -Name $MSSQLSERVER
if ($arrService.Status -eq "Running"){
Write-Host "Service is running" 
}
if ($arrService.Status -eq "Stopped"){ 
Write-Host "service is stopped"
}
}

然而,当我执行这个(在 Powershell ISE 中)时,我得到的只是第一行 "Checking if SQL Server is running..."

我做错了什么?

你的函数很好,但你从来没有调用过它。在脚本的末尾,就在最后一个花括号后面放上你的函数名。