Created
April 24, 2024 19:26
-
-
Save Rugby-Ball/8f052668daeaa3f120a100c49ea446f6 to your computer and use it in GitHub Desktop.
For Azure, get inventory of running Windows servers and status of the three Firewall Profiles. #Utility #Security #Public #Inventory #Audit #Azure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Azure-FireWall-Status-Inventory.ps1 | |
| <# | |
| Description: For Azure, get inventory of running Windows servers and status of the three Firewall Profiles. | |
| Edited by: Ed Walsh | |
| PowerShell.Core tested: Yes | |
| MS-Graph: No | |
| Version: 1.0.0 | |
| Create Date: 4/23/2024 | |
| Revised Date: 4/23/2024 | |
| #> | |
| # You don't need the next line for every run of script, once a day should be enough. | |
| # Connect-AzAccount | |
| $subscriptions = Get-AzSubscription # -SubscriptionName "MRI-Simmons Subscription" | |
| $sub_Count = ($subscriptions | Measure-Object).Count | |
| Clear-Host | |
| $stopWatch = [System.Diagnostics.Stopwatch]::StartNew() | |
| $stopwatch.Start() | |
| Write-Host "Script started on $(Get-Date -f "MM-dd-yyyy hh:mm tt") This script might take sometime to run." -ForegroundColor Yellow -BackgroundColor Red | |
| $timestamp = get-date -format yyyyMMddHHmmss | |
| $subfolder = if (($PSVersionTable.PSEdition) -eq "Core") { if ( $True -eq $iswindows ) { "\Documents\" } Else { "" } } Else { "\Documents\" } | |
| $mydocuments = $home + $subfolder | |
| $fileName = "Azure-FireWall-Status-Inventory-" + [string]$timestamp + ".csv" | |
| $filePath = Join-Path $mydocuments $fileName | |
| $out = @() | |
| $g = 0 | |
| ForEach ($Subscription in $Subscriptions) { | |
| Set-AzContext -SubscriptionId $Subscription.Id | Out-Null | |
| $g++ | |
| $sub_id = $Subscription.id | |
| $sub_name = $Subscription.Name | |
| $i = 0 | |
| # Get all VMs in the subscription | |
| $vms = Get-AzVM | |
| $servers = @() | |
| # Loop through each VM | |
| foreach ($vm in $vms) { | |
| # Check if the VM is running and is a Windows server | |
| if ($vm.StorageProfile.OsDisk.OsType -eq "Windows" -and (Get-AzVM -Name $vm.Name -Status).PowerState -eq "VM running") { | |
| $servers += $vm | |
| } | |
| } | |
| #Count how many running Windows servers in Subscription. | |
| $scount = ($servers | Measure-Object).Count | |
| # If no Windows servers running in Subscription, skip to next Subscription. | |
| if ($scount -le 0) { | |
| continue; # Skip to the next subscription. | |
| } | |
| Write-Host "Subscription $g of $sub_count :"$sub_name". Checking firewall status on "$scount" servers." -BackgroundColor Cyan | |
| # Write-Host "Subscription:"$sub_name". Checking firewall status on "$scount" servers" -BackgroundColor Cyan | |
| foreach ($server in $servers ) { | |
| $i++ | |
| Write-Host "Checking firewall status on " -NoNewline | |
| Write-Host $server.name -BackgroundColor Yellow -NoNewline | |
| Write-Host " at"(Get-Date -Format "hh:mm:ss tt")" $i of $scount" | |
| # Check the status of the Domain, Public and Private firewall profiles. | |
| $fw_status = $server | ForEach-Object { | |
| (Invoke-AzVMRunCommand -ResourceGroupName $_.ResourceGroupName -Name $_.Name -CommandId 'RunPowerShellScript' -ScriptString "Get-NetFirewallProfile | Select-Object Name,Enabled | ConvertTo-Json").Value[0].message | |
| } | |
| $firewall_status = ConvertFrom-Json $fw_status | |
| $DomainStatus = ($firewall_status | where-object name -eq "Domain" | select-object enabled ).Enabled | |
| $publicStatus = ($firewall_status | where-object name -eq "Public" | select-object enabled ).Enabled | |
| $privateStatus = ($firewall_status | where-object name -eq "Private" | select-object enabled ).Enabled | |
| # Output the status | |
| $o = New-Object -TypeName System.Management.Automation.PSObject -Property ([ordered]@{ | |
| 'SubscriptionID' = $sub_id; | |
| 'Subscription_Name' = $sub_name; | |
| 'Server' = $server.Name; | |
| 'Status_Checked' = Get-Date -f "MM-dd-yyyy hh:mm:ss tt" ; | |
| 'Domain_FW-Status' = $DomainStatus; | |
| 'Public_FW_Status' = $publicStatus; | |
| 'Private_FW_Status' = $privateStatus | |
| }) | |
| $out += $o | |
| } | |
| } | |
| $out | Export-Csv -NoTypeInformation -Path $filepath | |
| # $Out | Out-GridView | |
| Write-Output "Exported to: $filePath" | |
| Write-Output "Folder Path: $mydocuments" | |
| Write-Output "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|||||||////////////////////////////////////" | |
| $stopwatch.Stop() | |
| $time = $stopwatch.Elapsed | |
| Write-Host "Script finished on $(Get-Date -f "MM-dd-yyyy hh:mm tt"), Elapsed time to run script (HH:MM:SS.MS): $Time" -ForegroundColor Green -BackgroundColor Yellow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment