首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有Try...Catch的Powershell函数未包含在该函数中,因为该参数会导致错误

带有Try...Catch的Powershell函数未包含在该函数中,因为该参数会导致错误
EN

Stack Overflow用户
提问于 2019-06-13 03:00:58
回答 2查看 51关注 0票数 0

我正在尝试使用一个函数来确认或否认我们是否顺利通过了cmdlet --我正在运行一组AD/Exchange cmdlet,并在最后将结果存储/输出到.csv。我忘了导入Exchange模块,这对我有好处,因为当我使用Get-DistributionList时,它以一种我没有预料到的方式终止。

我试过使用$吗?代替Try..Catch,强制EA停止,并首先将参数存储在变量中,但由于模块未安装,并且cmdlet无法识别,因此它只是停止程序。

这就是我想要做的:

代码语言:javascript
复制
function Test-Success ($cmdlet){
    try{
          $cmdlet
          "Y"
    } catch {
          "Err -- Perform manually."
    }
}

Test-Success(Get-DistributionList)

但我得到以下错误,脚本停止:

代码语言:javascript
复制
Get-DistributionList : The term 'Get-DistributionList' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At line:46 char:10
+ Test-Success(Get-DistributionList)
+          ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-DistributionList:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

最坏的情况是,我可以每次都放入try...catch,因为它似乎是这样工作的(示例:)

代码语言:javascript
复制
try {
     Get-DistributionList
     "y"
} catch {
     "Err -- Perform manually"
}

我希望有一个变通的方法,这样如果cmdlet以这种方式失败,它就不会终止程序,但我对PowerShell不是很熟悉,我自己的搜索也没有结果。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56568788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档