windows 如何从批处理文件运行 PowerShell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19335004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a PowerShell script from a batch file
提问by Eka
I am trying to run this script in PowerShell. I have saved the below script as ps.ps1
on my desktop.
我正在尝试在 PowerShell 中运行此脚本。我已将以下脚本保存ps.ps1
在我的桌面上。
$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"
Register-WMIEvent -Query $query -Action { invoke-item "C:\Program Files\abc.exe"}
I have made a batch script to run this PowerShell script
我制作了一个批处理脚本来运行这个 PowerShell 脚本
@echo off
Powershell.exe set-executionpolicy remotesigned -File C:\Users\SE\Desktop\ps.ps1
pause
But I am getting this error:
但我收到此错误:
回答by Joey
You need the -ExecutionPolicy
parameter:
你需要-ExecutionPolicy
参数:
Powershell.exe -executionpolicy remotesigned -File C:\Users\SE\Desktop\ps.ps1
Otherwise PowerShell considers the arguments a line to execute and while Set-ExecutionPolicy
isa cmdlet, it has no -File
parameter.
否则 PowerShell会将参数视为要执行的一行,而它Set-ExecutionPolicy
是一个 cmdlet,它没有-File
参数。
回答by deadlydog
I explain both why you would want to call a PowerShell script from a batch file and how to do it in my blog post here.
我在我的博客文章中解释了为什么要从批处理文件中调用 PowerShell 脚本以及如何执行此操作。
This is basically what you are looking for:
这基本上就是你要找的:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Users\SE\Desktop\ps.ps1'"
And if you need to run your PowerShell script as an admin, use this:
如果您需要以管理员身份运行 PowerShell 脚本,请使用以下命令:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Users\SE\Desktop\ps.ps1""' -Verb RunAs}"
Rather than hard-coding the entire path to the PowerShell script though, I recommend placing the batch file and PowerShell script file in the same directory, as my blog post describes.
不过,我建议将批处理文件和 PowerShell 脚本文件放在同一目录中,而不是硬编码 PowerShell 脚本的整个路径,正如我的博客文章所述。
回答by e.gad
If you want to run from the current directory without a fully qualified path, you can use:
如果要在没有完全限定路径的情况下从当前目录运行,可以使用:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& './ps.ps1'"
回答by Root Loop
If you run a batch file calling PowerShell as a administrator, you better run it like this, saving you all the trouble:
如果你以管理员身份运行一个调用 PowerShell 的批处理文件,你最好这样运行它,省去你所有的麻烦:
powershell.exe -ExecutionPolicy Bypass -Command "Path\xxx.ps1"
It is better to use Bypass
...
最好使用Bypass
...
回答by Nux
If you want to run a few scripts, you can use Set-executionpolicy -ExecutionPolicy Unrestricted
and then reset with Set-executionpolicy -ExecutionPolicy Default
.
如果你想运行几个脚本,你可以使用Set-executionpolicy -ExecutionPolicy Unrestricted
然后用 .reset 重置Set-executionpolicy -ExecutionPolicy Default
。
Note that execution policy is only checked when you start its execution (or so it seems) and so you can run jobs in the background and reset the execution policy immediately.
请注意,仅在您开始执行时(或看起来如此)才会检查执行策略,因此您可以在后台运行作业并立即重置执行策略。
# Check current setting
Get-ExecutionPolicy
# Disable policy
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
# Choose [Y]es
Start-Job { cd c:\working\directory\with\script\ ; ./ping_batch.ps1 example.com | tee ping__example.com.txt }
Start-Job { cd c:\working\directory\with\script\ ; ./ping_batch.ps1 google.com | tee ping__google.com.txt }
# Can be run immediately
Set-ExecutionPolicy -ExecutionPolicy Default
# [Y]es
回答by Dobbelina
Another easy way to execute a ps script from batch is to simply incorporate it between the ECHO and the Redirection characters,(> and >>), example:
从批处理中执行 ps 脚本的另一种简单方法是将它简单地合并在 ECHO 和重定向字符之间,(> 和 >>),例如:
@echo off
set WD=%~dp0
ECHO New-Item -Path . -Name "Test.txt" -ItemType "file" -Value "This is a text string." -Force > "%WD%PSHELLFILE.ps1"
ECHO add-content -path "./Test.txt" -value "`r`nThe End" >> "%WD%PSHELLFILE.ps1"
powershell.exe -ExecutionPolicy Bypass -File "%WD%PSHELLFILE.ps1"
del "%WD%PSHELLFILE.ps1"
Last line deletes the created temp file.
最后一行删除创建的临时文件。
回答by DimoN
Small sample test.cmd
小样本test.cmd
<# :
@echo off
powershell /nologo /noprofile /command ^
"&{[ScriptBlock]::Create((cat """%~f0""") -join [Char[]]10).Invoke(@(&{$args}%*))}"
exit /b
#>
Write-Host Hello, $args[0] -fo Green
#You programm...
回答by Neil 11111
If your PowerShell login script is running after 5 minutes (as mine was) on a 2012 server, there is a GPO setting on a server - 'Configure Login script Delay' the default setting 'not configured' this will leave a 5-minute delay before running the login script.
如果您的 PowerShell 登录脚本在 2012 年服务器上运行 5 分钟(就像我的一样),则服务器上有一个 GPO 设置-“配置登录脚本延迟”默认设置“未配置”这将留下 5 分钟的延迟在运行登录脚本之前。