cmd到批处理脚本中的powershell转换


1

有没有办法编写一个批处理脚本,通过C:\>cmd.exe a.bat切换到powershell.exe并运行剩余的代码行来执行?

1. | echo "Hi. I'm cmd.exe."
2. | powershell
3. | echo "Now, I'm PowerShell.exe! Look:"
4. | get-random

这只是在等待输入的第2行之后停止


请参阅此StackOverflow问题:如何从批处理文件执行powershell命令?
Siim K

所以,看起来只是简单地说:powershell -Command "& {echo "Now, I''m PowerShell.exe! Look:";get-random}"谢谢!
Firebowl2000

Answers:


0

您可以编写.bat文件并将其用于第一行:

;@Findstr -bv ;@F "%~f0" | powershell -noprofile -command - & goto:eof

此行将当前文件(“%~f0”)中所有不以“; @F”开头的行传递给要执行的PowerShell。其余的行将是PowerShell代码。这是一个例子。

;@Findstr -bv ;@F "%~f0" | powershell -noprofile -command - & goto:eof

$procCount = (Get-Process).Count
Write-Host "There are $procCount processes running right now." -fore green
Start-Sleep 5
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.