Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么if语句不是';在Powershell脚本中不工作?_Powershell_Windows Server_Azure Runbook - Fatal编程技术网

为什么if语句不是';在Powershell脚本中不工作?

为什么if语句不是';在Powershell脚本中不工作?,powershell,windows-server,azure-runbook,Powershell,Windows Server,Azure Runbook,我有一个运行Windows Server 2008 R2的虚拟机,安装了Zabbix代理。 我想运行一个特定的代码块,如果它正在运行。 我手动启动了服务。 当我登录到VM时,打开PowerShell提示符并键入以下代码段,它可以完美地工作: $ZABBIX_INSTALLED = Get-Service "Zabbix Agent" If ($ZABBIX_INSTALLED) { Write-Host "It's running" } else {

我有一个运行Windows Server 2008 R2的虚拟机,安装了Zabbix代理。 我想运行一个特定的代码块,如果它正在运行。 我手动启动了服务。 当我登录到VM时,打开PowerShell提示符并键入以下代码段,它可以完美地工作:

$ZABBIX_INSTALLED = Get-Service "Zabbix Agent"
If ($ZABBIX_INSTALLED) {
   Write-Host "It's running"
}
else {
   Write-Host "Zabbix Agent is not running"
}
问题是,当我尝试将其作为脚本调用时,即使进程正常运行,它也不会返回true(我在任务管理器中检查了它)

有关于代码发生了什么的建议吗?谢谢你的帮助

PS:我通过Azure Runbook通过运行
Invoke-AzRunCommand
并将脚本作为参数传递来传递此脚本。

Bruno

此代码将涵盖所有3种可能的情况(未安装、未运行、正在运行)


HTH

这里有一个很好的使用
if/elseif/else
语句/条件:


$serv = Get-Service -Name "Zabbix Agent" -ErrorAction SilentlyContinue
    if($serv.Status -eq "Stopped")
        { Write-Host -Object "Service is Stopped!" -ForegroundColor Yellow } 
         elseif($serv.Status -eq "Running")
            { Write-Host -Object "Service is running!" -ForegroundColor Green }
            else { Write-Host -Object "No service by that name found!" -ForegroundColor Red }


如果服务的状态为“已停止”,则表示其已停止。如果它正在运行,就说它正在运行,如果没有该名称的服务,就说它。

您也可以在swith语句中对此进行说明:

try
{
    $srvName = "Zabbix Agent"
    switch  ( ( Get-Service $srvName -ea Stop).Status )
    {
        "Stopped" { "Zabbix Agent: Is not running" }
        "Running"{ "Zabbix Agent: Is running" }
    }
}
catch [Microsoft.PowerShell.Commands.ServiceCommandException]
{
    "Zabbix Agent: Not installed"
}
catch
{
    $_.Exception.Message
}

伙计们,我设法解决了这个问题

想法是在安装Zabbix代理之后,调用一个PS脚本(基于我在问题中编写的代码片段)来更新config目录中的配置文件

因此,当我调用更新脚本时,
Get Service
找不到该服务,因为某些原因它还不可用


然后我在调用更新脚本之前添加了一个
Start Sleep 5
,它成功了。

您显示的代码没有返回任何内容;如果要输出指示服务是否正在运行的布尔值,请使用
[bool]$ZABBIX_INSTALLED
@mklement0,如果有对象或值,
if($ZABBIX_INSTALLED)
是否返回true?我一直这样使用
if($variable)
,它不会返回true。它可能被评估为真实情况。如果您想返回true,那么执行
If($variable){$true}else{$false}
我觉得布尔求值不是这里的最佳选择。如果服务存在,即使它没有运行,现有的if语句也会激发。它可能更舒服,比如:
If($ZABBIX_INSTALLED.Status-eq'Running'){Write Host“It's Running”}
我认为OP意味着永远不会执行
Write Host“It's Running”
。听起来好像没有找到以“Zabbix代理”为名称的服务。您好@RetiredGeek,谢谢您的回答。我试过了,但结果是一样的。当我通过azure runbook运行代码时,结果是catch消息,但当我直接在VM中运行相同的代码段时(在PS提示符中),结果是Zabbix代理正在运行。似乎您正在尝试查询显示名称,因此我将
-name
参数更改为
-DisplayName
try
{
    $srvName = "Zabbix Agent"
    switch  ( ( Get-Service $srvName -ea Stop).Status )
    {
        "Stopped" { "Zabbix Agent: Is not running" }
        "Running"{ "Zabbix Agent: Is running" }
    }
}
catch [Microsoft.PowerShell.Commands.ServiceCommandException]
{
    "Zabbix Agent: Not installed"
}
catch
{
    $_.Exception.Message
}