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

[玩转系统] 使用 PowerShell 提取图标

作者:精品下载站 日期:2024-12-14 08:00:29 浏览:13 分类:玩电脑

使用 PowerShell 提取图标


上周我想知道我还可以向我的 Windows 终端设置添加什么。如果您可以从命令行启动控制台,则可能可以为其创建 Windows 终端配置文件。最近,我将 Ruby 和 Python 添加到我的桌面,两者都有交互式控制台。我想,“为什么不将它们添加到 Windows 终端呢?”

我查看了 Ruby 控制台的属性并复制了命令行。这样,添加新的配置文件就很容易了。

{
    // Ruby cmd
    "guid" :"{a7849c4a-db74-4723-adf2-c717357f0a1a}",
    "name" : "Ruby",
    "commandline": "C:\Windows\System32\cmd.exe /E:ON /K C:\Ruby27-x64\bin\setrbvars.cmd",
    "tabTitle": "Ruby Cmd"
},

我没有图标,但 ruby.exe 文件有。我需要一种 PowerShell 方法来提取图标并将其保存到文件中。有很多第三方工具可以处理这项任务,但这有什么乐趣呢?我四处寻找起点,发现了 Josh Duffney 发布在 Spiceworks 上的一段旧代码。他的代码包含我需要的关键部分、提取图标所需的 .NET Framework 类和命令。

受此启发,我编写了这个 PowerShell 脚本。

#requires -version 5.1

# inspired by https://community.spiceworks.com/topic/592770-extract-icon-from-exe-powershell

[CmdletBinding(SupportsShouldProcess)]
Param(
    [Parameter(Position = 0, Mandatory,HelpMessage = "Specify the path to the file.")]
    [ValidateScript({Test-Path $_})]
    [string]$Path,

    [Parameter(HelpMessage = "Specify the folder to save the file.")]
    [ValidateScript({Test-Path $_})]
    [string]$Destination = ".",

    [parameter(HelpMessage = "Specify an alternate base name for the new image file. Otherwise, the source name will be used.")]
    [ValidateNotNullOrEmpty()]
    [string]$Name,

    [Parameter(HelpMessage = "What format do you want to use? The default is png.")]
    [ValidateSet("ico","bmp","png","jpg","gif")]
    [string]$Format = "png"
    )

    Write-Verbose "Starting $($MyInvocation.MyCommand)"

    Try {
        Add-Type -AssemblyName System.Drawing -ErrorAction Stop
    }
    Catch {
        Write-Warning "Failed to import System.Drawing"
        Throw $_
    }

    Switch ($format) {
        "ico" {$ImageFormat = "icon"}
        "bmp" {$ImageFormat = "Bmp"}
        "png" {$ImageFormat = "Png"}
        "jpg" {$ImageFormat = "Jpeg"}
        "gif" {$ImageFormat = "Gif"}
    }

    $file = Get-Item $path
    Write-Verbose "Processing $($file.fullname)"
    #convert destination to file system path
    $Destination = Convert-Path -path $Destination

    if ($Name) {
        $base = $Name
    }
    else {
        $base = $file.BaseName
    }

    #construct the image file name
    $out = Join-Path -Path $Destination -ChildPath "$base.$format"

    Write-Verbose "Extracting $ImageFormat image to $out"
    $ico =  [System.Drawing.Icon]::ExtractAssociatedIcon($file.FullName)

    if ($ico) {
        #WhatIf (target, action)
        if ($PSCmdlet.ShouldProcess($out, "Extract icon")) {
            $ico.ToBitmap().Save($Out,$Imageformat)
            Get-Item -path $out
        }
    }
    else {
        #this should probably never get called
        Write-Warning "No associated icon image found in $($file.fullname)"
    }

    Write-Verbose "Ending $($MyInvocation.MyCommand)"

该脚本将从Path参数指定的文件中提取图标并以指定的格式保存。还有其他格式,但我决定使用最可能的格式的 ValidateSet() 属性。该脚本将根据目标文件名和原始文件名创建输出文件名。或者,您可以指定备用基本名称。我还添加了对 -WhatIf 的支持。

使用此脚本,我可以运行这样的命令来提取图标。

[玩转系统] 使用 PowerShell 提取图标

看起来不错。我将重新运行而不使用 -WhatIf 来提取图标。

[玩转系统] 使用 PowerShell 提取图标

现在,我可以更新我的 Windows 终端设置。

{
    // Ruby cmd
    "guid" :"{a7849c4a-db74-4723-adf2-c717357f0a1a}",
    "name" : "Ruby",
    "commandline": "C:\Windows\System32\cmd.exe /E:ON /K C:\Ruby27-x64\bin\setrbvars.cmd",
    "tabTitle": "Ruby Cmd",
    "icon": "d:\onedrive\windowsterminal\profileicons\ruby.png"
},

[玩转系统] 使用 PowerShell 提取图标

我可以用 Python.exe 做同样的事情。

希望这个对你有帮助。您可以轻松地将其转换为函数。随时欢迎提出意见和问题。

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

取消回复欢迎 发表评论:

关灯