Framework/Core/SVT/SVTIaasBase.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
Set-StrictMode -Version Latest
class SVTIaasBase: SVTBase
{
    hidden [PSObject] $ResourceObject;
    hidden [PSObject[]] $vNetNics;
    hidden [PSObject[]] $vNetNicsOutput;

    SVTIaasBase([string] $subscriptionId, [string] $resourceGroupName, [string] $resourceName):
        Base($subscriptionId, $resourceGroupName, $resourceName)
    {
        $this.GetResourceObject();
        $this.GetvNetNics($this.ResourceObject.Subnets);
        $this.GetvnetNicsProperties($this.vNetNics);
    }

    SVTIaasBase([string] $subscriptionId, [SVTResource] $svtResource):
        Base($subscriptionId, $svtResource)
    {
        $this.GetResourceObject();
        $this.GetvNetNics($this.ResourceObject.Subnets);
        $this.GetvnetNicsProperties($this.vNetNics);
    }

    hidden [PSObject] GetResourceObject()
    {
        if (-not $this.ResourceObject) {
            $this.ResourceObject = Get-AzureRmVirtualNetwork -ResourceGroupName $this.ResourceContext.ResourceGroupName `
                                             -Name $this.ResourceContext.ResourceName
            if(-not $this.ResourceObject)
            {
                throw ([SuppressedException]::new(("Resource '{0}' not found under Resource Group '{1}'" -f ($this.ResourceContext.ResourceName), ($this.ResourceContext.ResourceGroupName)), [SuppressedExceptionType]::InvalidOperation))
            }
        }
        return $this.ResourceObject;
    }

    hidden [PSObject[]] GetvNetNics($VNetSubnets)
    {
        if (-not $this.vNetNics)
        {
            $VNetSubnets | ForEach-Object{
                Set-Variable -Name currentsubnet -Scope Local -Value $_
                if($null -ne $currentsubnet.IpConfigurations )
                {
                        $currentsubnet.IpConfigurations | ForEach-Object{
                        Set-Variable -Name currentipconfig -Scope Local -Value $_
                        if($currentipconfig.Id.Contains("Microsoft.Network/networkInterfaces"))
                        {
                                $currentipconfig = $currentipconfig.Id.ToLower()
                                $nicresourceid =  $currentipconfig.Substring(0,$currentipconfig.LastIndexOf("ipconfigurations")-1)
                                $nic = Get-AzureRmResource -ResourceId $nicresourceid
                                $this.vNetNics += $nic
                        }
                    }
                }
            }
        }
        return $this.vNetNics;
    }

    hidden [PSObject[]] GetvnetNicsProperties($vNetNics)
    {
        if(-not $this.vNetNicsOutput)
        {
            if($null -ne $vNetNics )
            {
                $tempVNetNICS = [array]($vNetNics)
                $tempVNetNICS | ForEach-Object{
                    Set-Variable -Name nic -Scope Local -Value $_
                    Set-Variable -Name nicproperties -Scope Local -Value $_.Properties

                    $out = ""| Select-Object NICName, VMName, VMId, PrimaryStatus, NetworkSecurityGroupName,NetworkSecurityGroupId, PublicIpAddress, PrivateIpAddress,  EnableIPForwarding, IpConfigurations
                    $out.NICName = $nic.Name
                    $out.IpConfigurations = $nicproperties.IpConfigurations
                    $out.EnableIPForwarding = $nicproperties.EnableIPForwarding
                    $PublicIpAddresses = @()
                    $PrivateIpAddresses = @()
                    $nicproperties.IpConfigurations | ForEach-Object{
                        Set-Variable -Name ipconfiguration -Scope Local -Value $_
                        if(($ipconfiguration | Get-Member -Name "Properties") -and ($ipconfiguration.Properties | Get-Member -Name "PublicIpAddress") -and $ipconfiguration.Properties.PublicIpAddress)
                        {
                            $IPResource = Get-AzureRmResource -ResourceId $ipconfiguration.Properties.PublicIpAddress.Id
                            $pubResourceName = Get-AzureRmPublicIpAddress -Name $IPResource.Name -ResourceGroupName $IPResource.ResourceGroupName
                            $PublicIpAddresses += $pubResourceName.IpAddress
                        }
                        $PrivateIpAddresses += $ipconfiguration.Properties.PrivateIpAddress
                    }
                    $out.PublicIpAddress = ([System.String]::Join(";",$PublicIpAddresses))
                    $out.PrivateIpAddress = ([System.String]::Join(";",$PrivateIpAddresses))

                    if(($nicproperties | Get-Member -Name "VirtualMachine") -and $nicproperties.VirtualMachine )
                    {
                        $vmresource = Get-AzureRmResource -ResourceId $nicproperties.VirtualMachine.Id
                        $out.VMName = $vmresource.Name
                    }
                    else {
                        $out.VMName = ""
                    }
                    if($null -ne ($nicproperties | Get-Member primary))
                    {
                        $out.PrimaryStatus = $nicproperties.primary
                    }

                    if(($nicproperties | Get-Member -Name "NetworkSecurityGroup") -and $nicproperties.NetworkSecurityGroup)
                    {
                        $nsgresource = Get-AzureRmResource -ResourceId $nicproperties.NetworkSecurityGroup.Id
                        $out.NetworkSecurityGroupName = $nsgresource.Name
                    }
                    $this.vNetNicsOutput += $out
                }
                $this.vNetNicsOutput = [array]($this.vNetNicsOutput)
            }
        }
        return $this.vNetNicsOutput;
    }
}