通过cmd使用命令启动powershell

通过cmd使用命令启动powershell,powershell,command-line,cmd,Powershell,Command Line,Cmd,如何通过cmd使用powershell启动带有参数的程序 :) 现在,当用“”程序上的carts替换“”时,我得到了变量的文本表示形式 -argumentlist "-adminhost $($_.machinename) -servername $($_.name)" 是否可以编写这样的命令powershell使用参数启动进程?您最好的选择是将powershell代码写入文件并运行以下命令: powershell.exe -File "C:\path\to\your.ps1" 如果必须使用

如何通过cmd使用powershell启动带有参数的程序 :)

现在,当用“”程序上的carts替换“”时,我得到了变量的文本表示形式

-argumentlist "-adminhost $($_.machinename) -servername $($_.name)"

是否可以编写这样的命令powershell使用参数启动进程?

您最好的选择是将powershell代码写入文件并运行以下命令:

powershell.exe -File "C:\path\to\your.ps1"
如果必须使用嵌套双引号执行命令字符串,则需要使用反斜杠转义嵌套双引号:

powershell.exe -Command "&{Write-Output \"foo bar\"}"
就你而言:

powershell.exe -Command "&{Get-Service -ComputerName server1,server2 -DisplayName 'instance *'|where {$_.Status -eq 'Running'}|ForEach-Object {Start-Process -FilePath 'C:\monitor.exe' -ArgumentList \"-adminhost $($_.MachineName) -servername $($_.Name)\"}}"
或者,由于各个参数似乎不包含空格,因此可以将参数列表作为实际列表/数组传递:

powershell.exe -Command "&{Get-Service -ComputerName server1,server2 -DisplayName 'instance *'|where {$_.Status -eq 'Running'}|ForEach-Object {Start-Process -FilePath 'C:\monitor.exe' -ArgumentList '-adminhost', $_.MachineName, '-servername', $_.Name}}"

您最好的选择是将PowerShell代码写入一个文件并运行:

powershell.exe -File "C:\path\to\your.ps1"
如果必须使用嵌套双引号执行命令字符串,则需要使用反斜杠转义嵌套双引号:

powershell.exe -Command "&{Write-Output \"foo bar\"}"
就你而言:

powershell.exe -Command "&{Get-Service -ComputerName server1,server2 -DisplayName 'instance *'|where {$_.Status -eq 'Running'}|ForEach-Object {Start-Process -FilePath 'C:\monitor.exe' -ArgumentList \"-adminhost $($_.MachineName) -servername $($_.Name)\"}}"
或者,由于各个参数似乎不包含空格,因此可以将参数列表作为实际列表/数组传递:

powershell.exe -Command "&{Get-Service -ComputerName server1,server2 -DisplayName 'instance *'|where {$_.Status -eq 'Running'}|ForEach-Object {Start-Process -FilePath 'C:\monitor.exe' -ArgumentList '-adminhost', $_.MachineName, '-servername', $_.Name}}"