Powershell 脚本调用表达式查看传递给调用脚本的字符串参数并在其中查找错误

Powershell script invoke-expression looking at string parameter passed to script called and finding errors in it

我有一个收集错误消息的脚本,它调用另一个带有错误字符串的脚本,这样我的另一个脚本就可以通过电子邮件将错误信息发送给我。但是,invoke-expression 似乎在我传递的字符串中查找,并报告错误。我尝试从字符串中清除不寻常的字符以防出现问题,但我没有字符可以删除,这似乎是个问题。 因此,我们正在调试对 invoke-expression 的调用,而不是我在下面发布的错误消息。我已经知道我注释掉了创建它所缺少的目录的步骤。

这是我的错误字符串:

Could not find a part of the ph 'EDisasterBackuptoLoc_20190123PrivilegesPrivileges_HH_Bakcsv'Exception No such ph EDisasterBackuptoLoc_20190123PrivilegesPrivileges_HH_Bakcsv  Exception SystemManagementAutomionItemNotFoundException Cannot find ph 'EDisasterBa
ckuptoLoc_20190123PrivilegesPrivileges_HH_Bakcsv' because it does not exist
    SystemManagementAutomionLocionGlobberExpandMshGlobPhString ph Boolean allowNonexistingPhs PSDriveInfo drive ContainerCmdletProvider provider CmdletProviderContext context
    SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhString ph CmdletProviderContext context Boolean allowNonexistingPhs CmdletProvider providerInstance
    SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadPhString ph Boolean allowNonexistingPhs CmdletProviderContext context CmdletProvider providerInstance
    SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMonadPhString ph Boolean allowNonexistingPhs CmdletProviderContext context ProviderInfo provider CmdletProvider providerInstance
    SystemManagementAutomionSessionSteInternalCopyItemString phs String copyPh Boolean recurse CopyContainers copyContainers CmdletProviderContext context
    MicrosoftPowerShellCommandsCopyItemCommandProcessRecordException No such ph EDisasterBackuptoLoc_20190123PrivilegesPrivileges_EDC_Bakcsv  Exception SystemManagementAutomionItemNotFoundException Cannot find ph 'EDisasterBackuptoLoc_20190123PrivilegesPrivi
leges_EDC_Bakcsv' because it does not exist
    SystemManagementAutomionLocionGlobberExpandMshGlobPhString ph Boolean allowNonexistingPhs PSDriveInfo drive ContainerCmdletProvider provider CmdletProviderContext context
    SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhString ph CmdletProviderContext context Boolean allowNonexistingPhs CmdletProvider providerInstance
    SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadPhString ph Boolean allowNonexistingPhs CmdletProviderContext context CmdletProvider providerInstance
    SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMonadPhString ph Boolean allowNonexistingPhs CmdletProviderContext context ProviderInfo provider CmdletProvider providerInstance
    SystemManagementAutomionSessionSteInternalCopyItemString phs String copyPh Boolean recurse CopyContainers copyContainers CmdletProviderContext context
    MicrosoftPowerShellCommandsCopyItemCommandProcessRecordException No such ph EDisasterBackuptoLoc_20190123PrivilegesPrivileges_SMH_Bakcsv  Exception SystemManagementAutomionItemNotFoundException Cannot find ph 'EDisasterBackuptoLoc_20190123PrivilegesPrivi
leges_SMH_Bakcsv' because it does not exist
    SystemManagementAutomionLocionGlobberExpandMshGlobPhString ph Boolean allowNonexistingPhs PSDriveInfo drive ContainerCmdletProvider provider CmdletProviderContext context
    SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhString ph CmdletProviderContext context Boolean allowNonexistingPhs CmdletProvider providerInstance
    SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadPhString ph Boolean allowNonexistingPhs CmdletProviderContext context CmdletProvider providerInstance
    SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMonadPhString ph Boolean allowNonexistingPhs CmdletProviderContext context ProviderInfo provider CmdletProvider providerInstance
    SystemManagementAutomionSessionSteInternalCopyItemString phs String copyPh Boolean recurse CopyContainers copyContainers CmdletProviderContext context
    MicrosoftPowerShellCommandsCopyItemCommandProcessRecord

