<# Scheduled Task metadata
General
    Get IPs for PA - Azure Backup
    run as ndgov\svcitdazurescript
        run whether user is logged on or not
 Triggers
    Daily, 4:12pm - repeat every 1 hour indefinitely
Actions
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -file "C:\itdscript\GetIPsAzureBackup.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 D:\zm.log -force
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$OutputPath = "C:\inetpub\wwwroot"
$OutputBackupPath = "C:\inetpub\wwwroot\AzureBak\"
$CurrentDateTime = Get-Date -UFormat "%Y%m%d%H%M%S"


$BackupIPs = ""
$OutFileBak = $OutputPath + "\AzureBak.txt"
$OutFileBakBackup = $OutputBackupPath + "\AzureBak-$CurrentDateTime.txt"

Remove-Item $OutFileBak


$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

$Subscriptions = Get-AzureRMSubscription | Where-Object {$_.Name -ne "sandbox"}
foreach($subscription in $subscriptions)
    {
        Set-AzureRMContext -SubscriptionObject $subscription
        $BackupVMs = Get-AzureRMVM | Where-Object {$_.Tags["Backup"] -match "OS-BackupEnabled"}
        $nics = Get-AzureRMNetworkInterface | Where-Object {$_.VirtualMachine -NE $null}

        foreach($vm in $BackupVMs)
        {
            $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)
                    {
                        $BackupIPs += $private.privateIPAddress + "`n"
                    }
                }
                elseIf($privateip.privateIPAddress -ne $null)
                    {
                        $BackupIPs += $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
    $BackupVMs = Get-AzureRMVM | Where-Object {$_.Tags["Backup"] -match "OS-BackupEnabled"}
    $nics = Get-AzureRMNetworkInterface | Where-Object {$_.VirtualMachine -NE $null}

    foreach($vm in $BackupVMs)
    {
        $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)
                {
                    $BackupIPs += $private.privateIPAddress + "`n"
                }
            }
            elseIf($privateip.privateIPAddress -ne $null)
            {
                $BackupIPs += $privateip.privateIPAddress + "`n"
            }
        }
    }
}

If ($BackupIPs -ne "") {
    $BackupIPs | Out-File $OutFileBak -Encoding ASCII -NoNewline -Force
    $BackupUPs | Out-File $OutFileBakBackup -Encoding ascii -NoNewline -Force
    }
