try { # set the preference to STOP $old = $ErrorActionPreference $ErrorActionPreference = 'Stop' # RUN THE CONSOLE EXE THAT MIGHT EMIT AN ERROR, # and redirect the error channel #2 to the # output channel #1 net user doesnotexist 2>&1 }
catch [System.Management.Automation.RemoteException] { # catch the error emitted by the EXE, # and do what you want $errmsg = $_.Exception.Message Write-Warning$errmsg }
finally { # reset the erroractionpreference to what it was before $ErrorActionPreference = $old }
一个简单得多的办法是使用自定义作用域:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
& { try { # set the preference to STOP $ErrorActionPreference = 'Stop' # RUN THE CONSOLE EXE THAT MIGHT EMIT AN ERROR, # and redirect the error channel #2 to the # output channel #1 net user doesnotexist 2>&1 }
catch [System.Management.Automation.RemoteException] { # catch the error emitted by the EXE, # and do what you want: $errmsg = $_.Exception.Message Write-Warning$errmsg } }
try { # set the preference to STOP $old = $ErrorActionPreference $ErrorActionPreference = 'Stop' # RUN THE CONSOLE EXE THAT MIGHT EMIT AN ERROR, # and redirect the error channel #2 to the # output channel #1 net user doesnotexist 2>&1 }
catch [System.Management.Automation.RemoteException] { # catch the error emitted by the EXE, # and do what you want $errmsg = $_.Exception.Message Write-Warning$errmsg }
finally { # reset the erroractionpreference to what it was before $ErrorActionPreference = $old }
functionDo-Something { # function uses internal error handling try { Get-Process-Name NotThereOhWell -ErrorAction Stop } # catch this error type catch [Microsoft.PowerShell.Commands.ProcessCommandException] { $oldE = $_.Exception
# handle the error, OR SHOWN HERE: issue a new exception to the caller $newE = New-Object-TypeName System.InvalidOperationException('Do-Something: A fatal error occured', $oldE) Throw$newE } }
# function will encounter an internal error # error message shows error message generated by function instead Do-Something
Message Originalmessage ------- --------------- Do-Something: A fatal error occured Cannot find aprocesswiththe name "NotThereOhWell". Verify theprocess name and call the cmdlet again.
Name FriendlyName ApplicableOS ---------------------------- 5.1.14393.0 Windows PowerShell 5.1 Preview Windows 10 Anniversar... 5.1.14300.1000 Windows PowerShell 5.1 Preview Windows Server 2016 T... 5.0.10586.494 Windows PowerShell 5 RTM Windows 101511 + KB3... 5.0.10586.122 Windows PowerShell 5 RTM Windows 101511 + KB3... 5.0.10586.117 Windows PowerShell 5 RTM 1602 Windows Server 2012R... 5.0.10586.63 Windows PowerShell 5 RTM Windows 101511 + KB3... 5.0.10586.51 Windows PowerShell 5 RTM 1512 Windows Server 2012R... 5.0.10514.6 Windows PowerShell 5 Production Preview 1508 Windows Server 2012 R2 5.0.10018.0 Windows PowerShell 5 Preview 1502 Windows Server 2012 R2 5.0.9883.0 Windows PowerShell 5 Preview November 2014 Windows Server 2012R... 4.0 Windows PowerShell 4 RTM Windows Server 2012R... 3.0 Windows PowerShell 3 RTM Windows Server 2012, ... 2.0 Windows PowerShell 2 RTM Windows Server 2008R... 1.0 Windows PowerShell 1 RTM Windows Server 2008, ...
这是在您的企业中以友好的 PowerShell 版本名称获得 PowerShell 版本号的方法:
1 2 3 4 5
PS C:\> Get-PSVersion
PSComputerName PSVersion PSVersionFriendlyName -------------------------------------------- CLIENT12 5.1.14393.0 Windows PowerShell 5.1 Preview
Name Category Module Synopsis -------------------------- about_Aliases HelpFile SHORT DESCRIPTION about_Arithmetic_Operators HelpFile SHORT DESCRIPTION about_Arrays HelpFile SHORT DESCRIPTION about_Assignment_Operators HelpFile SHORT DESCRIPTION about_Automatic_Variables HelpFile SHORT DESCRIPTION about_Break HelpFile SHORT DESCRIPTION about_Classes HelpFile SHORT DESCRIPTION about_Command_Precedence HelpFile SHORT DESCRIPTION about_Command_Syntax HelpFile SHORT DESCRIPTION about_Comment_Based_Help HelpFile SHORT DESCRIPTION about_CommonParameters HelpFile SHORT DESCRIPTION about_Comparison_Operators HelpFile SHORT DESCRIPTION about_Continue HelpFile SHORT DESCRIPTION about_Core_Commands HelpFile SHORT DESCRIPTION about_Data_Sections HelpFile SHORT DESCRIPTION about_Debuggers HelpFile SHORT DESCRIPTION about_DesiredStateConfiguration HelpFile SHORT DESCRIPTION
# find all text files inside the PowerShell folder that start # with "about" Get-ChildItem-Path$pshome-Filter about*.txt -Recurse | # identify those that do not end with ".help.txt" Where-Object { $_.Name -notlike'*.help.txt' } | # rename the extension using a regex: Rename-Item-NewName { $_.Name -replace'\.txt$', '.help.txt'}