windows - 为 Powershell 脚本创建一个 Potocol 处理程序

标签 windows powershell

如何为 powershell 脚本创建协议(protocol)处理程序并使目标 powershell 脚本接收命令行参数?

这样做有哪些安全问题?

最佳答案

我想我为此写了一个不错的指南,因为我在网上找到的信息缺乏一些细节。

https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa767914(v=vs.85)

首先是安全问题

  • 在您的计算机上运行的任何程序、网站、脚本等都可以触发该协议(protocol)。没有授权检查。
  • 您不应该创建通用协议(protocol)处理程序。这将是一个巨大的安全问题。我的意思是这将使程序、网站、脚本等能够在您的计算机上运行任何 powershell 脚本或命令。

  • 在 Windows 注册表中创建协议(protocol)处理程序
    该协议(protocol)必须在 Windows 注册表中注册。这是一个简单的任务。

    我正在为 pwsh 调用我的 powershell 协议(protocol)处理程序

    第一步:打开注册表编辑器并导航到Computer\HKEY_CLASSES_ROOT
    如需灵感,您可以查看 Computer\HKEY_CLASSES_ROOT\http查看该协议(protocol)处理程序是如何制作的。

    第二步:创建以下层次结构:

    创建 key pwsh : [Computer\HKEY_CLASSES_ROOT\pwsh]
    编辑 (Default) 的默认值至URL:pwsh .请记住,我为 pwsh 调用我的协议(protocol)处理程序,写下你的名字。

    添加名称为 URL Protocol 的字符串值和空数据。

    现在应该是这样的:
    Registry entry for pwsh

    pwsh 下创建一个新 key , DefaultIcon : Computer\HKEY_CLASSES_ROOT\pwsh\DefaultIcon .
    设置(Default)数据字段指向指向图标或图像的文件路径。我使用了 Powershell 7 的 powershell 图标 C:\Program Files (x86)\PowerShell\7-preview\assets\ps_black_32x32.ico .

    然后创建 key shell -> open -> command如上图所示。

    在关键 command更改(Default)数据值到安装 powershell 的位置,然后是要运行的 powershell 脚本。

    测试时我这样做:"C:\Program Files\PowerShell\6\pwsh.exe" -noexit -executionpolicy bypass -Command {Write-Host %1} 注意我使用的是 powershell core 6,你的 powershell 路径可能不同。
    您可以通过打开 run 来测试它是否有效。 Windows (Windows+R) 中的程序。

    Run program with the protocol

    预期的行为是打开带有文本 pwsh:Hello Stackoverflow 的 powershell 窗口。打印。

    Powershell printing out text

    第三步:创建一个 powershell 脚本来处理协议(protocol)上的传入操作。command 的生产就绪数据值关键字:"C:\Program Files\PowerShell\6\pwsh.exe" -noexit -File C:\handleActions.ps1 %1
    Param($Argument="") # If the protocol is ran you always at least get the protocol name as an argument. (if using the %1)
    [String]
    $Argument 
    
    function Handle-Actions { # The cmdlet 'Handle-Actions' uses an unapproved verb. 
        [cmdletBinding()]
        param(
            [Parameter(Mandatory=$false, Position=0)]
            [String]
            $Argument 
        )
        $Argumuments = $Argument.Split([Char]0x003F) # Splits by `?`
        #Argumnets is now in an array, do whatever you need to next.
        $Argumuments | %{
            Write-Host $_ # Writes each argument that was seperated by ? to a line
        }
    }
    
    Handle-Actions -Argument $Argument
    

    给定运行命令 pwsh:?firstArgument?SecondArgument脚本将输出:
    pwsh:
    firstArgument
    SecondArgument
    

    关于windows - 为 Powershell 脚本创建一个 Potocol 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58579374/

    相关文章:

    function - 从 PowerShell 中的另一个函数调用一个函数

    powershell - 在资源组上使用 CpuPercentage 时,Azure PowerShell Add-AlertRule 返回错误请求

    c++ - 如何在 C++/WinAPI 中通过网络适配器获取发送/接收的字节数

    windows - 通过批处理 cmd 重命名/删除注册表中的键

    python - 如何在 Windows 上安装 opencv_contrib 存储库?

    xml - Powershell自动使用新行

    powershell - 从Powershell获得的总和参数

    regex - 使用正则表达式和 PowerShell 将以空格开头的行与上一行合并

    windows - 如何在 Windows 上安装和使用 cURL?

    c++ - 将 C++ .net 代码从 MSVS2005 移植到 MSVS2010 时找不到资源(?)