匹配来自两个不同阵列的行

时间:2016-05-02 14:39:06

标签: arrays powershell

我尝试创建域中所有服务器的列表,并记录正在备份哪些服务器。我使用Quest插件作为AD服务器列表,使用Veeam插件作为备份列表。正在创建数组,但我无法理解如何比较和创建最终列表。

# Get list of all servers in AD

if ((Get-PSSnapin -Name Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin Quest.ActiveRoles.ADManagement
}
$servers = get-qadcomputer -SizeLimit 0 | where {$_.OSName -match "Server"}

$arrAllServers = @()

foreach ($server in $servers) {
    $singleserver = New-Object PSObject
    $singleserver | Add-Member -Name "Name" -Value $x.name -MemberType NoteProperty
    $arrAllServers += $singleserver
}

# Get list of servers being backed up

if ((Get-PSSnapin -Name VeeamPSSnapin -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin VeeamPSSnapin
}

Connect-VBRServer -Server host.foo.org -Port 9392

$allDetails = @()

$jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}

# Loop through each job adding details to array
foreach ($job in $jobs) {
    $Object = Get-VBRJobObject -Job $job 
    $jobOptions = New-Object PSObject
    $jobOptions | Add-Member -MemberType NoteProperty -Name "Name" -value $job.name
    $jobOptions | Add-Member -MemberType NoteProperty -Name "Enabled" -value $job.isscheduleenabled
    $jobOptions | Add-Member -MemberType NoteProperty -Name "Servers" -Value (@($Object.name) | Out-String).Trim()
    $jobOptions | Add-Member -MemberType NoteProperty -Name "Backup Mode" -value $job.backuptargetoptions.algorithm
    $repo = (Get-VBRBackupRepository | ?{$_.HostId -eq $job.TargetHostId -and $_.Path -eq $job.TargetDir}).name
    $jobOptions | Add-Member -MemberType NoteProperty -Name "Repository" -value $repo
    $allBackupDetails += $jobOptions
}
Disconnect-VBRServer

# Now match by server name and produce some output. 

foreach ($lserver in $arrAllServers) {
    foreach ($bserver in $allDetails) {
        if($lserver.name -eq $bserver.servers) {
            Write-Host $lserver.name $bserver.name
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  1. 您创建数组$allDetails并从中读取,但您将对象写入$allBackupDetails
  2. $bserver.servers是一个数组,因此您需要使用-contains-in
  3. $bserver.servers可能会被打破"因为你的out-string - 操作(为什么要使用它?)
  4. 当您只需要服务器名时,
  5. $arrAllServers是浪费的资源。
  6. 尝试:

    # Load modules
    
    if ((Get-PSSnapin -Name Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue) -eq $null) {
        Add-PSSnapin Quest.ActiveRoles.ADManagement
    }
    
    if ((Get-PSSnapin -Name VeeamPSSnapin -ErrorAction SilentlyContinue) -eq $null) {
        Add-PSSnapin VeeamPSSnapin
    }
    
    # Get list of all servers in AD
    $servers = get-qadcomputer -SizeLimit 0 | where {$_.OSName -match "Server"} | Select-Object -ExpandProperty Name
    
    # Get backup jobs
    Connect-VBRServer -Server host.foo.org -Port 9392
    
    $allBackupDetails = @()
    
    $jobs = Get-VBRJob | ?{$_.JobType -eq "Backup"}
    
    # Loop through each job adding details to array
    foreach ($job in $jobs) {
        $Object = Get-VBRJobObject -Job $job 
        $jobOptions = New-Object PSObject
        $jobOptions | Add-Member -MemberType NoteProperty -Name "Name" -value $job.name
        $jobOptions | Add-Member -MemberType NoteProperty -Name "Enabled" -value $job.isscheduleenabled
        $jobOptions | Add-Member -MemberType NoteProperty -Name "Servers" -Value @($Object.name)
        $jobOptions | Add-Member -MemberType NoteProperty -Name "Backup Mode" -value $job.backuptargetoptions.algorithm
        $repo = (Get-VBRBackupRepository | ?{$_.HostId -eq $job.TargetHostId -and $_.Path -eq $job.TargetDir}).name
        $jobOptions | Add-Member -MemberType NoteProperty -Name "Repository" -value $repo
        $allBackupDetails += $jobOptions
    }
    Disconnect-VBRServer
    
    # Now match by server name and produce some output. 
    
    foreach ($server in $servers) {
        foreach ($backupDetails in $allBackupDetails) {
            if($backupDetails.servers -contains $server) {
                Write-Host "$server is in $($backupDetails.name)"
            }
        }
    }