Powershell脚本调用表达式查看传递给调用脚本的字符串参数并在其中查找错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Powershell脚本调用表达式查看传递给调用脚本的字符串参数并在其中查找错误相关的知识,希望对你有一定的参考价值。
我有一个收集错误消息的脚本,它使用错误字符串调用另一个脚本,因此我的其他脚本可以通过电子邮件向我发送有关错误的信息。但是,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 "& `"$ScriptPathEmailAlertFailure.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:UsersmeDocuments2018powershellFilesEmailAlertFailure.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:UsersmeDocuments2018powershellFilesEmailAlertFailure. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:Usersme...part of the ph :String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
答案
我使用call运算符使用以下代码:
& $ScriptPathFilename $errorCodeAsString
以上是关于Powershell脚本调用表达式查看传递给调用脚本的字符串参数并在其中查找错误的主要内容,如果未能解决你的问题,请参考以下文章