如何从批处理文件运行PowerShell脚本

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参数.

  • @joey,它工作正常,谢谢..但是运行bat文件后,出现此错误“ Waring:column'command'不适合显示并已删除” (2认同)
  • @Joey Haha,如此有效,您无需管理员即可覆盖此策略。那是安全问题吗? (2认同)
  • @KolobCanyon:如果您可以运行PowerShell,那么您还可以执行其他所有操作。请注意,执行策略并不意味着PowerShell具有比其他方式更多的特权。在某种程度上,这只是避免意外运行您可能不想运行的东西的便利。类似于必须在当前目录中的命令前加上`。/`并在Unix上具有可执行标志。 (2认同)

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脚本文件放在同一目录中,正如我的博客文章所描述的那样.

  • 将参数传递给该Commandlet怎么办? (2认同)

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)


Ely*_*755 6

也发布在这里: 如何在批处理文件中运行 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)

正如我之前所解释的。