简体   繁体   English

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

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

The 'Deployment and Imaging Tools Environment' is a shortcut I use that specifically runs this- 'C:\\WINDOWS\\system32\\cmd.exe /k "C:\\Program Files (x86)\\Windows Kits\\10\\Assessment and Deployment Kit\\Deployment Tools\\DandISetEnv.bat" '. “部署和映像工具环境”是我使用的快捷方式,它专门运行以下命令-'C:\\ WINDOWS \\ system32 \\ cmd.exe / k“ C:\\ Program Files(x86)\\ Windows Kits \\ 10 \\ Assessment and Deployment Kit \\ Deployment Tools \\ DandISetEnv.bat“'。

What I'm trying to do is run this command in a Powershell script, and then run another command within that new window. 我想做的是在Powershell脚本中运行此命令,然后在该新窗口中运行另一个命令。 The command I need to run is oscdimg, which can't run straight in powershell, this .bat file needs to run first, and then the long oscdimg inside that. 我需要运行的命令是oscdimg,它无法在powershell中直接运行,此.bat文件需要先运行,然后再运行其中的长oscdimg。 Here's my current script. 这是我当前的脚本。 All the comments at the end are different approaches I've already taken with no success. 最后的所有评论都是我已经采用的不同方法,没有成功。 The furthest I've gotten is starting the .bat file fine, but with the incorrect parameter passed through I think. 我得到的最详细的信息是启动.bat文件,但是我认为传递的参数不正确。 Thanks for any help 谢谢你的帮助

Per request, Here is a much simpler example of what I'm trying to do. 根据请求, 是我要执行的操作的简单得多的示例。 This small script runs cmd, and attempts to add arguments to change directories, but the resulting cmd window doesn't do that. 这个小脚本运行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

I was missing a /c or /k in the beginning of the argument so cmd knows to run it apparently. 我在参数开头缺少/ c或/ k,因此cmd显然可以运行它。 ( /c runs the argument and closes the window, /k runs it and keeps the window open) (/ c运行参数并关闭窗口,/ k运行参数并保持窗口打开)

Working Example- 工作示例

$command = "/k cd .."

Start-Process cmd -ArgumentList $command

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM