2015-01-15 85 views
0

我有一个PowerShell脚本和启动它的bat文件。我想让bat文件打开powershell,然后让powershell用提升的权限启动另一个shell,然后运行两个命令。第一个命令是更改目录,第二个命令是启动powershell脚本。启动管理外壳,然后执行多个命令

到目前为止,我有这样的:

powershell -NoProfile -ExecutionPolicy ByPass -Command "& {Start-Process PowerShell -Verb RunAs -ArgumentList '-NoExit -NoProfile -ExecutionPolicy Bypass cd %~dp0 .\App\Deploy-Application.ps1}'" 

这是我有问题的部分:

cd %~dp0 .\App\Deploy-Application.ps1 

我想运行这两个命令,但我不知道怎么样。它运行一个命令。我尝试在命令之间添加分号,但它不起作用。

回答

1

做了一个简单的测试,这是我得到的工作:

TEST.BAT

cd %~dp0 
powershell -NoProfile -Command ".\test.ps1" 

Test.ps1

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) 
{ 
    $arguments = "-noprofile & '" + $myinvocation.mycommand.definition + "'" 
    Start-Process powershell -Verb runAs -ArgumentList $arguments 
    Break 
} 

Write-Host "Rawr" 

Pause 

如果我运行该批处理文件,它会打开然后检查当前窗口是否以管理员身份运行,如果不是,请以管理员身份重新打开脚本。

之后它在我的屏幕上显示Rawr。

在你的情况,而不是写的主机,你可以把

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) 
{ 
    $arguments = "-noprofile & '" + $myinvocation.mycommand.definition + "'" 
    Start-Process powershell -Verb runAs -ArgumentList $arguments 
    Break 
} 

cd <Your directory to change to here> 
<run command here> 

Pause 
+0

这看起来比我的解决方案更好。明天去试试看。 – ConditionRacer