Sta*_*ord 3 windows powershell
如何为 powershell 脚本创建协议处理程序并使目标 powershell 脚本接收命令行参数?
这样做的安全问题是什么?
我想我写了一个体面的指南,因为我在网上找到的信息缺乏一些细节。
首先是安全问题
在您的计算机上运行的任何程序、网站、脚本等都可以触发该协议。没有授权检查。
您不应创建通用协议处理程序。这将是一个巨大的安全问题。我的意思是这将使程序、网站、脚本等能够在您的计算机上运行任何 powershell 脚本或命令。
在 Windows 注册表中创建协议处理程序 协议必须在 Windows 注册表中注册。这是一个简单的任务。
我正在调用我的 powershell 协议处理程序 pwsh
步骤 1:打开注册表编辑器并导航到
Computer\HKEY_CLASSES_ROOT
为了获得灵感,您可以查看Computer\HKEY_CLASSES_ROOT\http该协议处理程序是如何制作的。
第 2 步:创建以下层次结构:
创建密钥pwsh:[Computer\HKEY_CLASSES_ROOT\pwsh]
编辑(Default)to的默认值URL:pwsh。请记住,我调用了我的协议处理程序 for pwsh,写下您调用的任何内容。
添加具有名称URL Protocol和空数据的字符串值。
在pwsh, DefaultIcon:下创建一个新密钥Computer\HKEY_CLASSES_ROOT\pwsh\DefaultIcon。将(Default)数据字段设置为指向图标或图像的文件路径。我使用了 Powershell 7 的 powershell 图标C:\Program Files (x86)\PowerShell\7-preview\assets\ps_black_32x32.ico。
然后创建键shell-> open->command如上图所示。
在 keycommand中将(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 窗口。
第 3 步:创建一个 powershell 脚本来处理协议上的传入操作。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
Run Code Online (Sandbox Code Playgroud)
给定运行命令pwsh:?firstArgument?SecondArgument,脚本将输出:
pwsh:
firstArgument
SecondArgument
Run Code Online (Sandbox Code Playgroud)
使用自动创建自定义协议处理程序的示例代码来补充您的有用指南:
下列:
创建一个自定义 URI 协议custom:(而不是pwsh:,假设 PowerShell 只是用于实现该协议),可以向其传递不限数量的参数。
这样做的唯一的当前用户(HKEY_CURRENT_USER\Software\Classes)默认情况下; 但是,很容易调整代码来为所有用户实现自定义协议( HKEY_LOCAL_MACHINE\Software\Classes),但您需要以提升(以管理员身份)运行代码。
*.ps1自动创建处理程序脚本:
在$env:USERPROFILE\customUriHandler.ps1当前用户场景中。
在$env:ALLUSERPROFILE\customUriHandler.ps1所有用户场景中。
处理程序脚本简单地回显传递给它的参数,并在脚本执行后保持打开的 PowerShell 脚本窗口中调用它( -NoExit);根据需要调整 PowerShell 命令。
协议期望它的参数就好像它是一个shell 命令,即,作为一个以空格分隔的参数列表"...",如果需要,带有参数单独的引用。
最后的示例命令Start-Process用于调用以下 URI,您也可以从Run对话框 ( WinKey-R)提交该 URI,该对话框传递参数one, two & three, four:
custom:one "two & three" fourStart-Process:Start-Process 'custom:one "two & three" four'警告:如果您通过Web 浏览器的地址栏提交此 URI (注意:似乎不适用于 Microsoft Edge),它是 URI 转义的,one%20%22two%20&%20three%22%20four而是传递单个参数,这将需要自定义解析;同样,从文件资源管理器的地址栏提交通过one%20two%20&%20three%20four,但请注意"字符。奇怪的是,在这个过程中迷失了。
# Determine the scope:
# Set to $false to install machine-wide (for all users)
# Note: Doing so then requires running with ELEVATION.
$currentUserOnly = $true
if (-not $currentUserOnly) {
net session *>$null
if ($LASTEXITCODE) { Throw "You must run this script as administrator (elevated)." }
}
$ErrorActionPreference = 'Stop'
# The name of the new protocol scheme
$schemeName = 'custom'
$pwshPathEscaped = (Get-Process -Id $PID).Path -replace '\\', '\\'
$handlerScript = ($env:ALLUSERSPROFILE, $env:USERPROFILE)[$currentUserOnly] + "\${schemeName}UriHandler.ps1"
$handlerScriptEscaped = $handlerScript -replace '\\', '\\'
# Create the protocol handler script.
@'
# Remove the protocol scheme name from the 1st argument.
$argArray = $args.Clone()
$argArray[0] = $argArray[0] -replace '^[^:]+:'
# If the 1st argument is now empty, remove it.
if ('' -eq $argArray[0]) { $argArray = $argArray[1..($argArray.Count-1)] }
"Received $($argArray.Count) argument(s)."
$i = 0
foreach ($arg in $argArray) {
"#$((++$i)): [$arg]"
}
'@ > $handlerScript
# Construct a temp. *.reg file.
# Target the scope-appropriate root registrykey.
$rootKey = ('HKEY_LOCAL_MACHINE\Software\Classes', 'HKEY_CURRENT_USER\Software\Classes')[$currentUserOnly]
# Determine a temp. file path.
$tempFile = [IO.Path]::GetTempPath() + [IO.Path]::GetRandomFileName() + '.reg'
@"
Windows Registry Editor Version 5.00
[$rootKey\$schemeName]
@="URL:$schemeName"
"URL Protocol"=""
[$rootKey\$schemeName\DefaultIcon]
@="$pwshPathEscaped"
[$rootKey\$schemeName\shell]
@="open"
[$rootKey\$schemeName\shell\open\command]
; === Tweak the PowerShell command line here: ===
@="\"$pwshPathEscaped\" -ExecutionPolicy Bypass -NoProfile -NoExit -File \"$handlerScriptEscaped\" %1"
"@ > $tempFile
# Import the *.reg file into the registry.
& {
$ErrorActionPreference = 'Continue'
reg.exe import $tempFile 2>$null
if ($LASTEXITCODE) { Throw "Importing with reg.exe failed: $tempFile"}
}
# Remove the temp. *.reg file.
Remove-Item -ErrorAction Ignore -LiteralPath $tempFile
# ---
# Sample invocation of the new protocol with 3 arguments:
$uri = "$schemeName`:one `"two & three`" four"
Write-Verbose -Verbose "Invoking the following URI: $uri"
Start-Process $uri
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1040 次 |
| 最近记录: |