Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

You must provide a value expression on the right-hand side of the '/' operator

Tags:

powershell

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?

like image 846
burnt1ce Avatar asked Jan 20 '11 15:01

burnt1ce


1 Answers

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"
like image 142
Keith Hill Avatar answered Nov 17 '22 00:11

Keith Hill