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

[玩转系统] 适用于 Windows 终端的 PowerShell 帮助程序脚本

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

适用于 Windows 终端的 PowerShell 帮助程序脚本


在过去的几天里,我花了一些时间来整理我的 Windows 终端设置。希望您看到我最近关于备份 settings.json 文件的帖子。我还整理了一些其他简单的 PowerShell 脚本,用于使 Windows 终端更易于使用和管理。

曲目版本

挑战之一是 Windows Terminal 可以自动静默更新。就目前而言,我很欣赏这一点。但是,这也意味着我错过了了解我可能需要了解的变化。我的个人资料设置一团糟,因为我没有及时了解更改。所以我解决了这个问题。

因为我在 Windows 终端中运行每日 PowerShell 7 会话,这似乎是一个不错的起点。不过,该脚本也可以在 Windows PowerShell 5.1 中运行。这个想法是将当前版本号存储在某个地方。我决定创建一个存储在 $Home 中的 json 文件。该脚本将根据存储的先前版本检查当前安装的版本。如果当前版本较新,我会收到一条消息,并且我的浏览器会打开 GitHub 上的“发布”页面,以便我可以查看新增内容。这是脚本。

#Track-WTVersion.ps1

#get the currently installed application
$pkg = Get-AppxPackage -Name Microsoft.WindowsTerminal

#get the version numbert
[version]$current = $pkg.Version

#check for previous version file
$verFile = Join-Path -path $home -ChildPath wtver.json

if (Test-Path $verfile) {
    #compare versions
    $in = (Get-Content -Path $verFile | ConvertFrom-Json)
    $previous = $in.VersionString -as [version]
    # Write-Host "Comparing stored version $previous with current version $current" -ForegroundColor cyan
    If ($current -gt $previous) {
        Write-Host "A newer version of Windows Terminal has been detected." -ForegroundColor Yellow
        #view release notes in your default browser
        Start-Process https://github.com/microsoft/terminal/releases
    }
    else {
     #   Write-Host "Windows Terminal is up to date." -ForegroundColor Green
    }
}

#create the json file, adding the version as a string which makes it easier to reconstruct
$current | Select-Object *, 
@{Name = "VersionString"; Expression = {$_.tostring()}},
@{Name="Date";Expression={(Get-Date).DateTime}} |
ConvertTo-Json | Out-File -FilePath $verfile -Encoding ascii -Force

在我的 PowerShell 配置文件脚本中,如果我检测到我正在 Windows 终端中运行,我会运行此脚本。

#If in Windows Terminal
    if ( $env:wt_session) {
        #dot source my custom prompt function
        . C:\scripts\ps7terminal-prompt.ps1
        #check if there is a new version of Windows Terminal
        C:\scripts\Track-WTVersion.ps1
    }

每当我启动新的 PowerShell 会话时,如果 Windows 终端版本发生更改,我都会收到通知。

打开默认值

Windows 终端使用 defaults.json 文件,您可以使用自己的 settings.json 覆盖该文件。或者换句话说,这些文件本质上与 settings.json 合并,“赢得”任何冲突。我的挑战是想知道默认文件中的内容,尤其是与键绑定有关的内容。我编写了这个非常短的脚本来启动该文件。

#Open-WTDefaults.ps1

# a simple script to open the defaults.json file for Windows Terminal using
# the assoociate application for json files

$json = Join-Path -path (Get-AppxPackage Microsoft.WindowsTerminal).InstallLocation -ChildPath defaults.json
if (Test-Path $json) {
    Invoke-Item $json
}
else {
    Write-Warning "Could not find default.json file."
}

因为我使用的是 Invoke-Item,所以该文件将使用关联的应用程序打开。对于大多数人来说,这可能是 VS Code。您不想编辑此文件。但这使得打开和查看变得很容易。

获取 Windows 终端进程

最后一个脚本来自我在 Windows Terminal GitHub 存储库上阅读的一个问题。关于内存使用情况发表了评论。我很好奇,想出了这个非常短的脚本来获取 Windows 终端进程及其所有子进程。

#requires -version 7.0
#Get-WSProcess.ps1

#get the Windows Terminal process and its children
[cmdletbinding()]
Param()

Get-Process -name WindowsTerminal -OutVariable main
Write-Verbose "Getting processes related to $($main.id)"
(Get-Process).Where({$_.parent.id -eq $main.id})

我确信有不止一种方法可以做到这一点,但这对我来说效果很好。

[玩转系统] 适用于 Windows 终端的 PowerShell 帮助程序脚本

更新:我做了一些更多的测试,这个版本的脚本仅适用于 PowerShell 7。

我希望你和我一样觉得这些很方便。与往常一样,欢迎提出问题和意见。

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

取消回复欢迎 发表评论:

关灯