Powershell 将catch块发送到$error变量
我有一个脚本,可以在启动时测试一些事情,例如,如果某些NetworkDrive仍然需要映射。然后,如果需要,它将映射它 有时会发生这样的错误Powershell 将catch块发送到$error变量,powershell,Powershell,我有一个脚本,可以在启动时测试一些事情,例如,如果某些NetworkDrive仍然需要映射。然后,如果需要,它将映射它 有时会发生这样的错误 In \\server\powershell$\Powershell-Scripts\_DVLP\LogonScript.ps1:57 Zeichen:9 + $Map.MapNetworkDrive($_.letter,$_.path,$false) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In \\server\powershell$\Powershell-Scripts\_DVLP\LogonScript.ps1:57
Zeichen:9
+ $Map.MapNetworkDrive($_.letter,$_.path,$false)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
我通过将$error
变量导出到文件中生成的文件将该错误作为电子邮件附件
$error |out-file C:\myfile.txt -force
正如您所看到的,这个错误不是很有帮助,因为我没有看到驱动器号。因此,我想将.mapnetworkdrive包装到Try/Catch块中
Try {...}
Catch {
$failedletter= $_.letter
$failedpath = $_.path
$export = $failedletter + $failedpath + $_.exception.message #send this to $error variable
}
我现在的问题是,是否可以将整个Catch块(或部分Catch块)发送到$error
变量,这样就不必使日志记录变得更复杂。是否有任何方法将内容发送到错误变量
我可以简单地执行
$error+=$export
?在$error变量中是否有两次?如果在ErrorRecord
上调用Get Member
cmdlet,它应该只在那里出现一次,您将看到,您只能设置ErrorDetails
:
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void ISerializable....
GetType Method type GetType()
ToString Method string ToString()
CategoryInfo Property System.Management.Automation.ErrorCategoryInfo CategoryInfo {get;}
ErrorDetails Property System.Management.Automation.ErrorDetails ErrorDetails {get;set;}
Exception Property System.Exception Exception {get;}
FullyQualifiedErrorId Property string FullyQualifiedErrorId {get;}
InvocationInfo Property System.Management.Automation.InvocationInfo InvocationInfo {get;}
PipelineIterationInfo Property System.Collections.ObjectModel.ReadOnlyCollection[int] PipelineIterationInfo {get;}
ScriptStackTrace Property string ScriptStackTrace {get;}
TargetObject Property System.Object TargetObject {get;}
PSMessageDetails ScriptProperty System.Object PSMessageDetails {get=& { Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails };}
所以你可以这样做:
Try {...}
catch {
$_.ErrorDetails += ('Letter: {0} Path: {1}' -f $_.letter, $_.path)
throw $_
}
如果在
ErrorRecord
上调用Get Member
cmdlet,您将看到,您只能设置ErrorDetails
:
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void ISerializable....
GetType Method type GetType()
ToString Method string ToString()
CategoryInfo Property System.Management.Automation.ErrorCategoryInfo CategoryInfo {get;}
ErrorDetails Property System.Management.Automation.ErrorDetails ErrorDetails {get;set;}
Exception Property System.Exception Exception {get;}
FullyQualifiedErrorId Property string FullyQualifiedErrorId {get;}
InvocationInfo Property System.Management.Automation.InvocationInfo InvocationInfo {get;}
PipelineIterationInfo Property System.Collections.ObjectModel.ReadOnlyCollection[int] PipelineIterationInfo {get;}
ScriptStackTrace Property string ScriptStackTrace {get;}
TargetObject Property System.Object TargetObject {get;}
PSMessageDetails ScriptProperty System.Object PSMessageDetails {get=& { Set-StrictMode -Version 1; $this.Exception.InnerException.PSMessageDetails };}
所以你可以这样做:
Try {...}
catch {
$_.ErrorDetails += ('Letter: {0} Path: {1}' -f $_.letter, $_.path)
throw $_
}
我将使用
$error | fl*-Force
扩展错误,并将结果传输到输出文件
@jisaak问题更像是,如何向错误变量获取额外的内容(请参见$failedletter和$failedpath),因为此变量行为是用错误信息填充自身。我不知道如何在不中断任何内容或两次获取有关1个错误的错误信息的情况下执行此操作。我会使用$error | fl*-Force
扩展错误,并将结果传输到输出文件
@jisaak。问题更像是,如何获得错误变量的其他内容(请参见$failedletter和$failedpath),因为这个变量的行为是用错误信息填充自己。我不知道如何在不破坏任何东西或两次获取有关1个错误的错误信息的情况下执行此操作。