This works
Dependencies\iis7psprov_x86.msi /qn /l* $SnapinInstallLog
But this doesn't
$SnapinInstaller = "Dependencies\iis7psprov_x86.msi"
$SnapinInstaller /qn /l* $SnapinInstallLog
I get the following error : You must provide a value expression on the right-hand side of the '/' operator. + $SnapinInstaller /q <<<< n /l* $SnapinInstallLog
How do i get the snap-in to install with the variable $SnapinInstaller?
PowerShell recognizes $SnapinInstaller
as a variable and evaluates it - to a string. If you want PowerShell to "call" the command named by the variable then you use the call operator &
like so:
& $SnapinInstaller /qn /l* $SnapinInstallLog
While you can use Invoke-Expression
, it is usually avoided, especially with user provided data, due to the possibilty of script injection attacks e.g.:
PS> $SnapinInstallLog = Read-Host "Enter log file name"
Enter log file name: c:\temp\snapin.log; remove-item C:\xyzzy -r -force -whatif
PS> Invoke-Expression "$SnapinInstaller /qn /l* $SnapinInstallLog"
Badness happens here because the user was able to inject arbitrary script.
OTOH if you have a bunch arguments represented in a single string (and no user provided input), Invoke-Expression
can come in handy in that scenario e.g.:
$psargs = "\\$computer -d -i 0 notepad.exe"
Invoke-Expression "psexec.exe $psargs"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With