如何为 powershell 脚本创建协议(protocol)处理程序并使目标 powershell 脚本接收命令行参数?
这样做有哪些安全问题?
最佳答案
我想我为此写了一个不错的指南,因为我在网上找到的信息缺乏一些细节。
https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa767914(v=vs.85)
首先是安全问题
在 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
的字符串值和空数据。现在应该是这样的:
在
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) 中的程序。预期的行为是打开带有文本
pwsh:Hello Stackoverflow
的 powershell 窗口。打印。第三步:创建一个 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/