开发者

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

开发者 https://www.devze.com 2023-02-05 13:17 出处:网络
This works Dependencies\\iis7psprov_x86.msi /qn /l* $SnapinInstallLog But this doesn\'t $SnapinInstaller = \"Dependencies\\iis7psprov_x86.msi\"

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 开发者_Python百科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"


That's because powershell treats $SnapinInstaller like a string, not a command.

The first way to do what you want I can remeber is to write

Invoke-Expression -Command ($SnapinInstaller + " /qn /l* " + $SnapinInstallLog)

This works with *.exe, I didn't try it with *.msi.

0

精彩评论

暂无评论...
验证码 换一张
取 消