【发布时间】:2018-06-06 10:03:34
【问题描述】:
我找到了这个https://itpro.outsidesys.com/2017/10/21/powershell-change-values-in-csv-data/
我想用它来制作一个CSV,它会被更新并且没有两次相同的条目。
Get-BitlockerVolume 没有唯一的行数据,所以有没有办法可以使用 2 个标题/行数据来表示这是 ROW1,这是 ROW2 等等。
就像使用标题中的日期 ComputerName 和 Mountpoint 一样(由于某些 PC 有多个分区,计算机名可能会多次出现,MountPoint 是驱动器号,我想使用这 2 个信息来识别行。
这可能吗?
#Get all AD Computers ATNB*
$cn = Get-ADComputer -Filter * | Where-Object {$_.Name -like "ATNB*"} | Select -Property Name
foreach ($pc in $cn.name){
#Test connection
$connection = test-connection -buffersize 32 -count 1 -ComputerName $pc -quiet
if ($connection -eq $True) {
#Run command on pc esport to csv add new row
invoke-command -computername $cn.name -scriptblock {get-bitlockervolume} | export-csv -Append "C:\_test\New folder\bitlockercheck.csv"
echo "$pc Info added to CSV"
}
else {echo "$pc is not online"}
}
这是新的/更新的脚本:
$csv = @(Import-Csv "C:\_test\New folder\bvt_2.csv")
#Get all AD Computers ATNB* and start looping
Get-ADComputer -Filter { Name -like "ATNB*" } | ForEach-Object {
# Assign the "Name" value of the current computer to $pc
$pc = $_.Name
# Test connection
if (Test-Connection -buffersize 32 -count 1 -ComputerName $pc -quiet) {
#Run command on pc esport to csv add new row
Invoke-Command -Computername $pc -scriptblock { Get-BitlockerVolume } | ForEach-Object {
# For each bit locker volume...
$volume = $_
# ... see if a row exists for the volume
$csvRow = $csv | Where-Object { $_.ComputerName -eq $pc -and $_.MountPoint -eq $volume.MountPoint }
# if a row exists, update the Data column only
if ($csvRow) {
$csvRow.Data = $volume.Data
echo "$pc Info updated in CSV"
} else {
# Otherwise add a new row to the CSV
$csv += [PSCustomObject]@{
ComputerName = $pc
MountPoint = $volume.MountPoint
Data = $volume.Data
}
echo "$pc Info added to CSV"
}
}
} else {
echo "$pc is not online"
}
}
# Export the updated CSV file
$csv | Export-Csv "C:\_test\New folder\bvt_2.csv" -NoTypeInformation
【问题讨论】:
-
到目前为止,您能添加您的 Powershell 代码吗?
-
到目前为止我已经添加了我的代码... 1 次运行没问题。在第一次运行之后它只会一直添加双打......所以我正在寻找一种方法来识别行并将它们与我从 Get-BitlockerVolume 获得的新输出进行比较
标签: arrays powershell csv formatting