I need to call DISM.exe via its absolute path which changes dependent on system architecture. Therefore, I store the filepath of dism.exe in a variable and then proceed to execute the wntire command via
invoke-expression, like so:
$dism = "C:\Windows\sysnative\dism.exe" invoke-expression "$dism /online /enable-feature /featurename:$index"
This is working fine. However, I also have to pipe DISM's output into a find command, so I tries this:
invoke-expression "$dism /online /get-featureinfo /featurename:$index | find `"State :`""
That gives me the error "FIND: Parameter format not correct" I tried putting the parameter-string for find into its own variable -> nothing.
How can I pass quoted strings inside a call for invoke-expression?
Usually I would use the cmdlet-version of any DISM command (in this case I could use
get-WindowsOptionalFeature). However, this script is part of an application that is rolled out via SCCM Software Center. The reason I cannot use the cmdlet version here is as folows:
SoftwareCenter is a 32-bit application and thus the powershell-script is also run in 32-bit mode. I have to modify both 32- and 64-bit images. I work around this problem by launching Powershell from the hidden sysnative folder (explanation see here).
However, since I can't konw in advance wheahter the image I'm modifying is 32-bit or 64-bit, I have to check that at the beginning of my script and then I store the corresponding program path for Powershell in a variable.
Now, I know I can use
invoke-expression (see code above) to call a command stored in a variable with parameters. I do not know how to do that with cmdlets, since that doesn't give me the possibility to use a custom program path. That is why I had to use the old dism.exe command. I now have a workaround for the whole situation which consists of having a "launcher" script which detect the bitness in advance and then launches my actual script with the corresponding version of Powershell.
The original issue, which is "how can I pass parameters containing quoted arguments to the
invoke-expresion-command, still stands...
Thank you in advance!