我正在尝试以Set-DistributionGroup下列方式使用Exchange cmdlet:
$Exch_Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $ExchangeURI -Authentication Kerberos
Import-PSSession $Exch_Session -commandname Set-DistributionGroup -AllowClobber
if (<condition>) {
try {
Set-DistributionGroup @Setparams
}
catch {
<error capture code here...>
}
}
Run Code Online (Sandbox Code Playgroud)
在成功和失败时,一切都按预期工作......但是在测试运行期间,在尝试将PrimarySMTPAddress更改为无效值时,我得到一个警告(而不是错误),即新电子邮件地址不符合e-邮件地址政策,所以不会改变.但由于这是一个警告,try/ catch不会触发,整个过程错误地完成成功.
我试过了:
-WarningAction Stop到命令并确实触发try/catch,但错误太通用了:
命令执行已停止,因为首选项变量"WarningPreference"或common参数设置为Stop.
所以我试图捕获变量和文件的警告(以便在报告成功或失败时检查以后),但我尝试捕获的所有方法都失败了,即使它不断向屏幕显示警告.
Set-Distributiongroup @Setparams -WarningVariable cmd_warn
Set-Distributiongroup @Setparams 3> c:\temp\warnings.txt
...
$command = "Set-Distributiongroup @Setparams"
iex $command 3> c:\temp\warnings.txt
Run Code Online (Sandbox Code Playgroud)
但是文件和变量总是空的,我做错了什么或错过了什么?
给定以下匿名哈希数组:
$fields_dump = [
{
'DEFAULT_FIELD_LABEL' => 'telco_hier_building',
'APPROVER_ORDER' => '0',
'IS_READONLY' => '0',
'FORM_ID' => '3913',
'REFERENCE' => 'building',
},
{
'DEFAULT_FIELD_LABEL' => 'Request Type',
'APPROVER_ORDER' => '0',
'IS_READONLY' => '0',
'FORM_ID' => '3913',
'REFERENCE' => 'request_type',
},
{
'DEFAULT_FIELD_LABEL' => 'Request Completion Date',
'APPROVER_ORDER' => '0',
'IS_READONLY' => '0',
'FORM_ID' => '3913',
'REFERENCE' => 'request_completion_date',
},
Run Code Online (Sandbox Code Playgroud)
我想有条件地grep数组DEFAULT_FIELD_LABEL的值为telco_hier_building或者telco_imac_building.当它成功时,我需要提取到匹配值的第二个下划线.
我使用以下代码:
if ( grep { $_->{DEFAULT_FIELD_LABEL} =~ m/(telco_hier_|telco_imac_)building/ } @$fields_dump ) {
my $telco_prefix = …Run Code Online (Sandbox Code Playgroud)