【问题标题】:Get-BitlockerVolume Multiple factors for Uniqe row IDGet-BitlockerVolume Uniqe 行 ID 的多个因素
【发布时间】: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 等等。

就像使用标题中的日期 ComputerNameMountpoint 一样(由于某些 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


【解决方案1】:

由于您在使用Invoke-Command 时将PSComputerName 用作额外属性,因此您可以将其用作唯一键。

当您可以访问一台计算机并列出其 BitLockerVolume 时,您将获得一个完整的列表。您基本上想从 CSV 文件中删除该计算机的所有卷并添加新列表。

现在我要做的是

  • 从所有在线计算机收集所有 BitLockerVolume 对象
  • 从 PSComputerName 创建一个唯一的字符串数组
  • 导入缓存的 CSV 文件,过滤掉所有计算机名在此数组中的对象
  • 从缓存数据 + 当前数据创建一个新列表并覆盖 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

您绝对应该在 AD 服务器上进行过滤。按照您目前的做法,您首先将所有 AD 计算机下载到您的机器上,然后立即将其中的大部分丢弃。通过将过滤器字符串传递给Get-ADComputer,您将只会获得您感兴趣的那些对象,这样效率更高。

【讨论】:

  • 我曾向 IRC 的一些人请教过,我曾在他们那里获得过一些帮助。我添加了“新”脚本,看起来更合乎逻辑。
  • 也许你应该继续在 IRC 上获得帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
相关资源
最近更新 更多