我正在尝试将异常消息转换为字符串,我可以throw
也可以写到事件管理器,但是有严重的问题。
尝试了How can I get powershell exception descriptions into a string?,但是这里没有任何作用。
这就是我用来验证网络上凭据的方法:
$user = 'user'
$pass = 'pass'
$domain = "ldap://" + ( [ADSI]"" ).DistinguishedName
$creds = New-Object System.Management.Automation.PSCredential("$($env:USERDOMAIN)\$user",$(ConvertTo-SecureString $pass -AsPlainText -Force))
New-Object System.DirectoryServices.DirectoryEntry ($domain,$user,$creds.GetNetworkCredential().Password)
如果凭据有效,您将得到类似的回信:
distinguishedName : {DC=COMP}
Path : LDAP://COMP
如果有错误,它将类似于:
format-default : The following exception occurred while retrieving member "distinguishedName": "Unknown error (0x80005000)" + CategoryInfo : NotSpecified: (:) [format-default],ExtendedTypeSystemException + FullyQualifiedErrorId : CatchFromBaseGetMember,microsoft.PowerShell.Commands.FormatDefaultCommand
关于我只能用该输出执行的唯一操作是放下format-default
。
关于如何获得最高收入的任何建议?
到目前为止,我已经尝试过单独或组合使用$PSItem
,$_.Exceptionmessage
,.ToString()
,| Out-String
,[string]
。