如何从 cmd 执行 ps1 文件?
how to exec ps1 file from cmd?
我有一个 ps1 文件,Test.ps1,我需要从 cmd 执行它。出于测试目的,此文件只有 1 行:
write "ps1 test successful"
我试图从 cmd 执行这个 ps1 文件。我用谷歌搜索,似乎包含以下行可能会有所帮助:
Set-ExecutionPolicy RemoteSigned
write "ps1 test successful"
但是我仍然无法执行此测试。我试过:
powershell Test
powershell Test.ps1
Test
Test.ps1
cmd 路径上下文设置为 ps1 脚本所在的目录。知道我在这里可能做错了什么吗?
使用
powershell.exe -ExecutionPolicy Bypass -File "C:\dir name\test.ps1"
当然,将C:\dir name\test.ps1
替换为你想要运行的脚本的路径和文件名,用"
(双引号)括起来。
或者,在自己的 window 中启动 PowerShell,然后 运行 脚本。
这个有用吗?
Powershell -ExecutionPolicy Bypass -File .\Test.ps1
我以前用 .bat 文件做过,这是使用的语法。在这种情况下,您 运行 来自与 powershell 脚本相同的目录(否则根据需要调整文件名参数)。如果您还没有,您可能需要 运行 以管理员身份进入 CMD 提示符。
在 macOS 上:
使用 Homebrew 安装 Powershell:
brew install --cask powershell
切换到 Powershell:
pwsh
执行脚本:
./Test.ps1
我的 PowerShell 脚本(测试。ps1):
echo "trying to test something"
我可以用这个命令在cmd中执行它:
.\Test.ps1
我的输出:
trying to test something
我有一个 ps1 文件,Test.ps1,我需要从 cmd 执行它。出于测试目的,此文件只有 1 行:
write "ps1 test successful"
我试图从 cmd 执行这个 ps1 文件。我用谷歌搜索,似乎包含以下行可能会有所帮助:
Set-ExecutionPolicy RemoteSigned
write "ps1 test successful"
但是我仍然无法执行此测试。我试过:
powershell Test
powershell Test.ps1
Test
Test.ps1
cmd 路径上下文设置为 ps1 脚本所在的目录。知道我在这里可能做错了什么吗?
使用
powershell.exe -ExecutionPolicy Bypass -File "C:\dir name\test.ps1"
当然,将C:\dir name\test.ps1
替换为你想要运行的脚本的路径和文件名,用"
(双引号)括起来。
或者,在自己的 window 中启动 PowerShell,然后 运行 脚本。
这个有用吗?
Powershell -ExecutionPolicy Bypass -File .\Test.ps1
我以前用 .bat 文件做过,这是使用的语法。在这种情况下,您 运行 来自与 powershell 脚本相同的目录(否则根据需要调整文件名参数)。如果您还没有,您可能需要 运行 以管理员身份进入 CMD 提示符。
在 macOS 上:
使用 Homebrew 安装 Powershell:
brew install --cask powershell
切换到 Powershell:
pwsh
执行脚本:
./Test.ps1
我的 PowerShell 脚本(测试。ps1):
echo "trying to test something"
我可以用这个命令在cmd中执行它:
.\Test.ps1
我的输出:
trying to test something