我正在尝试使用一个函数来确认或否认我们是否顺利通过了cmdlet --我正在运行一组AD/Exchange cmdlet,并在最后将结果存储/输出到.csv。我忘了导入Exchange模块,这对我有好处,因为当我使用Get-DistributionList时,它以一种我没有预料到的方式终止。
我试过使用$吗?代替Try..Catch,强制EA停止,并首先将参数存储在变量中,但由于模块未安装,并且cmdlet无法识别,因此它只是停止程序。
这就是我想要做的:
function Test-Success ($cmdlet){
try{
$cmdlet
"Y"
} catch {
"Err -- Perform manually."
}
}
Test-Success(Get-DistributionList)
但我得到以下错误,脚本停止:
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,因为它似乎是这样工作的(示例:)
try {
Get-DistributionList
"y"
} catch {
"Err -- Perform manually"
}
我希望有一个变通的方法,这样如果cmdlet以这种方式失败,它就不会终止程序,但我对PowerShell不是很熟悉,我自己的搜索也没有结果。
https://stackoverflow.com/questions/56568788
复制相似问题