收集计算机分区信息,去除列中的重复值(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