问题描述
if 我无法获得细节错误 用于显示给用户。
If PowerShell Error (User or pwd fail when connect Credential ,Etc..) i cant get detail Error for Show to user.
此我的代码
This My code
公共共享功能执行命令(运行空间为运行空间,命令为字符串)为集合( PSObject)
Public Shared Function ExecuteCommand(runspace As Runspace, command As String) As Collection(Of PSObject)
如果命令为Nothing那么为
抛出新的ArgumentOutOfRangeException(" command")
结束如果
If command Is Nothing Then
Throw New ArgumentOutOfRangeException("command")
End If
如果运行空间为Nothing那么就是
抛出新的ArgumentOutOfRangeException(" runspace")
结束如果
If runspace Is Nothing Then
Throw New ArgumentOutOfRangeException("runspace")
End If
使用ps = System.Management.Automation.PowerShell.Create
Using ps = System.Management.Automation.PowerShell.Create
  ;  Dim cmd As New PSCommand
cmd.AddScript(命令)
ps.Commands = cmd
ps.Runspace = runspace
Dim cmd As New PSCommand
cmd.AddScript(command)
ps.Commands = cmd
ps.Runspace = runspace
返回ps.Invoke(Of PSObject)()
Return ps.Invoke(Of PSObject)()
结束使用
End Using
结束功能
推荐答案
尝试runspace.Open()
Try runspace.Open()
将异常作为异常捕获异常如果TypeOf异常是System.Management.Automation。 Remoting.PSRemotingTransportException然后
Catch exception As Exception If TypeOf exception Is System.Management.Automation.Remoting.PSRemotingTransportException Then
选择Case DirectCast(例外, System.Management.Automation.Remoting.PSRemotingTransportException).ErrorCode
Select Case DirectCast(exception, System.Management.Automation.Remoting.PSRemotingTransportException).ErrorCode
案例5
'处理密码问题
退出选择
案例-2144108526
Case -2144108526
'处理网址问题
退出选择
Case Else
Case Else
'流程其他错误
退出选择
结束选择
结束如果结束尝试
End If End Try
这篇关于如何从PowerShell获取错误& DOTNET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!