<# Scheduled Task metadata
General
    Get IPs for PA - Azure
    run as ndgov\svcitdazurescript
        run whether user is logged on or not
 Triggers
    Daily, 2pm - repeat every 1 hour indefinitely
Actions
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -file "C:\itdscript\GetIPsAzure.ps1"
Settings
    allow task to be run on demand
    stop the task if it runs longer than 1 hour -eq $true
    if the running task does not end when requested, force it to stop
#>


Start-Transcript C:\itdscript\Logs\GetIPsAzureB.txt -Append
$VerbosePerfrence = "Continue"

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$OutputPath = "C:\inetpub\wwwroot"

$WindowsIPs = ""
$LinuxIPs = ""
$OutFileWin = $OutputPath + "\AzureWin.txt"
$OutFileLin = $OutputPath + "\AzureLin.txt"
Remove-Item $OutFileWin
Remove-Item $OutFileLin


$username="svcitdazurescript@nd.gov"
$usernameg="svcitdazurescript@ndstate.onmicrosoft.com"
#Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File C:\Users\svcitdazurescript\AppData\Local\Microsoft\sac.bat
$password=Get-Content C:\Users\svcitdazurescript\AppData\Local\Microsoft\sac.bat | ConvertTo-SecureString
$passwordg=Get-Content C:\Users\svcitdazurescript\AppData\Local\Microsoft\sacg.bat | ConvertTo-SecureString
$AdminCred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password
$AdminCredg=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $usernameg,$passwordg

Login-AzureRMAccount -Credential $AdminCred -Environment AzureCloud -Verbose

$Subscriptions = Get-AzureRMSubscription | Where-Object {$_.Name -ne "sandbox"}
foreach($subscription in $subscriptions)
    {
        Set-AzureRMContext -SubscriptionObject $subscription
        $WindowsVMs = Get-AzureRMVM | Where-Object {$_.StorageProfile.osdisk.ostype -match "Windows"}
        $LinuxVMs = Get-AzureRMVM | Where-Object {$_.StorageProfile.osdisk.ostype -match "Linux" -and $_.StorageProfile.ImageReference.Publisher -ne "infoblox" -and $_.StorageProfile.ImageReference.Publisher -ne "paloaltonetworks" -and $_.StorageProfile.ImageReference.Publisher -ne "juniper-networks"}
        $nics = Get-AzureRMNetworkInterface | Where-Object {$_.VirtualMachine -NE $null}

        foreach($vm in $WindowsVMs)
        {
            $vmnicinterfaces = $vm.Networkprofile.NetworkInterfaces.id
            foreach($vmnicinterface in $vmnicinterfaces)
            {
                $nic = $nics | Where-Object {$_.Id -eq $vmnicinterface}
                $privateip = $nic.IpConfigurations | Select PrivateIPAddress
        
                If($privateip.count -gt 1)
                {
                    foreach($private in $privateip)
                    {
                        $WindowsIPs += $private.privateIPAddress + "`n"
                    }
                }
                elseIf($privateip.privateIPAddress -ne $null)
                    {
                        $WindowsIPs += $privateip.privateIPAddress + "`n"
                    }
            }
        }

    foreach($vm in $LinuxVMs)
    {
        $vmnicinterfaces = $vm.Networkprofile.NetworkInterfaces.id
        foreach($vmnicinterface in $vmnicinterfaces)
        {
            $nic = $nics | Where-Object {$_.Id -eq $vmnicinterface}
            $privateip = $nic.IpConfigurations | Select PrivateIPAddress
    
            If($privateip.count -gt 1)
            {
                foreach($private in $privateip)
                {
                    $LinuxIPs += $private.privateIPAddress + "`n"
                }
            }
            elseif($privateip.privateIPAddress -ne $null)
            {
                $LinuxIps += $privateip.privateIPAddress + "`n"
            }
        }
    }
}

Login-AzureRMAccount -EnvironmentName AzureUSGovernment -Credential $AdminCredg

$Subscriptions = Get-AzureRMSubscription | Where-Object {$_.Name -ne "sandbox"}
foreach($subscription in $subscriptions)
{
    Set-AzureRMContext -SubscriptionObject $subscription
    $WindowsVMs = Get-AzureRMVM | Where-Object {$_.StorageProfile.osdisk.ostype -match "Windows"}
    $LinuxVMs = Get-AzureRMVM | Where-Object {$_.StorageProfile.osdisk.ostype -match "Linux" -and $_.StorageProfile.ImageReference.Publisher -ne "infoblox" -and $_.StorageProfile.ImageReference.Publisher -ne "paloaltonetworks" -and $_.StorageProfile.ImageReference.Publisher -ne "juniper-networks"}
    $nics = Get-AzureRMNetworkInterface | Where-Object {$_.VirtualMachine -NE $null}

    foreach($vm in $WindowsVMs)
    {
        $vmnicinterfaces = $vm.Networkprofile.NetworkInterfaces.id
        foreach($vmnicinterface in $vmnicinterfaces)
        {
            $nic = $nics | Where-Object {$_.Id -eq $vmnicinterface}
            $privateip = $nic.IpConfigurations | Select PrivateIPAddress
            If($privateip.count -gt 1)
            {
                foreach($private in $privateip)
                {
                    $WindowsIPs += $private.privateIPAddress + "`n"
                }
            }
            elseIf($privateip.privateIPAddress -ne $null)
            {
                $WindowsIPs += $privateip.privateIPAddress + "`n"
            }
        }
    }

    foreach($vm in $LinuxVMs)
    {
        $vmnicinterfaces = $vm.Networkprofile.NetworkInterfaces.id
        foreach($vmnicinterface in $vmnicinterfaces)
        {
            $nic = $nics | Where-Object {$_.Id -eq $vmnicinterface}
            $privateip = $nic.IpConfigurations | Select PrivateIPAddress
    
            If($privateip.count -gt 1)
            {
                foreach($private in $privateip)
                {
                    $LinuxIPs += $private.privateIPAddress + "`n"
                }
            }
            elseif($privateip.privateIPAddress -ne $null)
            {
                $LinuxIps += $privateip.privateIPAddress + "`n"
            }
        }
    }
}

If ($WindowsIPs -ne "" -and $WindowsIPs.Length -gt 150) {$WindowsIPs | Out-File $OutFileWin -Encoding ASCII -NoNewline -Force}
If ($LinuxIPs -ne ""-and $LinuxIPs.Length -gt 150) {$LinuxIPs | Out-File $OutFileLin -Encoding ASCII -NoNewline -Force}
