使用参数从Powershell脚本运行.bat文件

时间:2017-06-14 18:16:58

标签: windows powershell batch-file cmd parameters

“部署和映像工具环境”是我使用的一种快捷方式,专门运行它 - 'C:\ WINDOWS \ system32 \ cmd.exe / k“C:\ Program Files(x86)\ Windows Kits \ 10 \ Assessment和部署工具包\部署工具\ DandISetEnv.bat“'。

我要做的是在Powershell脚本中运行此命令,然后在该新窗口中运行另一个命令。我需要运行的命令是oscdimg,它不能直接在powershell中运行,这个.bat文件需要先运行,然后再运行那个长的oscdimg。这是我目前的剧本。最后的所有评论都是我已经采取的不同方法,但没有成功。我得到的最远的是启动.bat文件,但我认为通过了错误的参数传递。谢谢你的帮助

根据请求,Here是我正在尝试做的更简单的示例。这个小脚本运行cmd,并尝试添加参数以更改目录,但生成的cmd窗口不会这样做。

$srcDIR = Read-Host -Prompt 'Paste the Source Files Directory '
$destDIR = Read-Host -Prompt 'Paste the output destination, with .iso extension '

$etfsbootDIR = "$srcDIR\boot\etfsboot.com"
$efisysDIR = "$srcDIR\efi\microsoft\boot\efisys.bin"

$etfsboot = "`"" + $etfsbootDIR + "`""
$efisys = "`"" + $efisysDIR + "`""
$src = "`"" + $srcDIR + "`""
$dest = "`"" + $destDIR + "`""

$dir8 = "C:\Program Files (x86)\Windows Kits\8.1\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg"
$dir10 = "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg"

if(Test-Path $dir8) { 
    # cd $dir8 
    $etftest = $dir8 + "\etfsboot.com"
}
elseif(Test-Path $dir10){ 
    # cd $dir10 
    $etftest = $dir10 + "\etfsboot.com"
}
else{
    "No Assessment and Deployment Kit detected."
    return
}

$etft = "`"" + $etftest + "`""

if(Test-Path $etfsbootDIR) { "etfsboot.com found at $etfsboot" }
if(Test-Path $efisysDIR)   { "efisys.bin found at $efisys"     }

$beginning = "./oscdimg.exe -m -o -u2 -udfver102 -bootdata:2#p0,e,b"
$middle = "#pEF,e,b"
$command = $beginning + $etfsboot + $middle + $efisys + " " + $src + " " + $dest

$rest = "C:\windows\system32\cmd.exe /k 'C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\DandISetEnv.bat'"
$shortcut = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Windows Kits\Windows ADK\Deployment and Imaging Tools Environment.lnk"

# Start-Process -FilePath $shortcut -ArgumentList $command
# cmd.exe /c $rest $command
# cmd %1 $rest
# & $rest $command

1 个答案:

答案 0 :(得分:0)

我在参数的开头缺少一个/ c或/ k,所以cmd知道显然会运行它。 (/ c运行参数并关闭窗口,/ k运行它并保持窗口打开)

工作实例 -

$command = "/k cd .."

Start-Process cmd -ArgumentList $command