简体   繁体   English

如何使用PowerShell中的参数运行批处理命令

[英]How to run a batch command with parameters from PowerShell

I'm writing up a PowerShell script that will run ElasticSearch's Support Diagnostics tool on demand. 我正在编写一个PowerShell脚本,该脚本将按需运行ElasticSearch的Support Diagnostics工具。

Here is my code: ES.Config.xml - This is an xml file that stores the diagnostic command that I want to run from PowerShell. 这是我的代码:ES.Config.xml-这是一个xml文件,用于存储要从PowerShell运行的诊断命令。 Its stored as a string (See DEV.Diagnostic.ExecuteCommand: 其存储为字符串(请参阅DEV.Diagnostic.ExecuteCommand:

<DEV>
<Diagnostic FolderPath="" ExecuteCommand="C:\ElasticSearch\support-diagnostics-5.12\diagnostics.bat  --host localhost --port 9200  --user elastic -p"  />

In my powershell script I'm trying to run the command as: 在我的Powershell脚本中,我尝试以以下方式运行命令:

Invoke-Expression -Command $global:DiagnosticCommand

$global:DiagnosticCommand is a global variable that stores the value "C:\\ElasticSearch\\support-diagnostics-5.12\\diagnostics.bat --host localhost --port 9200 --user elastic -p" $ global:DiagnosticCommand是一个全局变量,存储值“ C:\\ ElasticSearch \\ support-diagnostics-5.12 \\ diagnostics.bat --host localhost --port 9200 --user elastic -p”

I always get an error when I run this command. 当我运行此命令时,总是出现错误。 Any advice would be appreciated. 任何意见,将不胜感激。

I can't be sure the context you'd use this in, perhaps a few examples would help below. 我不确定您将在其中使用的上下文,也许下面的几个示例会有所帮助。 I've seen two key commandlets which may help you. 我看过两个关键的Commandlet,它们可能会对您有所帮助。 I put in an example which doesn't need an explicit path, though you can point to the full path with parameters for either ArgumentList or -ScriptBlock {enter your full executable and parameters here} 我输入了一个不需要显式路径的示例,尽管您可以指向带有ArgumentList或-ScriptBlock参数的完整路径{在此处输入完整的可执行文件和参数}

Start-Process -FilePath C:\Windows\SysWOW64\mstsc.exe -ArgumentList "/?" 
Start-Process -FilePath msiexec.exe -ArgumentList "/?"

Invoke-Command -ScriptBlock {C:\Windows\SysWOW64\mstsc.exe /?}
Invoke-Command -ScriptBlock {msiexec.exe /?}

In your case can you try the following? 您可以尝试以下方法吗?

Invoke-Command -ScripBlock{
C:\ElasticSearch\support-diagnostics-5.12\diagnostics.bat  --host localhost --port 9200  --user elastic -p}

You can reference the full technet white papers for both commandlets: https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Management/Start-Process?view=powershell-5.1 您可以参考这两个Commandlet的完整Technet白皮书: https ://docs.microsoft.com/zh-cn/powershell/module/Microsoft.PowerShell.Management/Start-Process?view=powershell-5.1

https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Core/Invoke-Command?view=powershell-5.1 https://docs.microsoft.com/zh-cn/powershell/module/Microsoft.PowerShell.Core/Invoke-Command?view=powershell-5.1

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

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