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

[玩转系统] PowerShell:使用计算机信息创建图形桌面快捷方式并链接到票证系统(帮助台)

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

PowerShell:使用计算机信息创建图形桌面快捷方式并链接到票证系统(帮助台)


作为管理员,您不仅负责分配组策略或扮演始终监视用户做什么或不做什么的警察。您的主要职责是确保您的用户能够尽可能舒适地工作。这就是这篇博文的主题。我将向您展示一个创建图形桌面快捷方式图标的代码示例。通过此桌面图标,用户可以获得有关其计算机的信息,如果需要,还可以获得 IT 票务系统的链接。

目标

目标是当用户单击桌面上的快捷方式时显示此屏幕。

[玩转系统] PowerShell:使用计算机信息创建图形桌面快捷方式并链接到票证系统(帮助台)

所有这些都有助于用户可以轻松检索有关其计算机的信息并使用此信息联系帮助台。

这让我进入下一部分。这是关于这个弹出窗口背后的代码。

代码(WindowsForms)

您可以通过两种主要方式在 PowerShell 中创建弹出窗口:VB 和 WindowsForms。我决定使用 Windows 窗体,因此下面的代码是用窗体编写的。

重要笔记

代码在 PowerShell 5.1 和 PowerShell 7 中进行了测试。

您当然想要自定义示例代码。我准备了一些有用的提示和应调整的行的参考。

第9行:编辑头部

第12行:图标编辑左上角(如果图标丢失,则使用默认图标)

第29,30行:编辑链接文本和超链接以帮助系统

最后,这是代码。

将其复制到您最喜欢的编辑器中,例如 PowerShell ISE 或 VS Code。


$ErrorActionPreference = "SilentlyContinue"

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$font = New-Object System.Drawing.Font("Arial", 9)

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Computer-Info'
$form.Size = New-Object System.Drawing.Size(325,225)
$form.StartPosition = 'CenterScreen'
$form.Icon = 'C:\users\patri\OneDrive\Bilder\favicon.ico'

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(120,150)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.TextAlign = 'MiddleCenter'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$LinkLabel = New-Object System.Windows.Forms.LinkLabel
$LinkLabel.Location = New-Object System.Drawing.Point(81,110)
$LinkLabel.Size = New-Object System.Drawing.Size(280,20)
$LinkLabel.Font = $font
$LinkLabel.LinkColor = "BLUE"
$LinkLabel.ActiveLinkColor = "RED"
$LinkLabel.Text = "Ticketsystem"
$LinkLabel.add_Click({[system.Diagnostics.Process]::start("https://a-d.site")})
$Form.Controls.Add($LinkLabel)

$hostn = New-Object System.Windows.Forms.Label
$hostn.Location = New-Object System.Drawing.Point(10,10)
$hostn.Size = New-Object System.Drawing.Size(280,20)
$hostn.Font = $font
$hostn.Text = "Computer: $(hostname)"
$form.Controls.Add($hostn)

$os = New-Object System.Windows.Forms.Label
$os.Location = New-Object System.Drawing.Point(10,90)
$os.Size = New-Object System.Drawing.Size(280,20)
$os.Font = $font
$os.Text = "OS: $((Get-CimInstance win32_operatingsystem).Caption.Trimstart('Microsoft '))"
$form.Controls.Add($os)

$mac = New-Object System.Windows.Forms.Label
$mac.Location = New-Object System.Drawing.Point(10,50)
$mac.Size = New-Object System.Drawing.Size(280,20)
$mac.Font = $font
$mac.Text = "MAC: $((Get-NetAdapter | Where-Object Status -EQ 'up').MacAddress)"
$form.Controls.Add($mac)

$user = New-Object System.Windows.Forms.Label
$user.Location = New-Object System.Drawing.Point(10,30)
$user.Size = New-Object System.Drawing.Size(280,20)
$user.Font = $font
$user.Text = "User: $env:username"
$form.Controls.Add($user)

$ip = New-Object System.Windows.Forms.Label
$ip.Location = New-Object System.Drawing.Point(10,70)
$ip.Size = New-Object System.Drawing.Size(280,20)
$ip.Font = $font
$ip.Text = "IP: $((Get-NetAdapter | Where-Object Status -EQ 'up' | Get-NetIPAddress -AddressFamily IPv4).IPAddress)"
$form.Controls.Add($ip)

$help = New-Object System.Windows.Forms.Label
$help.Location = New-Object System.Drawing.Point(10,110)
$help.Font = $font
$help.Size = New-Object System.Drawing.Size(280,20)
$help.Text = "Help:"
$form.Controls.Add($help)

$form.Topmost = $true

$form.ShowDialog()

现在轮到您通过 GPO 将代码作为链接分发到您的工作站了。

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

取消回复欢迎 发表评论:

关灯