如何在powershell中使用带有空格的$ ScriptDir传递msi ArgumentList? [英] How to pass msi ArgumentList with $ScriptDir with spaces in powershell?

查看:59
本文介绍了如何在powershell中使用带有空格的$ ScriptDir传递msi ArgumentList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从

例如如果 source directory/$ScriptDirC:\Users\Dell\Desktop\New folder powershell 脚本挂在上面的消息.. 还没有安装.

我在脚本末尾添加了 echo 来查找路径 $ScriptDir

它给出了下面的回声结果,这让我更加困惑.

 C:\Users\Dell\Desktop\New 文件夹

不确定为什么 msiexec.exe 在有空格时不能运行参数.

请帮我找出可能是什么原因?即使 $ScriptDir 有空格,如何修复它才能运行?

解决方案

如果您要从命令行调用 msiexec.exe(或大多数其他命令),并且路径中包含空格,您需要将该路径用引号括起来.

不幸的是,您的路径在通过时实际上并没有它们(尽管在您的哈希表中提供了它们.)

为此,请提供一些转义引号 (""):

$MSIArguments = @(/一世""""$ScriptDir\PBIDesktop.msi""""/qn"# "/norestart"ACCEPT_EULA=1")

实际的最终结果是:对于路径,将它们用三个引号括起来.

I downloaded Powerbi 32 bit and 64 bit msi files from https://powerbi.microsoft.com/en-us/downloads/

and created below script .

$ScriptDir = (Split-Path $MyInvocation.MyCommand.Path)


$MSIArguments = @(
    "/i"
    "$ScriptDir\PBIDesktop.msi"
    "/qn"
 #   "/norestart"
     "ACCEPT_EULA=1"
)

$MSIArguments2 = @(
    "/i"
    "$ScriptDir\PBIDesktop_x64.msi"
    "/qn"
#    "/norestart"
    "ACCEPT_EULA=1"
)

$architecture=gwmi win32_processor | select -first 1 | select addresswidth
if ($architecture.addresswidth -eq "64"){
    Start-Process "msiexec.exe" -ArgumentList $MSIArguments2 -wait
}
elseif ($architecture.addresswidth -eq "32"){
   Start-Process "msiexec.exe" -ArgumentList $MSIArguments -wait
    }
$ScriptDir

Script works perfectly only if the source directory/$ScriptDir does not have spaces in between. for example if source directory is c:/test or c:/test_test/test it works perfectly.

But if the source directory/$ScriptDir has spaces, it hangs with msi option error given below

for example if source directory/$ScriptDir is C:\Users\Dell\Desktop\New folder powershell script hangs at above message .. yet no installation .

i have added echo at the end of the script to find the path $ScriptDir

and it gives below echo result which makes me more confusing.

    C:\Users\Dell\Desktop\New folder

Not sure why msiexec.exe cant run arguments when there is a space .

Please help me to fiugre out What could be the reason ? how could this be fixed to run even if $ScriptDir is having spaces ?

解决方案

If you were to call msiexec.exe (or most other commands) from the command line with a path with spaces in it, you'd need to wrap that path in quotes.

Unfortunately, your path by the time it's being passed through doesn't actually have them (despite supplying them in your hash table.)

To do this, provide some escaped quotes (""):

$MSIArguments = @(
    "/i"
    """$ScriptDir\PBIDesktop.msi"""
    "/qn"
 #   "/norestart"
     "ACCEPT_EULA=1"
)

This has the practical end result of: for paths, wrap them in three quote marks.

这篇关于如何在powershell中使用带有空格的$ ScriptDir传递msi ArgumentList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