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

     2023-02-25     290

关键词:

【中文标题】使用参数从 Powershell 脚本运行 .bat 文件【英文标题】:Running .bat file from Powershell script with parameters 【发布时间】:2017-11-17 00:41:16 【问题描述】:

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

我要做的是在 Powershell 脚本中运行此命令,然后在该新窗口中运行 another 命令。我需要运行的命令是oscdimg,它不能在powershell中直接运行,这个.bat文件需要先运行,然后是里面的长oscdimg。这是我当前的脚本。最后的所有 cmets 都是我已经采取的不同方法,但没有成功。我得到的最远的是启动 .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

【问题讨论】:

您能否提供一个简化示例来说明您正在尝试做什么?可以在任一方向传递变量,从 PowerShell 到 Batch 或从 Bach 到 PowerShell。我的评估是你有太多的变量,你遇到了逃避问题。此外,您可以将所有内容放入 powershell 脚本中,然后让脚本写出 .bat 文件并执行它。 查看更改后的帖子。感谢您的回复 【参考方案1】:

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

工作示例-

$command = "/k cd .."

Start-Process cmd -ArgumentList $command

【讨论】:

PowerShell 添加任务以使用参数运行 PowerShell 脚本

】PowerShell添加任务以使用参数运行PowerShell脚本【英文标题】:PowerShelladdTasktorunPowerShellscriptwithparameters【发布时间】:2013-05-1909:41:42【问题描述】:我正在尝试从PowerShell脚本将任务添加到任务计划程序,该脚本将运行带有参数... 查看详情

从批处理文件运行 Powershell 脚本中的参数

