如何将“Program Files (x86)”传递给 Powershell?

如何将“Program Files (x86)”传递给 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

它一直给我这个:

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

我尝试了不同的路径,但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

编辑3:

我有这个脚本调用 load-ems.ps1(用于加载 Exchange 管理 Shell),并且我试图以提升权限启动该 Shell。但我的问题是:

  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"

答案1

您必须将 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

答案2

如果是 Exchange 2013,您可以尝试:

$Credential = Get-Credential  
$Session = New-PSSession -ConfigurationName Microsoft.Exchange ` 
-ConnectionUri http://your.exchange.server/PowerShell/ -Credential $Credential
Import-PSSession $Session

答案3

根据 @matandra 的评论,我稍微修改了一下脚本,得到了我想要的结果。我需要用'单引号括住路径。例如:"cd '$pwd'"

我还发布了我的完整脚本,希望它能帮助其他人。我现在可以找到并加载 EMS,然后执行我需要的任何其他命令。Load-EMS不带参数的调用只会为您加载 EMS(以编程方式,无论安装了哪个版本的 Exchange Server,只要安装了)。

是的,它的功能远远超出了我最初的要求,但看到这是superuser论坛,我的脚本可能会对其他人有所帮助:

<#
.SYNOPSIS
Find the Microsoft Exchange that is installed on the current machine

.DESCRIPTION
The function will go through the Windows Registry and try to locate 
the latest installed Microsoft Exchange instances on the current machine,
then locate the RemoteExchange.ps1 powershell script and return its location.

.EXAMPLE
Find-Exchange
C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1
#>
function Find-Exchange() {
    $exchangeroot = "HKLM\SOFTWARE\Microsoft\ExchangeServer\"

    $allexchanges = Get-ChildItem -Path Registry::$exchangeroot -Name | Where-Object { $_ -match "^V.." }
    $sorted = $allexchanges | Sort-Object -descending

    If ($sorted.Count -gt 1) { $latest = $sorted[0] } Else { $latest = $sorted }

    $setup = $exchangeroot + $latest + "\Setup"

    $properties = Get-ItemProperty -Path Registry::$setup

    $installPath = $properties.MsiInstallPath

    $bin = $installPath + "bin"

    $remoteexchange = $bin + "\RemoteExchange.ps1"

    echo $remoteexchange
}

<#
.SYNOPSIS
Load Exchange Management Shell (EMS) and execute additional commands

.DESCRIPTION
The script loads the EMS and then execute commands you pass via $AdditionalCommands parameter

.EXAMPLE
Load-EMS -AdditionalCommands @("echo 'HELLO WORLD'", "Get-TransportAgent")  
#>
function Load-EMS($AdditionalCommands) {
    $pwd = (Get-Item -Path "." -verbose).FullName
    $remoteexchangeps1 = Find-Exchange

    # These commands are needed to load the EMS
    # We need to cd to the current directory because we will be starting a
    # brand new elevated powershell session, then we load the EMS script,
    # finally we connect to the exchange server after loading the EMS.
    [System.Collections.ArrayList] 
    $cmdchain = @(
        "cd '$pwd'"
        ". '$remoteexchangeps1'"
        "Connect-ExchangeServer -auto -ClientApplication:ManagementShell"
    )

    # We will also chain the commands passed by the user and run them too
    $cmdchain += $AdditionalCommands;

    # We combine all of the commands into a single line and pass it over
    # to the new elevated powershell session that we are about to launch
    $chained = @($cmdchain | % {"$_;"})
    $arguments = "-noexit -command $chained"

    # Launching the powershell
    Start-Process powershell.exe -Verb RunAs -ArgumentList $arguments
}

答案4

我使用的一个解决方法是将工作目录更改为包含该问题(x86),然后做你的开始进程从那里打电话。考虑到%1是个“C:\Program Files (x86).....\application.exe”, 你可以做:

cd /d %~dp1
powershell.exe Start-Process '.\%~nx1' -Verb runAs

相关内容