批量运行 Windows PowerShell,然后让它运行一些文本

Aus*_*tin 3 windows powershell batch command-line windows-8.1

我是制作批处理文件的新手。我正在尝试运行一个命令,该命令将在特定目录中打开 Windows PowerShell,然后在那里运行一个命令。

到目前为止,我有以下内容。Powershell.exe -noexit -command "cd 'c:\Dev\ProductDev'"这似乎可以将 PowerShell 打开到正确的位置,但我无法让它运行命令node sp

我尝试了以下但没有成功 PowerShell -NoExit -Command "Write-Host 'node sp'"

另外,是否可以让它在蓝色显示屏幕而不是 CMD(黑/白)窗口中打开 Windows PowerShell?

Wer*_*nze 7

由于您尚未提供完整的 bat 文件,我猜是

Powershell.exe -noexit -command "cd 'c:\Dev\ProductDev'"
PowerShell -NoExit -Command "Write-Host 'node sp'"
Run Code Online (Sandbox Code Playgroud)

这是错误的做法。您首先启动一个 Powershell,它更改给给定的目录,然后保持打开状态。如果您退出此 powershell,bat 文件将启动第二个 PowerShell。

您需要运行一个 PowerShell 并让它执行这两个命令。一种方法是

Powershell.exe -noexit -command "cd 'c:\Dev\ProductDev'; Write-Host 'node sp'"
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用以下内容编写一个类似于 MyScript.ps1 的 PowerShell 脚本文件:

cd 'c:\Dev\ProductDev'
Write-Host 'node sp'
Run Code Online (Sandbox Code Playgroud)

并启动 PowerShell 不给它执行命令,而是给它执行脚本(另见这个这个StackOverflow 问题)。您可以直接运行此命令或将其放入 bat 文件中,甚至可以将其用作在 lnk 文件中执行的命令:

PowerShell.exe -noexit -ExecutionPolicy Bypass -File "MyScript.ps1"
Run Code Online (Sandbox Code Playgroud)

PowerShell 的一个问题是,默认情况下它不会运行未经(加密)签名的脚本。解决这个问题的简单方法是-ExecutionPolicy Bypass参数。