这是我脚本的一部分:

#cleanse string and add to global string
function CleanseString($tempStr){
   $temp2Str = $tempStr -replace "\-"
   $temp3Str = $temp2Str -replace "\:"
   $temp4Str = $temp3Str -replace "\/"
   $temp5Str = $temp4Str -replace "\,"
   $temp6Str = $temp5Str -replace "\."
   $temp7Str = $temp6Str -replace "\*"
   $temp8Str = $temp7Str -replace "\"
   $temp9Str = $temp8Str -replace "\("
   $temp10Str = $temp9Str -replace "\)"
   $temp11Str = $temp10Str -replace "\+"
   $temp12Str = $temp11Str -replace "\["
   $temp13Str = $temp12Str -replace "\]"
   $temp14Str = $temp13Str -replace "\-"
   $temp15Str = $temp14Str -replace "\&" #replace space with nothing
   $temp16Str = $temp15Str+"`r`n" #add newline to error log so it's readable
   $temp17Str = $temp16Str -replace "\@" #replace at symbol with nothing
   $temp18Str = $temp17Str -replace "AT"#, "location" #replace AT  with location 
   $temp19Str = $temp18Str -replace "at"#, "location" #replace at  with location 
   return $temp19Str
}

[string] $errorCodeAsString = ""
foreach ($item in $global:ErrorStrings){
   $errorCodeAsString += $item 
}
if($errorCodeAsString -ne "")
{
   $errorCodeAsString = CleanseString($errorCodeAsString)
   write-output  $errorCodeAsString
   #$errorCodeAsString = "Error in Privilege script. Length error $errorCodeAsString.length " + $errorCodeAsString
   $ScriptPath = Split-Path $MyInvocation.InvocationName
   $ScriptPathFilename = Join-Path -Path $ScriptPath -Child "\EmailAlertFailure.ps1"
   Invoke-Expression "$ScriptPathFilename $errorCodeAsString"
   #Invoke-Expression "& `"$ScriptPath\EmailAlertFailure.ps1`" $errorCodeAsString" 
   Exit 99
}
Exit 0

这是我跨过 invoke-expression 行时收到的错误消息:

SystemManagementAutomionLocionGlobberExpandMshGlobPhString : The term 'SystemManagementAutomionLocionGlobberExpandMshGlobPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if 
a path was included, verify that the path is correct and try again.
At line:2 char:5
+     SystemManagementAutomionLocionGlobberExpandMshGlobPhString ph Boo ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...MshGlobPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhString : The term 'SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of 
the name, or if a path was included, verify that the path is correct and try again.
At line:3 char:5
+     SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhStrin ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...alifiedPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadPhString : The term 'SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:4 char:5
+     SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadP ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...omMonadPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMonadPhString : The term 'SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMonadPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. 
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:5 char:5
+     SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMon ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...omMonadPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionSessionSteInternalCopyItemString : The term 'SystemManagementAutomionSessionSteInternalCopyItemString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again.
At line:6 char:5
+     SystemManagementAutomionSessionSteInternalCopyItemString phs Stri ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...lCopyItemString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

