Eka*_*Eka 181 windows shell powershell batch-file
我试图在PowerShell中运行此脚本.我已将以下脚本保存在ps.ps1
桌面上.
$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"
Register-WMIEvent -Query $query -Action { invoke-item "C:\Program Files\abc.exe"}
Run Code Online (Sandbox Code Playgroud)
我已经制作了一个批处理脚本来运行这个PowerShell脚本
@echo off
Powershell.exe set-executionpolicy remotesigned -File C:\Users\SE\Desktop\ps.ps1
pause
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误:
Joe*_*oey 243
你需要-ExecutionPolicy
参数:
Powershell.exe -executionpolicy remotesigned -File C:\Users\SE\Desktop\ps.ps1
Run Code Online (Sandbox Code Playgroud)
否则,PowerShell 会将参数视为要执行的行,而Set-ExecutionPolicy
对于 cmdlet,它没有-File
参数.
dea*_*dog 106
我解释了为什么要从批处理文件中调用PowerShell脚本以及如何在我的博客文章中执行此操作.
这基本上就是你要找的东西:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Users\SE\Desktop\ps.ps1'"
Run Code Online (Sandbox Code Playgroud)
如果您需要以管理员身份运行PowerShell脚本,请使用以下命令:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Users\SE\Desktop\ps.ps1""' -Verb RunAs}"
Run Code Online (Sandbox Code Playgroud)
虽然不是硬编码PowerShell脚本的整个路径,但我建议将批处理文件和PowerShell脚本文件放在同一目录中,正如我的博客文章所描述的那样.
Roo*_*oop 13
如果您运行以管理员身份调用PowerShell的批处理文件,最好像这样运行它,为您节省所有麻烦:
powershell.exe -ExecutionPolicy Bypass -Command "Path\xxx.ps1"
Run Code Online (Sandbox Code Playgroud)
最好使用Bypass
......
e.g*_*gad 13
如果要从没有完全限定路径的当前目录运行,可以使用:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& './ps.ps1'"
Run Code Online (Sandbox Code Playgroud)
小智 6
小样本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...
Run Code Online (Sandbox Code Playgroud)
也发布在这里: 如何在批处理文件中运行 powershell 命令
按照此线程:
https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/converting-powershell-to-batch
您可以使用此 PowerShell 函数轻松将任何 PowerShell 脚本转换为批处理文件:
function Convert-PowerShellToBatch
{
param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]
[Alias("FullName")]
$Path
)
process
{
$encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $Path -Raw -Encoding UTF8)))
$newPath = [Io.Path]::ChangeExtension($Path, ".bat")
"@echo off`npowershell.exe -NoExit -encodedCommand $encoded" | Set-Content -Path $newPath -Encoding Ascii
}
}
Run Code Online (Sandbox Code Playgroud)
要转换目录中的所有PowerShell 脚本,只需运行以下命令:
Get-ChildItem -Path <DIR-PATH> -Filter *.ps1 |
Convert-PowerShellToBatch
Run Code Online (Sandbox Code Playgroud)
所需文件夹的路径在哪里。例如:
Get-ChildItem -Path "C:\path\to\powershell\scripts" -Filter *.ps1 |
Convert-PowerShellToBatch
Run Code Online (Sandbox Code Playgroud)
要转换单个PowerShell 脚本,只需运行以下命令:
Get-ChildItem -Path <FILE-PATH> |
Convert-PowerShellToBatch
Run Code Online (Sandbox Code Playgroud)
所需文件的路径在哪里。
转换后的文件位于源目录中。即<FILE-PATH>或<DIR-PATH>。
将它们放在一起:
创建一个 .ps1 文件(PowerShell 脚本),其中包含以下代码:
function Convert-PowerShellToBatch
{
param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]
[Alias("FullName")]
$Path
)
process
{
$encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $Path -Raw -Encoding UTF8)))
$newPath = [Io.Path]::ChangeExtension($Path, ".bat")
"@echo off`npowershell.exe -NoExit -encodedCommand $encoded" | Set-Content -Path $newPath -Encoding Ascii
}
}
# change <DIR> to the path of the folder in which the desired powershell scripts are.
# the converted files will be created in the destination path location (in <DIR>).
Get-ChildItem -Path <DIR> -Filter *.ps1 |
Convert-PowerShellToBatch
Run Code Online (Sandbox Code Playgroud)
不要忘记,如果您只想转换一个文件而不是多个文件,您可以替换以下内容
Get-ChildItem -Path <DIR> -Filter *.ps1 |
Convert-PowerShellToBatch
Run Code Online (Sandbox Code Playgroud)
有了这个:
Get-ChildItem -Path <FILE-PATH> |
Convert-PowerShellToBatch
Run Code Online (Sandbox Code Playgroud)
正如我之前所解释的。