簡體   English   中英

使用參數從Powershell腳本運行.bat文件

[英]Running .bat file from Powershell script with parameters

“部署和映像工具環境”是我使用的快捷方式,它專門運行以下命令-'C:\\ WINDOWS \\ system32 \\ cmd.exe / k“ C:\\ Program Files(x86)\\ Windows Kits \\ 10 \\ Assessment and Deployment Kit \\ Deployment Tools \\ DandISetEnv.bat“'。

我想做的是在Powershell腳本中運行此命令,然后在該新窗口中運行另一個命令。 我需要運行的命令是oscdimg,它無法在powershell中直接運行,此.bat文件需要先運行,然后再運行其中的長oscdimg。 這是我當前的腳本。 最后的所有評論都是我已經采用的不同方法,沒有成功。 我得到的最詳細的信息是啟動.bat文件,但是我認為傳遞的參數不正確。 謝謝你的幫助

根據請求, 是我要執行的操作的簡單得多的示例。 這個小腳本運行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

我在參數開頭缺少/ c或/ k,因此cmd顯然可以運行它。 (/ c運行參數並關閉窗口,/ k運行參數並保持窗口打開)

工作示例

$command = "/k cd .."

Start-Process cmd -ArgumentList $command

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM