Files
Zack Meier 1d304511b8 update
2026-04-15 15:45:50 -05:00

90 lines
3.5 KiB
PowerShell

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
}