当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 和 WPF 制作短链接

作者:精品下载站 日期:2024-12-14 07:53:00 浏览:15 分类:玩电脑

使用 PowerShell 和 WPF 制作短链接


有时,当我无事可做时,我会在托德·克林特 (Todd Klindt) 和肖恩·杨 (Shane Young) 的播客中为难他们,以此消磨时间。你应该找个时间加入我。不管怎样,在最近的一次节目中,Todd 提到了他整理的一些 PowerShell 代码来解析短链接。您一直在 Twitter 信息流和其他信息流中看到这些内容。该链接真正指向什么? Todd 的解决方案是使用 Invoke-Webrequest 访问链接,然后查看响应中的 uri。我像往常一样获取了他的代码,然后直接运行它到一个简单的 WPF 表单中。

这是托德原始代码的本质。

$short = https://bit.ly/PSMoL3
$wr = Invoke-WebRequest -UseBasicParsing -Uri $short
$wr.baseresponse.ResponseUri.AbsoluteURI

如果运行此命令,您应该会得到结果 https://www.manning.com/books/learn-windows-powershell-in-a-month-of-lunches-third-edition。我想如果有一个表单可以让我从 Twitter 复制并粘贴链接并获取已解析的链接,那可能会很好。因此,我编写了一个基于 WPF 的脚本,该脚本使用堆栈面板来显示一个简单的输入框。

[玩转系统] 使用 PowerShell 和 WPF 制作短链接

单击“确定”运行关联脚本块中的代码,以使用 Invoke-Webrequest 解析链接,然后关闭表单。该函数将结果(一个对象)写回管道。

[玩转系统] 使用 PowerShell 和 WPF 制作短链接

根据已解析的链接,您可能看不到全部内容,但希望足以知道您是否信任它。或者运行 Resolve-ShortLink 函数并将其通过管道传输到 Format-List。

您可以从 GitHub 获取我的函数的代码。

Resolve-ShortLink.ps1:

Function Resolve-ShortLink {
    [cmdletbinding()]
    Param([string]$Url)
    
    Add-Type -AssemblyName PresentationFramework
    Add-Type -AssemblyName PresentationCore
    Add-Type -AssemblyName WindowsBase

    $form = New-Object System.Windows.Window
    $stack = New-object System.Windows.Controls.StackPanel
    
    #define what it looks like
    $form.Title = "Link Lookup"
    $form.Height = 150
    $form.Width = 350

    $label = New-Object System.Windows.Controls.Label
    $label.Content = "    Enter a short url"
    $label.HorizontalAlignment = "left"
    $stack.AddChild($label)

    $inputbox = New-Object System.Windows.Controls.TextBox

    $inputbox.Width = 300
    $inputbox.HorizontalAlignment = "center"
    $inputbox.text = $url

    $stack.AddChild($inputbox)

    $space = new-object System.Windows.Controls.Label
    $space.Height = 10
    $stack.AddChild($space)

    $btn = New-Object System.Windows.Controls.Button
    $btn.Content = "_OK"

    $btn.Width = 65
    $btn.HorizontalAlignment = "center"
    $btn.VerticalAlignment = "bottom"

    #add an event handler
    $btn.Add_click( {
            #write-host "resolving $($inputbox.text)"
            $resolve = (Invoke-WebRequest -UseBasicParsing -Uri $($inputbox.Text)).baseresponse.ResponseUri.AbsoluteURI
            $script:result = [pscustomobject]@{
                ShortUrl = $inputbox.Text
                Resolved = $resolve
            }

            $form.Close()
        })

    $stack.AddChild($btn)
    $space2 = new-object System.Windows.Controls.Label
    $space2.Height = 10
    $stack.AddChild($space2)

    $btn2 = New-Object System.Windows.Controls.Button
    $btn2.Content = "_Cancel"

    $btn2.Width = 65
    $btn2.HorizontalAlignment = "center"
    $btn2.VerticalAlignment = "bottom"

    #add an event handler
    $btn2.Add_click( {
            $script:result = $null
            $form.Close()
        })

    $stack.AddChild($btn2)

    #add the stack to the form
    $form.AddChild($stack)

    #show the form
    $inputbox.Focus() | Out-Null
    $form.WindowStartupLocation = [System.Windows.WindowStartupLocation]::CenterScreen

    $form.ShowDialog() | Out-Null
    if ($script:result) {
        #write result to the pipeline
        $script:result
    } 
}

Set-Alias -Name rsl -Value Resolve-ShortLink

如果您有问题或建议,请在要点页面上发布问题。

我希望你能尝试一下。现在请原谅,我有一些网上的质问要处理。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