将来自第二个 Invoke-WebRequest 导入的数据添加到现有数组中

Adding data from a second Invoke-WebRequest import into an existing array

我是 powershell 的新手,我的脚本将来自第二个 Invoke-WebRequest 的数据添加到现有数组 $arr1 时遇到问题。

对变量 $annotations 的第二次导入工作正常。然后我需要匹配 where $vm.Name = $annotations.Name 以便最终的 ForEach($vm in $vms) 工作并提取注释数据但我卡住了。

我的代码如下。请帮忙

$StorageSystemName = "storageName"
$StoragePoolName = "storagepool"
$ReportName = "~\Reports\ServerList_$((Get-Date).ToString('yyyy-MM-dd')).xlsx"

Invoke-WebRequest -Uri http://srv1/location/Report_volume_storage.csv -OutFile .\Report_volume_storage.csv
$luns = Import-Csv .\Report_volume_storage.csv -Delimiter ";" | 
    Where-Object {$_.'Storage System name' -eq $StorageSystemName -and $_.'Storage Pool Name' -eq $StoragePoolName -and $_.'Volume Name'} | 
    Sort-Object "Storage Pool Name", "Volume Name" 

Invoke-WebRequest -Uri http://srv2/addmdata/addmdata.csv -OutFile .\addmdata.csv
$annotations = Import-Csv .\addmdata.csv -Delimiter "," | 
    Select @{n='Name';e={$_.name.Split('.')[0]}}, * -Exclude Name,
        @{n="Annotationserverdescription";e={$_.'Server Description'}},
        @{n="Annotationapowner";e={$_.'Annotationapowner (Annotationappowner)'}},
        @{n="Annotationclient";e={$_.'Client'}}
    Sort-Object Name

$arr1 = @()

ForEach($lun in $luns)
{
    $dsnaa = $lun.'LUN UID'
    $dsnaa = "*$dsnaa*"

    $datastore = Get-Datastore | 
        Where {($_.ExtensionData.Info.Vmfs.Extent).DiskName -like $dsnaa}
    $VMs = @()
    $datastore | ForEach-Object {
        $dstore = $_.name
        $VMs += get-VM -datastore $dstore | Where {$_.PowerState -eq "PoweredOn"} | Select @{n="Name";e={$_.name}}, @{n="PowerState";e={$_.PowerState}}, @{n="Datastore_Name";e={$dstore}}
}

    ForEach($vm in $vms)
    {

        $data = New-Object System.Object

        $data | Add-Member -MemberType NoteProperty -Name "Name" -Value $vm.Name
        $data | Add-Member -MemberType NoteProperty -Name "PowerState" -Value $vm.PowerState
        $data | Add-Member -MemberType NoteProperty -Name "Annotationserverdescription" -Value $annotation.'Server Description'
        $data | Add-Member -MemberType NoteProperty -Name "Annotationapowner" -Value $annotation.'Annotationapowner (Annotationappowner)'
        $data | Add-Member -MemberType NoteProperty -Name "Annotationclient" -Value $annotation.Client
        $data | Add-Member -MemberType NoteProperty -Name "Volume Name" -Value $lun.'Volume Name'
        $data | Add-Member -MemberType NoteProperty -Name "LUN UID" -Value $lun.'LUN UID'
        $data | Add-Member -MemberType NoteProperty -Name "Capacity (GiB)" -Value $lun.'Capacity (GiB)'       
        $data | Add-Member -MemberType NoteProperty -Name "Storage Pool Name" -Value $lun.'Storage Pool Name'
        $data | Add-Member -MemberType NoteProperty -Name "Storage System name" -Value $lun.'Storage System name'
        $data | Add-Member -MemberType NoteProperty -Name "Storage Tier" -Value $lun.'Storage Tier'

        $arr1 += $data

    }

}

    $arr1 | Export-Excel $ReportName

您可以执行以下操作:

$StorageSystemName = "storageName"
$StoragePoolName = "storagepool"
$ReportName = "~\Reports\ServerList_$((Get-Date).ToString('yyyy-MM-dd')).xlsx"

Invoke-WebRequest -Uri http://srv1/location/Report_volume_storage.csv -OutFile .\Report_volume_storage.csv
$luns = Import-Csv .\Report_volume_storage.csv -Delimiter ";" | 
    Where-Object {$_.'Storage System name' -eq $StorageSystemName -and $_.'Storage Pool Name' -eq $StoragePoolName -and $_.'Volume Name'} | 
    Sort-Object "Storage Pool Name", "Volume Name" 

Invoke-WebRequest -Uri http://srv2/addmdata/addmdata.csv -OutFile .\addmdata.csv
$annotations = Import-Csv .\addmdata.csv -Delimiter "," | 
    Select @{n='Name';e={$_.name.Split('.')[0]}}, * -Exclude Name,
        @{n="Annotationserverdescription";e={$_.'Server Description'}},
        @{n="Annotationapowner";e={$_.'Annotationapowner (Annotationappowner)'}},
        @{n="Annotationclient";e={$_.'Client'}}
    Sort-Object Name

$arr1 = @()

ForEach($lun in $luns)
{
    $dsnaa = $lun.'LUN UID'
    $dsnaa = "*$dsnaa*"

    $datastore = Get-Datastore | 
        Where {($_.ExtensionData.Info.Vmfs.Extent).DiskName -like $dsnaa}
    $VMs = @()
    $datastore | ForEach-Object {
        $dstore = $_.name
        $VMs += get-VM -datastore $dstore | Where {$_.PowerState -eq "PoweredOn"} | Select @{n="Name";e={$_.name}}, @{n="PowerState";e={$_.PowerState}}, @{n="Datastore_Name";e={$dstore}}
}

    ForEach($vm in $vms)
    {
        $ActiveAnnotation = $null
        $ActiveAnnotation = $annotations | where-object {$_.name -eq $vm.name}
        $data = New-Object System.Object

        $data | Add-Member -MemberType NoteProperty -Name "Name" -Value $vm.Name
        $data | Add-Member -MemberType NoteProperty -Name "PowerState" -Value $vm.PowerState
        $data | Add-Member -MemberType NoteProperty -Name "Annotationserverdescription" -Value $ActiveAnnotation.'Server Description'
        $data | Add-Member -MemberType NoteProperty -Name "Annotationapowner" -Value $ActiveAnnotation.'Annotationapowner (Annotationappowner)'
        $data | Add-Member -MemberType NoteProperty -Name "Annotationclient" -Value $ActiveAnnotation.Client
        $data | Add-Member -MemberType NoteProperty -Name "Volume Name" -Value $lun.'Volume Name'
        $data | Add-Member -MemberType NoteProperty -Name "LUN UID" -Value $lun.'LUN UID'
        $data | Add-Member -MemberType NoteProperty -Name "Capacity (GiB)" -Value $lun.'Capacity (GiB)'       
        $data | Add-Member -MemberType NoteProperty -Name "Storage Pool Name" -Value $lun.'Storage Pool Name'
        $data | Add-Member -MemberType NoteProperty -Name "Storage System name" -Value $lun.'Storage System name'
        $data | Add-Member -MemberType NoteProperty -Name "Storage Tier" -Value $lun.'Storage Tier'

        $arr1 += $data

    }

}

    $arr1 | Export-Excel $ReportName

我在您的 VM 循环中添加了 $ActiveAnnotation 变量,以在每次循环中找到注释匹配。