Вызов-выражение скрипта Powershell, смотрящий на строковый параметр, переданный вызываемому скрипту, и находящий в нем ошибки - PullRequest
0 голосов
/ 23 января 2019

У меня есть сценарий, который собирает сообщения об ошибках, и он вызывает другой сценарий со строкой ошибки, чтобы мой другой сценарий мог отправить мне сообщение об ошибке. Однако 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

Это сообщение об ошибке, которое я получаю, когда перехожу строку выражения-вызова:

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 с &, но сообщение об ошибке жалуется на & для этой версии.

Я искал команду вызова , но я не уверен, смогу ли я использовать это вместо invoke-expression с параметром. Я также был бы открыт для предложений о том, как лучше отправить мне сообщение об ошибке по электронной почте.

* Обновление: Я пытался использовать

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

с одинарной кавычкой, и получите это сообщение об ошибке:

& : The term 'C:\Users\me\Documents\2018\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\Documents\2018\powershellFiles\EmailAlertFailure. ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\me...part of the ph :String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

1 Ответ

0 голосов
/ 24 января 2019

У меня получилось работать со следующими, используя оператор вызова:

& $ScriptPathFilename $errorCodeAsString
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...