您如何将“程序文件(x86)”传递给 Powershell?

cod*_*ero 11 powershell

我已经阅读并尝试了各种方法,但它就是不接受......我什至试图逃避空格以及试图在路径前添加额外的引号(转义引号)......

$cmd = 'powershell.exe'
$dir = 'C:\Program Files (x86)\W T F'
$inner = "-NoExit -Command cd $dir"
$arguments = "Start-Process powershell -ArgumentList '$inner' -Verb RunAs"
& $cmd $arguments
Run Code Online (Sandbox Code Playgroud)

它不断给我这个:

x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:22
+ cd C:\Program Files (x86)\W T F
+                      ~~~
    + CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的路径,说它C:\Blah\W T F仍然会抱怨里面的空间W T F

编辑:基本上我需要启动一个elevated powershell然后 CD 到我的目录中以运行一些安装后脚本。但是我很难将 CD 放入我的目录中,我能够启动提升的 powershell,但它总是转到c:\windows\system32.

编辑2:

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.42000
BuildVersion                   6.3.9600.18728
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2
Run Code Online (Sandbox Code Playgroud)

编辑3:

我有这个脚本调用 load-ems.ps1(用于加载 Exchange 命令行管理程序),我正在尝试以提升的方式启动该外壳程序。但我的问题是:

  1. shell 将在 system32 中启动并且找不到我的脚本
  2. 如果我尝试 CD 到我的目录,我不能。
. ".\find-exchange.ps1"

$remoteexchangeps1 = Find-Exchange
$commands = @(
    ". '$remoteexchangeps1';",
    "Connect-ExchangeServer -auto -ClientApplication:ManagementShell;",
    ".\plugin-reinstall.ps1;"
)

$command = @($commands | % {$_})
powershell.exe -noexit -command "$command"

Run Code Online (Sandbox Code Playgroud)

小智 7

您必须将 CD 的参数用单引号括起来以保留空格。直接使用 Start-Process 避免过多的变量插值:

$cmd = 'powershell.exe'
$dir = 'C:\Program Files (x86)\W T F'
$inner = "-NoExit -Command cd '$dir'"
Start-Process $cmd -ArgumentList $inner -Verb RunAs
Run Code Online (Sandbox Code Playgroud)