】从批处理文件运行Powershell脚本中的参数【英文标题】:RunparametersinaPowershellscriptfromabatchfile【发布时间】:2015-01-0302:43:26【问题描述】:我正在尝试从批处理文件中指定powershell脚本中的参数。脚本本身如下所示:Param([Parameter(M... 查看详情

如何使用 Python 中的参数运行 PowerShell 脚本

】如何使用Python中的参数运行PowerShell脚本【英文标题】:HowdoIrunaPowerShellscriptwithparametersfromPython【发布时间】:2019-11-2814:56:13【问题描述】:我正在尝试运行具有Python3.7.3参数的PowerShell脚本,但不知道如何正确调用Popen中的函数... 查看详情

使用命令行参数从 C# 执行 PowerShell 脚本

】使用命令行参数从C#执行PowerShell脚本【英文标题】:ExecutePowerShellScriptfromC#withCommandlineArguments【发布时间】:2010-10-0608:45:24【问题描述】:我需要在C#中执行一个PowerShell脚本。该脚本需要命令行参数。这是我到目前为止所做的... 查看详情

如何通过使用标志或参数启动脚本来跳过部分 PowerShell 脚本

】如何通过使用标志或参数启动脚本来跳过部分PowerShell脚本【英文标题】:HowtoskippartofPowerShellscriptbystartingscriptwithflagsorarguments【发布时间】:2020-02-0806:30:55【问题描述】:我正在编写一个向用户提供功能菜单的脚本,但我也希... 查看详情

如何从 C# 程序运行 PowerShell 脚本?

】如何从C#程序运行PowerShell脚本?【英文标题】:HowtorunaPowerShellscriptfromaC#program?【发布时间】:2018-10-1014:18:50【问题描述】:我有一个PowerShell脚本,它接受用户输入作为用户凭据。它会提示一个窗口,要求输入用户名和密码。... 查看详情

使用 Powershell 在远程计算机上运行脚本

】使用Powershell在远程计算机上运行脚本【英文标题】:UsingPowerShelltorunscriptsonaremotecomputer【发布时间】:2014-07-2607:33:19【问题描述】:我的任务是创建一个可以从命令提示符运行的powershell脚本,该脚本将利用MicrosoftWord(2013)打开... 查看详情

从提示符运行 powershell 脚本时,SQL Server 2008 不使用备份设备路径

】从提示符运行powershell脚本时,SQLServer2008不使用备份设备路径【英文标题】:SQLServer2008doesn\'tusebackupdevicepathwhenrunningpowershellscriptfromprompt【发布时间】:2013-11-1517:16:25【问题描述】:我制作了一个脚本来备份我的远程服务器的... 查看详情

Powershell:使用动态创建的参数运行 msiexec

】Powershell:使用动态创建的参数运行msiexec【英文标题】:Powershell:runmsiexecwithdynamicallycreatedparameters【发布时间】:2012-01-1812:32:26【问题描述】:在我的powershell脚本中,我需要运行msiexec并传递一些参数来初始化它。问题是,如果... 查看详情

通过 npm 脚本从 Powershell 使用 --collectCoverageFrom 运行 Jest 测试时,转义引号和斜杠的正确方法是啥?

】通过npm脚本从Powershell使用--collectCoverageFrom运行Jest测试时,转义引号和斜杠的正确方法是啥?【英文标题】:WhatistheproperwaytoescapequotesandslasheswhenrunningaJesttestwith--collectCoverageFromfromPowershellvianpmscripts?通过npm脚本从Powershell使用--col... 查看详情

Powershell - 运行位于远程机器上的脚本,该脚本反过来从网络共享访问文件

】Powershell-运行位于远程机器上的脚本,该脚本反过来从网络共享访问文件【英文标题】:Powershell-Runascriptlocatedataremotemachinewhichinturnaccessfilesfromanetworkshare【发布时间】:2011-01-0416:43:38【问题描述】:我正在尝试使用以下命令执... 查看详情

使用 PowerShell 函数传递和获取值

】使用PowerShell函数传递和获取值【英文标题】:WorkingwithPowerShellfunctionspassingandgettingvalues【发布时间】:2011-08-2109:42:30【问题描述】:PowerShell脚本从命令行运行。当尝试在脚本中传递参数时,当调用函数以传递值时,没有出现... 查看详情

如何使用脚本块和参数将 powershell.exe 与 -Command 一起使用

】如何使用脚本块和参数将powershell.exe与-Command一起使用【英文标题】:Howtousepowershell.exewith-Commandusingascriptblockandparameters【发布时间】:2017-03-1313:30:31【问题描述】:由于不应该影响当前问题的原因,我需要运行一个脚本,在命... 查看详情

将参数从批处理文件传递到 PowerShell 脚本

】将参数从批处理文件传递到PowerShell脚本【英文标题】:PassparameterfromabatchfiletoaPowerShellscript【发布时间】:2011-09-1514:54:20【问题描述】:在我的批处理文件中,我这样调用PowerShell脚本:powershell.exe"&"G:\\Karan\\PowerShell_Scripts\\ST... 查看详情

将参数从批处理文件传递到 PowerShell 脚本

】将参数从批处理文件传递到PowerShell脚本【英文标题】:PassparameterfromabatchfiletoaPowerShellscript【发布时间】:2011-09-1514:54:20【问题描述】:在我的批处理文件中,我这样调用PowerShell脚本:powershell.exe"&"G:\\Karan\\PowerShell_Scripts\\ST... 查看详情

从 C# 异步运行多个 PowerShell 脚本

】从C#异步运行多个PowerShell脚本【英文标题】:AsynchronouslyrunningmultiplePowerShellscriptsfromC#【发布时间】:2021-08-1617:45:24【问题描述】:简要说明我正在尝试做的事情:我正在开发一个.NetWinForm应用程序,我试图在远程服务器上运行... 查看详情

从 powershell 脚本运行 cmd 命令

】从powershell脚本运行cmd命令【英文标题】:runacmdcommandfrompowershellscript【发布时间】:2021-09-1323:29:24【问题描述】:我正在尝试在PowerShell脚本的后期运行一个名为Graphviz的程序,我必须收集并创建员工信息以制作组织结构图scriptl... 查看详情

如何从批处理文件运行 PowerShell 脚本

】如何从批处理文件运行PowerShell脚本【英文标题】:HowtorunaPowerShellscriptfromabatchfile【发布时间】:2013-10-2012:42:08【问题描述】:我正在尝试在PowerShell中运行此脚本。我已在桌面上将以下脚本保存为ps.ps1。$query="SELECT*FROMWin32_Device... 查看详情