Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
Powershell IF语句_Powershell_If Statement - Fatal编程技术网

Powershell IF语句

Powershell IF语句,powershell,if-statement,Powershell,If Statement,我正在尝试创建一个用于禁用/启用网络摄像头的快速IF语句 到目前为止,我已经想到了这个: $InstIDDisabled = Get-PnpDevice -FriendlyName "Camname" -Status error $InstIDEnabled = Get-PnpDevice -FriendlyName "Camname" -Status OK If($InstIDDisabled.Status -eq "error"){

我正在尝试创建一个用于禁用/启用网络摄像头的快速IF语句

到目前为止,我已经想到了这个:

$InstIDDisabled = Get-PnpDevice -FriendlyName "Camname" -Status error
$InstIDEnabled = Get-PnpDevice -FriendlyName "Camname" -Status OK
If($InstIDDisabled.Status -eq "error"){
Enable-PnpDevice -InstanceId $InstIDDisabled.InstanceId -Confirm:$false
else
Disable-PnpDevice -InstanceId $InstIDEnabled.InstanceId -Confirm:$false}
我得到的结果是:

Get-PnpDevice : No matching Win32_PnPEntity objects found by CIM query for instances of the ROOT\cimv2\Win32_PnPEntity class on the  CIM server: 
SELECT * FROM Win32_PnPEntity  WHERE ((Name LIKE 'Camname')) AND ((Status = 'error')). Verify query parameters and retry.
At line:1 char:19
+ ... DDisabled = Get-PnpDevice -FriendlyName "Camname" -Status erro ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Win32_PnPEntity:String) [Get-PnpDevice], CimJobException
    + FullyQualifiedErrorId : CmdletizationQuery_NotFound,Get-PnpDevice
该错误看起来似乎无法通过CIM查询查看网络摄像头,但当单独运行(删除IF元素)时,它可以工作


我哪里出错了?

看起来您的错误在第1行,但可能是因为您混淆了此处的“camname”

您所指的问题可能是由于您的
if/else
语句没有正确地用大括号括起来造成的:

还要注意的是,我认为您不需要显式检查
$institdisabled
的属性是否等于“error”,因为您只返回具有该属性的对象,所以您可以只检查变量在if语句中是否有任何内容

$InstIDDisabled = Get-PnpDevice -FriendlyName "Camname" -Status error
$InstIDEnabled = Get-PnpDevice -FriendlyName "Camname" -Status OK
if ($InstIDDisabled) {
    Enable-PnpDevice -InstanceId $InstIDDisabled.InstanceId -Confirm:$false
} 
else {
    Disable-PnpDevice -InstanceId $InstIDEnabled.InstanceId -Confirm:$false
}

您的错误似乎出现在第1行,但可能是因为您混淆了此处的“camname”

您所指的问题可能是由于您的
if/else
语句没有正确地用大括号括起来造成的:

还要注意的是,我认为您不需要显式检查
$institdisabled
的属性是否等于“error”,因为您只返回具有该属性的对象,所以您可以只检查变量在if语句中是否有任何内容

$InstIDDisabled = Get-PnpDevice -FriendlyName "Camname" -Status error
$InstIDEnabled = Get-PnpDevice -FriendlyName "Camname" -Status OK
if ($InstIDDisabled) {
    Enable-PnpDevice -InstanceId $InstIDDisabled.InstanceId -Confirm:$false
} 
else {
    Disable-PnpDevice -InstanceId $InstIDEnabled.InstanceId -Confirm:$false
}

你少了一些牙套。If语句结构为If(缺少一些大括号。If语句结构为If(谢谢你的帮助,安德鲁,成功了!我确实混淆了cam的名称,我对Ifs/Elses很陌生,这帮了大忙。谢谢你的帮助,安德鲁,成功了!我确实混淆了cam的名称,我对Ifs/Elses很陌生,这帮了大忙。