检查命令是否成功运行 [英] Check if a command has run successfully

查看:51
本文介绍了检查命令是否成功运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试将以下内容包含在 if 语句中,以便在成功时执行另一个命令:

I've tried enclosing the following in an if statement so I can execute another command if this succeeds:

Get-WmiObject -Class Win32_Share -ComputerName $Server.name -Credential $credentials -Filter "Description='Default share'" | Foreach-Object {
        $Localdrives += $_.Path

但是我不知道该怎么做.我什至尝试创建一个函数,但我也不知道如何检查该函数是否已成功完成.

but I can't figure out how to do it. I even tried creating a function but I couldn't figure out how to check if the function had successfully completed either.

推荐答案

你可以试试:

$res = get-WmiObject -Class Win32_Share -Filter "Description='Default share'"
if ($res -ne $null)
{
  foreach ($drv in $res)
  {
    $Localdrives += $drv.Path
  }
}
else
{
  # your error
}

这篇关于检查命令是否成功运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