随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

收集计算机分区信息,去除列中的重复值(Excel)(空行)

收集计算机分区信息

复制代码
 1 $servers = gc D:\serverlist.txt
 2 $result = $results = $null
 3 $results = @() 
 4 foreach ($server in $servers)
 5 {
 6  If ($server -eq "10")
 7  $UserName = "administrator"
 8  $serverpass ="abcd"
 9  $Password = ConvertTo-SecureString $serverpass -AsPlainText –Force
10  $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)
11  
12  $disks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $server -Credential $cred -Filter "DriveType=3" 
13  foreach ($disk in $disks)
14     {
15      $result = ""|Select Server,DeviceID,FreeSize,TotalSize,Free_Percentage
16      $result.Server = $server
17      $result.DeviceID = $disk.deviceid
18      $result.FreeSize = ($disk.freespace/1GB).tostring("f1")
19      $result.TotalSize = ($disk.size/1GB).tostring("f1")
20      $result.Free_Percentage = "{0:p}" -f ($disk.freespace / $disk.size)
21      $results += $result
22     }
23 }
24  $results |export-csv  D:\disks.csv -Encoding default -NoTypeInformation
复制代码

收集结果如下图所示:

要求去除A列中的重复字段,去除后如下:

代码如下:

复制代码
 1 $dd = import-csv d:\disks.csv
 2 #通过倒序方法去除列中重复值
 3 for ($i=$dd.count-1;$i -ge 0;$i--)
 4 {
 5 $previous = $dd[$i-1].Server
 6 $current = $dd[$i].Server
 7 If ($current -eq $previous)
 8    {
 9     $dd[$i].Server = "" 
10    }
11 }
12 $dd|export-csv d:\disks2.csv -Encoding default -NoTypeInformation
复制代码

 

posted on   momingliu11  阅读(393)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2013-11-11 内存--工作集
2013-11-11 Windows常用性能计数器总结
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示