0

I have a script that collects error messages, and it calls another script with the error string so my other script can email me about the error. However, invoke-expression seems to be looking within the string I pass, and reporting errors. I tried cleaning unusual characters from within the string in case that was the issue, but I'm out of characters to remove that seem like they would be an issue. So, we are debugging the call to invoke-expression, not the error message I am posting below. I already know that I commented out the step that creates the directory it is missing.

This is my error string:

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

This is a portion of my script:

#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

This is the error message I get when I step over the invoke-expression line:

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

Before I removed at or AT from the string parameter, the error message was telling me that AT is a deprecated command or something. The problem is that invoke-expression is looking into the string I'm passing and trying to execute the parameter, $errorCodeAsString. I tried the line for invoke-expression with the &, but the error message complains about & for that version.

I've been looking into the call command , but I'm not sure if I can use that instead of invoke-expression with a parameter. I'd also be open to suggestions of a better way to email my error message to myself.

*update: I tried using

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

with single quote, and get this error message:

& : 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
3
  • Is this a duplicate of question 47074872? (See my answer there, not the incorrect accepted answer.) Jan 23, 2019 at 16:42
  • The error message is different, plus I think it's looking into my parameter string and not passing it. I tried the & in the commented out invoke command, and the error message complained about &.
    – Michele
    Jan 23, 2019 at 17:40
  • Is it single quote or backticks around the scriptPath? Does that need to go around the parameter too?
    – Michele
    Jan 23, 2019 at 17:42

1 Answer 1

0

I got it working with the following, using call operator:

& $ScriptPathFilename $errorCodeAsString

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.