gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:27:51 24 4
gpt4 key购买 nike

如何为 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com