zyfsky666 发表于 2022-2-4 11:25:54

恭贺新年,powershell统计硬件信息GDI修改桌面背景

本帖最后由 zyfsky666 于 2022-2-4 11:28 编辑

简单说,自动修改桌面背景图显示硬件信息,并可添加水印图片,祝大家虎年大吉,马到成功!

cls
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.IO
Add-Type -AssemblyName Microsoft.VisualBasic
$SPI_SETDESKWALLPAPER = 20
$API = Add-Type -memberDefinition @"
public static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, string lpvparam, Int32 fuwinIni);
"@ -passthru -name WindowAPI ###引用系统API
$Currentpath=Split-Path -parent $MyInvocation.MyCommand.Definition #获取脚本目录

function Mypcinfo
{
param($savepath)
cls
$system = Get-WmiObject -Class Win32_ComputerSystem
#获取计算机域名、型号
$domainname = $system.Domain
$model = $system.Model
$user=$system.UserName
#获取计算机IP地址,取IP不为空的网卡IP地址
$ips = gwmi Win32_NetworkAdapterConfiguration |?{ $_.IPAddress -ne $null}
$ipdata=$null
foreach ($ip in $ips )
{
#查找对应网速
$ok=Get-WmiObject -Class Win32_NetworkAdapter -Filter "Description='$($ip.Description)'"|Select-Object Description,speed,MACAddress
$ipdata+="网卡名称:$($ip.Description) IP地址:$($ip.IPAddress)`r`n 速率:$($ok.speed/1000/1000)M MAC地址:$($ok.MACAddress)`n`r"
}
$ipdata=$ipdata.Substring(0,$ipdata.Length-2)


#获取操作系统版本
$os = Get-WmiObject -Class Win32_OperatingSystem

#获取操作系统版本
$os_caption =$os.Caption
If ($os_caption.Contains("Server 2008 R2 Enterprise"))
{$os_caption_s = "Win2008"}
ElseIf ($os_caption.Contains("Server 2003 Enterprise"))
{$os_caption_s = "Win2003"}
Else {$os_caption_s = $os.Caption}
$osversion = $os_caption_s + " " + $os.OSArchitecture.Substring(0,2) + "bit"


#获取CPU名称、单颗CPU核心数量*CPU个数
$cpus = Get-WmiObject -Class win32_processor
$cpunamecore=$null
Foreach ($cpu in $cpus)
{
$cpunamecore += "$($cpu.name) $($cpu.NumberOfCores)核 $($cpu.NumberOfLogicalProcessors)线程`n`r"
}
$cpunamecore = $cpunamecore.Substring(0, $cpunamecore.Length-2)
#获取内存大小
$memorys = Get-WmiObject -Class Win32_PhysicalMemory
$memorylist = $null
$memorysize_sum = $null
$memorysize_sum_n=0
Foreach ($memory in $memorys)
{
#$memory|Select-Object * |fc
   $memorylist +="品牌:$($memory.Manufacturer.Trim()),"+"主频:$($memory.Speed)MZ,容量:" +($memory.capacity/1024/1024/1024).tostring("F1")+"GB`n`r"
   
}
$memorylist=$memorylist.Substring(0,$memorylist.Length-2)

#获取磁盘信息
$disks = Get-WmiObject -Class Win32_Diskdrive
$disklist = $null
#$disksize_sum = $null
Foreach ($disk in $disks)
{

   $disklist += ($disk.deviceid.replace("\\.\PHYSICALDRIVE","Disk") +":型号,$($disk.model) 容量,"+ ($disk.size/1024/1024/1024)+"GB`n`r")
   #$disksize_sum+=$disk.size
}
$disklist= $disklist.Substring(0, $disklist.Length-2)
#获取计算机序列号、制造商
$bios = Get-WmiObject -Class Win32_BIOS
$sn = $bios.SerialNumber
If ($sn.Substring(0,6) -eq "VMware")
   {$sn = "VMware"}
If ($bios.Manufacturer.contains("Dell"))
{$manufacturer = "Dell"}
Elseif ($bios.Manufacturer.contains("HP"))
{$manufacturer = "HP"}
Elseif ($bios.Manufacturer.contains("Microsoft"))
{
   $manufacturer = "Microsoft"
   $sn = ""
   }
Else {$manufacturer = $bios.Manufacturer}
$type = $manufacturer + " " + $model
if ($type.contains("Microsoft Virtual Machine"))
    {$type = "Hyper-V"}
#获取声卡信息
$sound=Get-WmiObject -Class Win32_SoundDevice
$soundnew=$null
foreach ($i in $sound)
{
   $soundnew+="$($i.name)`n`r"
}
$soundnew=$soundnew.Substring(0,$soundnew.Length-2)
$soundnew=($soundnew -split '\n\r'|Sort-Object -Unique) -join "`n`r"

#获取显卡信息
$view=Get-WmiObject -Class Win32_VideoController
$viewnew=$null
foreach ($item in $view)
{
#判断是否小于1G,显示M
$xc=$item.AdapterRAM/1024/1024/1024
switch ($xc)
{
   
    {$_ -lt 1} {$xcdata="$(::Ceiling($item.AdapterRAM/1024/1024))M"}
   
    Default {$xcdata="$(::Ceiling($item.AdapterRAM/1024/1024/1024))G"}
}
$viewnew+= "名称:$($item.Caption),显存大小:$($xcdata)`n`r"
}
$viewnew=$viewnew.Substring(0,$viewnew.Length-2)
#获取主板信息
$basebord=Get-WmiObject -Class Win32_BaseBoard -Property *
$basebordnew="$($basebord.Manufacturer) $($basebord.Product)"
#获取打印机信息
$aa=Get-WmiObject -Class win32_printer -Filter "PortName like '%USB%' and Local=True"|select @{N="printer";e={
if($_.WorkOffline -eq $true){return "$($_.DriverName)(离线)"} else{ return "$($_.DriverName)(在线)"}
}}
$pris=$aa.printer -join ','

#获取显示器信息
$Screen=Get-WmiObject -Class Win32_DesktopMonitor|Select-Object @{l="显示器";e={$($_.PNPDeviceID -split '\\')}}
$Scr=$Screen.显示器 -join ','

$Infors= @{计算机名=$env:ComputerName;当前用户=$user;网络=$ipdata;系统版本= $osversion;工作组或域=$domainname;内存=$memorylist;
CPU=$cpunamecore;硬盘=$disklist;BIOS版本=$type;主板制造商=$basebordnew;声卡=$soundnew;显卡=$viewnew;显示器=$Scr;打印机=$pris}
$news=$Infors
#更新CSV
if(Test-Path $savepath){
$list=Import-Csv $savepath-Encoding Default
if($list.计算机名.IndexOf($news.计算机名) -eq -1){
$news|Export-Csv -NoTypeInformation $savepath -Encoding Default -Append -Force
}
}else{
$news|Export-Csv -NoTypeInformation $savepath -Encoding Default -Force}
return ($news.psobject.Members|?{$_.MemberType -eq "NoteProperty"}|%{"$($_.Name):$($_.Value)"}) -join "`r`n"

#Write-Host "你的主机最大支持内存:$((Get-WmiObject -Class Win32_PhysicalMemoryarray).MaxCapacity/1024/1024)G" -ForegroundColor Red
}
$txt=mypcinfo -savepath C:\统计.csv
$txt
function showmybackpic
{
param(
$txt="恭迎寅虎乾坤啸,贺送金牛耕耘勤。新年飘下世纪雪,春风遍拂华夏人",
::Exists($_)){return $true}else { throw "找不到背景文件$($_)"}})]$picpath,
$fontkind="宋体",
$fontsize=9,
$color=::White,
$logo
)
$image=::FromFile($picpath)
$pic=New-Object -TypeNameSystem.Drawing.Bitmap($image)
#$pic=::new($image)
#$g=::FromImage($pic)
$sourceWidth = $pic.Width
$sourceHeight=$pic.Height
$size=New-Object -TypeName System.Drawing.Size(::PrimaryScreen.WorkingArea.Width,::PrimaryScreen.WorkingArea.Height)
#$size=::new(::PrimaryScreen.WorkingArea.Width,::PrimaryScreen.WorkingArea.Height)
#计算宽度的缩放比例
$nPercentW = ($size.Width / $sourceWidth);
#计算高度的缩放比例
$nPercentH = ($size.Height / $sourceHeight);
#期望的宽度
$destWidth = ($sourceWidth * $nPercentW);
#期望的高度
$destHeight=($sourceHeight * $nPercentH);
$bitmap=New-Object -TypeName System.Drawing.Bitmap($destWidth, $destHeight)
#$bitmap = ::new($destWidth, $destHeight);
$g = ::FromImage($bitmap)
$g.InterpolationMode=::HighQualityBicubic
#绘制图像
$g.DrawImage($pic, 0, 0, $destWidth, $destHeight)
#绘制文字
#$S=::new(::Chocolate)
#$F=::new("宋体",9,::Bold)
$S=New-Object -TypeName System.Drawing.SolidBrush($color)
$F=New-Object -TypeName System.Drawing.Font($fontkind,$fontsize,::Bold)
$XMaxSize= $g.MeasureString($txt, $F)
$Fwidth = $XMaxSize.Width;
$Fheight = $XMaxSize.Height;
$G.DrawString($txt,$F,$S,$destWidth-$Fwidth-50,20)
#绘制logo
if( ($PSBoundParameters.Keys -contains "logo") -and (Test-Path -Path $logo) ){
$logoimage=::FromFile($logo)
#$logopic=::new($logoimage)
$logopic=New-Object -TypeName System.Drawing.Bitmap($logoimage)
$g.DrawImage($logoimage,$destWidth-$logopic.Width/2, $destHeight-$logopic.Height/2)
$logopic.Dispose()
}
$info=New-Object -TypeName System.IO.FileInfo($picpath)
$backpic=$info.Directory.FullName +'\' + ::GetFileNameWithoutExtension($picpath) +'.bmp'
$bitmap.Save($backpic,::Bmp)
$g.Dispose()
$bitmap.Dispose()
#更换桌面背景
$API::SystemParametersInfo($SPI_SETDESKWALLPAPER,0,$backpic,1)   
}
showmybackpic -txt $txt -fontsize 10 -color (::Blue) -picpath "$($Currentpath)\一马当先.png"-logo "$($Currentpath)\logo.png"






wawdgj 发表于 2022-2-4 23:00:32

非常感谢楼主的分享
页: [1]
查看完整版本: 恭贺新年,powershell统计硬件信息GDI修改桌面背景