if我无法获得细节错误用于显示给用户。
IfPowerShell 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
结束功能
推荐答案
使用这段代码和现在checjk。我希望它对您有用。
Hi, Use this code and checjk it now. I hope it will works for you.
尝试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
发布评论