2

私はposhを学んでいます。そして、なぜこのスクリプトが警告を受けないのか理解しようとしています。私は-WarningActions Stop-ErrorAction Stopが、何もありませんがtryiedましPowershell試してみる/キャッチ - 取得する

get-user : The operation couldn't be performed because object 'aaaa' couldn't be found on 
'iDC01.contoso.com'. At C:\Users\Gra***\Desktop\test.ps1:2 char:5 
+  get-user aaaa -WarningAction Stop 
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [Get-User], ManagementObjectNotFoundException 
    + FullyQualifiedErrorId : [Server=ME1,RequestId=ebcde0d2-9222-443b-b25a-ef7279fd168e, 
     TimeStamp=20.06.2017 13:51:35] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] 
     FE0D594D,Microsoft.Exchange.Management.RecipientTasks.GetUser 

:ここ

try{ 
    get-user aaaa -WarningAction Stop 
} 
catch 
{ 
    Write-Host "hi" 
} 

はエラーです。

一般に私はtry/catchの基本を理解しており、次のスクリプトは正常に動作します。

try{ 
Get-Process -name xyz -ErrorAction Stop 
} 
catch{ 
Write-Host "oops" 
} 

私はpowershell_ise 5.1を使用しています。 get-userの何が問題なのか分かりますか? また、Get-ADuserは使用できません。

+0

'取得-user'は、ネイティブのPowerShellコマンドではありません。この関数のコードはどのように見えますか? – gms0ulman

+0

'Get-User'はExchangeコマンドレットとして文書化されており、Exchangeからのオブジェクトのみを返します。 –

+0

WarningActionは設定しますが、ErrorActionは設定しません。これを実行したときにあなたのErrorActionPreferenceはおそらく続きましたか? –

答えて

1

ファンクションを呼び出すときにキャッチできる2つのエラー、警告があります。スクリプト内では$WarningPreferenceまたは$ErrorActionPreferenceをグローバルに設定するか、-eaまたは-wa引数を使用して個別に設定できます。あなたの例では、私は確かに、次を使用したい:

Try { 
    Get-User aaaa -wa Stop -ea Stop 
} Catch { 
    Write-Output "hi" 
    Write "[$($_.Exception.GetType().FullName)] - $($_.Exception.Message)" 
} 

about_Preference_Variables

+0

@ TheIncorrigible1とPaal Braathenありがとうございました。しかし、私は - ErrAction Stopを試みましたが、それは助けになりませんでした。そして私は、New-PSSession経由でRemote-PowerShellを使用しているため、これが発生することを発見しました。それはサーバー上で正常に動作します。 – apatic

関連する問題