Arrays 获取Uniqe行ID的BitlockerVolume多个因子
我找到了这个 我想用它来制作一个Arrays 获取Uniqe行ID的BitlockerVolume多个因子,arrays,powershell,csv,formatting,Arrays,Powershell,Csv,Formatting,我找到了这个 我想用它来制作一个CSV,它会被更新,并且不会有两次相同的条目。 Get BitlockerVolume没有唯一的行数据,因此有没有一种方法可以使用2个标题/行数据来表示这是第1行,这是第2行等 就像使用标题ComputerName和Mountpoint中的日期一样(ComputerName可能会出现多次,因为某些PC有多个分区,Mountpoint是驱动器号,我想使用这两个信息来标识行。 这可能吗 #Get all AD Computers ATNB* $cn = Get-ADC
CSV
,它会被更新,并且不会有两次相同的条目。
Get BitlockerVolume
没有唯一的行数据,因此有没有一种方法可以使用2个标题/行数据来表示这是第1行,这是第2行等
就像使用标题ComputerName
和Mountpoint
中的日期一样(ComputerName可能会出现多次,因为某些PC有多个分区,Mountpoint是驱动器号,我想使用这两个信息来标识行。
这可能吗
#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
由于使用
Invoke命令时,PSComputerName
作为额外属性可用,因此可以将其用作唯一键
当您可以访问一台计算机并列出其BitLockerVolume时,您将得到一个完整的列表。您基本上希望从CSV文件中删除该计算机的所有卷并添加新的列表
现在我要做的是
- 从所有联机计算机收集所有BitLockerVolume对象
- 从PSComputerName生成唯一的字符串数组
- 导入缓存的CSV文件,筛选出在此数组中具有computername的所有对象
- 从缓存数据+当前数据创建新列表,并覆盖CSV文件
因此,按照这一思路:
$CSV_PATH = "C:\_test\New folder\bitlockercheck.csv"
$current_data = Get-ADComputer -Filter 'Name -like "ATNB*"' | Foreach-Object {
if (Test-Connection $_.Name -BufferSize 32 -Count 1 -Quiet) {
Invoke-Command -ComputerName $_.Name -ScriptBlock {Get-BitLockerVolume}
}
}
$computernames = $current_data | Select-Object -ExpandProperty PSComputerName -Unique
$cached_data = Import-Csv $CSV_PATH | Where-Object {
-not ($computernames -contains $_.PSComputerName)
}
$cached_data + $current_data | Export-Csv $CSV_PATH
你绝对应该在广告服务器上进行筛选。按照你目前的做法,你首先将所有广告计算机下载到你的机器上,然后立即扔掉大部分。通过将筛选字符串传递给Get-ADComputer
,你只会得到你感兴趣的对象,这样效率会更高。你能添加你吗你的Powershell代码到目前为止?我添加了我的代码到目前为止…一次运行就可以了。在第一次运行之后,它只会一直添加两倍的代码…因此我正在寻找一种方法来识别行,并将它们与我从get BitlockerVolumeI获得的新输出进行比较。我问过IRC的一些人,我以前在哪里得到了一些帮助。我添加了“新的”脚本,看起来更符合逻辑。也许你应该继续获得IRC的帮助。