MicrosoftPowerShellCommandsCopyItemCommandProcessRecordException : The term 'MicrosoftPowerShellCommandsCopyItemCommandProcessRecordException' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 
name, or if a path was included, verify that the path is correct and try again.
At line:7 char:5
+     MicrosoftPowerShellCommandsCopyItemCommandProcessRecordException  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (MicrosoftPowerS...RecordException:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionLocionGlobberExpandMshGlobPhString : The term 'SystemManagementAutomionLocionGlobberExpandMshGlobPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if 
a path was included, verify that the path is correct and try again.
At line:8 char:5
+     SystemManagementAutomionLocionGlobberExpandMshGlobPhString ph Boo ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...MshGlobPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhString : The term 'SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of 
the name, or if a path was included, verify that the path is correct and try again.
At line:9 char:5
+     SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhStrin ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...alifiedPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadPhString : The term 'SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:10 char:5
+     SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadP ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...omMonadPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMonadPhString : The term 'SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMonadPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. 
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:11 char:5
+     SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMon ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...omMonadPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionSessionSteInternalCopyItemString : The term 'SystemManagementAutomionSessionSteInternalCopyItemString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again.
At line:12 char:5
+     SystemManagementAutomionSessionSteInternalCopyItemString phs Stri ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...lCopyItemString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

MicrosoftPowerShellCommandsCopyItemCommandProcessRecordException : The term 'MicrosoftPowerShellCommandsCopyItemCommandProcessRecordException' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 
name, or if a path was included, verify that the path is correct and try again.
At line:13 char:5
+     MicrosoftPowerShellCommandsCopyItemCommandProcessRecordException  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (MicrosoftPowerS...RecordException:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionLocionGlobberExpandMshGlobPhString : The term 'SystemManagementAutomionLocionGlobberExpandMshGlobPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if 
a path was included, verify that the path is correct and try again.
At line:14 char:5
+     SystemManagementAutomionLocionGlobberExpandMshGlobPhString ph Boo ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...MshGlobPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhString : The term 'SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of 
the name, or if a path was included, verify that the path is correct and try again.
At line:15 char:5
+     SystemManagementAutomionLocionGlobberResolveDriveQualifiedPhStrin ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...alifiedPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadPhString : The term 'SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:16 char:5
+     SystemManagementAutomionLocionGlobberGetGlobbedMonadPhsFromMonadP ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...omMonadPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMonadPhString : The term 'SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMonadPhString' is not recognized as the name of a cmdlet, function, script file, or operable program. 
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:17 char:5
+     SystemManagementAutomionLocionGlobberGetGlobbedProviderPhsFromMon ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...omMonadPhString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

SystemManagementAutomionSessionSteInternalCopyItemString : The term 'SystemManagementAutomionSessionSteInternalCopyItemString' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again.
At line:18 char:5
+     SystemManagementAutomionSessionSteInternalCopyItemString phs Stri ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SystemManagemen...lCopyItemString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

MicrosoftPowerShellCommandsCopyItemCommandProcessRecord : The term 'MicrosoftPowerShellCommandsCopyItemCommandProcessRecord' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path 
was included, verify that the path is correct and try again.
At line:19 char:5
+     MicrosoftPowerShellCommandsCopyItemCommandProcessRecord
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (MicrosoftPowerS...ndProcessRecord:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

在我从字符串参数中删除 at 或 AT 之前,错误消息告诉我 AT 是一个已弃用的命令之类的。问题是 invoke-expression 正在查看我传递的字符串并尝试执行参数 $errorCodeAsString。我尝试了带有 & 的 invoke-expression 行,但错误消息抱怨该版本的 &。

我一直在研究 call command ,但我不确定是否可以使用它来代替带参数的 invoke-expression。我也乐于接受有关将错误消息通过电子邮件发送给自己的更好方法的建议。

*更新: 我尝试使用

Invoke-Expression "&'$ScriptPathFilename $errorCodeAsString' "

用单引号,得到这个错误信息:

& : The term 'C:\Users\me\Documents18\powershellFiles\EmailAlertFailure.ps1 Could not find a part of the ph ' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again.
At line:1 char:2
+ &'C:\Users\me\Documents18\powershellFiles\EmailAlertFailure. ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\me...part of the ph :String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我使用调用运算符将​​其与以下内容一起使用:

& $ScriptPathFilename $errorCodeAsString