Search code examples
powershellfor-loopvmwarepowercli

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


I am fairly new to powershell and am having a problem with my script adding data from a second Invoke-WebRequest into an existing array $arr1.

The second import into variable $annotations works fine. I then need to match where $vm.Name = $annotations.Name in order for final ForEach($vm in $vms) to work and pull in the annotations data as well but am stuck.

My code is as follows. Please help

$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

Solution

  • You could do something like the following:

    $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
    

    I added the $ActiveAnnotation variable inside of your VMs loop to find the annotation match each time through the loop.