设为首页收藏本站
查看: 3710|回复: 1

[脚本语言] 恭贺新年,powershell统计硬件信息GDI修改桌面背景

[复制链接]
  • TA的每日心情
    奋斗
    2022-8-5 08:00
  • 签到天数: 265 天

    [LV.8]以坛为家I

    发表于 2022-2-4 11:25:54 | 显示全部楼层 |阅读模式
    本帖最后由 zyfsky666 于 2022-2-4 11:28 编辑

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

    1. cls
    2. Add-Type -AssemblyName System.Windows.Forms
    3. Add-Type -AssemblyName System.Drawing
    4. Add-Type -AssemblyName System.IO
    5. Add-Type -AssemblyName Microsoft.VisualBasic
    6. $SPI_SETDESKWALLPAPER = 20
    7. $API = Add-Type -memberDefinition @"
    8. [DllImport("user32.dll")]public static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, string lpvparam, Int32 fuwinIni);
    9. "@ -passthru -name WindowAPI ###引用系统API
    10. $Currentpath=Split-Path -parent $MyInvocation.MyCommand.Definition #获取脚本目录

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


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

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


    40. #获取CPU名称、单颗CPU核心数量*CPU个数
    41. $cpus = Get-WmiObject -Class win32_processor
    42. $cpunamecore=$null
    43. Foreach ($cpu in $cpus)
    44.   {
    45. $cpunamecore += "$($cpu.name) $($cpu.NumberOfCores)核 $($cpu.NumberOfLogicalProcessors)线程`n`r"
    46.   }
    47. $cpunamecore = $cpunamecore.Substring(0, $cpunamecore.Length-2)
    48. #获取内存大小
    49. $memorys = Get-WmiObject -Class Win32_PhysicalMemory
    50. $memorylist = $null
    51. $memorysize_sum = $null
    52. $memorysize_sum_n=0
    53. Foreach ($memory in $memorys)
    54.   {
    55.   #$memory|Select-Object * |fc
    56.    $memorylist +="品牌:$($memory.Manufacturer.Trim()),"+"主频:$($memory.Speed)MZ,容量:" +($memory.capacity/1024/1024/1024).tostring("F1")+"GB`n`r"
    57.    
    58.   }
    59. $memorylist=$memorylist.Substring(0,$memorylist.Length-2)

    60. #获取磁盘信息
    61. $disks = Get-WmiObject -Class Win32_Diskdrive
    62. $disklist = $null
    63. #$disksize_sum = $null
    64. Foreach ($disk in $disks)
    65.   {

    66.    $disklist += ($disk.deviceid.replace("\\.\PHYSICALDRIVE","Disk") +":型号,$($disk.model) 容量,"  + [int]($disk.size/1024/1024/1024)+"GB`n`r")
    67.    #$disksize_sum+=$disk.size
    68.   }
    69. $disklist= $disklist.Substring(0, $disklist.Length-2)
    70. #获取计算机序列号、制造商
    71. $bios = Get-WmiObject -Class Win32_BIOS
    72. $sn = $bios.SerialNumber
    73. If ($sn.Substring(0,6) -eq "VMware")
    74.    {$sn = "VMware"}
    75. If ($bios.Manufacturer.contains("Dell"))
    76.   {$manufacturer = "Dell"}
    77. Elseif ($bios.Manufacturer.contains("HP"))
    78.   {$manufacturer = "HP"}
    79. Elseif ($bios.Manufacturer.contains("Microsoft"))
    80.   {
    81.    $manufacturer = "Microsoft"
    82.    $sn = ""
    83.    }
    84. Else {$manufacturer = $bios.Manufacturer}
    85. $type = $manufacturer + " " + $model
    86. if ($type.contains("Microsoft Virtual Machine"))
    87.     {$type = "Hyper-V"}
    88. #获取声卡信息
    89. $sound=Get-WmiObject -Class Win32_SoundDevice
    90. $soundnew=$null
    91. foreach ($i in $sound)
    92. {
    93.    $soundnew+="$($i.name)`n`r"  
    94. }
    95. $soundnew=$soundnew.Substring(0,$soundnew.Length-2)
    96. $soundnew=($soundnew -split '\n\r'|Sort-Object -Unique) -join "`n`r"

    97. #获取显卡信息
    98. $view=Get-WmiObject -Class Win32_VideoController
    99. $viewnew=$null
    100. foreach ($item in $view)
    101. {
    102. #判断是否小于1G,显示M
    103. $xc=$item.AdapterRAM/1024/1024/1024
    104. switch ($xc)
    105. {
    106.    
    107.     {$_ -lt 1} {$xcdata="$([math]::Ceiling($item.AdapterRAM/1024/1024))M"}
    108.    
    109.     Default {$xcdata="$([math]::Ceiling($item.AdapterRAM/1024/1024/1024))G"}
    110. }
    111.   $viewnew+= "名称:$($item.Caption),显存大小:$($xcdata)`n`r"
    112. }
    113. $viewnew=$viewnew.Substring(0,$viewnew.Length-2)
    114. #获取主板信息
    115. $basebord=Get-WmiObject -Class Win32_BaseBoard -Property *
    116. $basebordnew="$($basebord.Manufacturer) $($basebord.Product)"
    117. #获取打印机信息
    118. $aa=Get-WmiObject -Class win32_printer -Filter "PortName like '%USB%' and Local=True"|select @{N="printer";e={
    119. if($_.WorkOffline -eq $true){return "$($_.DriverName)(离线)"} else{ return "$($_.DriverName)(在线)"}
    120. }}
    121. $pris=$aa.printer -join ','

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

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

    137. #Write-Host "你的主机最大支持内存:$((Get-WmiObject -Class Win32_PhysicalMemoryarray).MaxCapacity/1024/1024)G" -ForegroundColor Red
    138. }
    139. $txt=mypcinfo -savepath C:\统计.csv
    140. $txt
    141. function showmybackpic
    142. {
    143. param(
    144. [string]$txt="恭迎寅虎乾坤啸,贺送金牛耕耘勤。新年飘下世纪雪,春风遍拂华夏人",
    145. [validatescript({if([System.IO.File]::Exists($_)){return $true}else { throw "找不到背景文件$($_)"}})][string]$picpath,
    146. [string]$fontkind="宋体",
    147. [int]$fontsize=9,
    148. [System.Drawing.Color]$color=[System.Drawing.Color]::White,
    149. [string]$logo
    150. )
    151. $image=[System.Drawing.Image]::FromFile($picpath)
    152. $pic=New-Object -TypeName  System.Drawing.Bitmap($image)
    153. #$pic=[System.Drawing.Bitmap]::new($image)
    154. #$g=[System.Drawing.Graphics]::FromImage($pic)
    155. $sourceWidth = $pic.Width
    156. $sourceHeight=$pic.Height
    157. $size=New-Object -TypeName System.Drawing.Size([System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Width,[System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Height)
    158. #$size=[System.Drawing.Size]::new([System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Width,[System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Height)
    159. #计算宽度的缩放比例
    160. $nPercentW = ([float]$size.Width / [float]$sourceWidth);
    161. #计算高度的缩放比例
    162. $nPercentH = ([float]$size.Height / [float]$sourceHeight);
    163. #期望的宽度
    164. $destWidth = [int]($sourceWidth * $nPercentW);
    165. #期望的高度
    166. $destHeight=[int]($sourceHeight * $nPercentH);
    167. $bitmap=New-Object -TypeName System.Drawing.Bitmap($destWidth, $destHeight)
    168. #$bitmap = [System.Drawing.Bitmap]::new($destWidth, $destHeight);
    169. $g = [System.Drawing.Graphics]::FromImage([System.Drawing.Image]$bitmap)
    170. $g.InterpolationMode=[System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
    171. #绘制图像
    172. $g.DrawImage($pic, 0, 0, $destWidth, $destHeight)
    173. #绘制文字
    174. #$S=[System.Drawing.SolidBrush]::new([System.Drawing.Color]::Chocolate)
    175. #$F=[System.Drawing.Font]::new("宋体",9,[System.Drawing.FontStyle]::Bold)
    176. $S=New-Object -TypeName System.Drawing.SolidBrush($color)
    177. $F=New-Object -TypeName System.Drawing.Font($fontkind,$fontsize,[System.Drawing.FontStyle]::Bold)
    178. $XMaxSize= $g.MeasureString($txt, $F)
    179. $Fwidth = [int]$XMaxSize.Width;
    180. $Fheight = [int]$XMaxSize.Height;
    181. $G.DrawString($txt,$F,$S,$destWidth-$Fwidth-50,20)
    182. #绘制logo
    183. if( ($PSBoundParameters.Keys -contains "logo") -and (Test-Path -Path $logo) ){
    184. $logoimage=[System.Drawing.Image]::FromFile($logo)
    185. #$logopic=[System.Drawing.Bitmap]::new($logoimage)
    186. $logopic=New-Object -TypeName System.Drawing.Bitmap($logoimage)
    187. $g.DrawImage($logoimage,$destWidth-$logopic.Width/2, $destHeight-$logopic.Height/2)
    188. $logopic.Dispose()
    189. }
    190. $info=New-Object -TypeName System.IO.FileInfo($picpath)
    191. $backpic=$info.Directory.FullName +'\' + [System.IO.Path]::GetFileNameWithoutExtension($picpath) +'.bmp'
    192. $bitmap.Save($backpic,[System.Drawing.Imaging.ImageFormat]::Bmp)
    193. $g.Dispose()
    194. $bitmap.Dispose()
    195. #更换桌面背景
    196. $API::SystemParametersInfo($SPI_SETDESKWALLPAPER,0,$backpic,1)   
    197. }
    198. showmybackpic -txt $txt -fontsize 10 -color ([System.Drawing.Color]::Blue) -picpath "$($Currentpath)\一马当先.png"  -logo "$($Currentpath)\logo.png"


    复制代码




    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?注册

    x
  • TA的每日心情
    开心
    2022-10-30 16:37
  • 签到天数: 158 天

    [LV.7]常住居民III

    发表于 2022-2-4 23:00:32 | 显示全部楼层
    非常感谢楼主的分享
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    红盟社区--红客联盟 

    Processed in 0.060384 second(s), 22 queries.

    站点统计| 举报| Archiver| 手机版| 黑屋 |   

    备案号:冀ICP备20006029号-1 Powered by HUC © 2001-2021 Comsenz Inc.

    手机扫我进入移动触屏客户端

    关注我们可获取更多热点资讯

    Honor accompaniments. theme macfee

    快速回复 返回顶部 返回列表