Storage Account vnet details - azure

I am trying to create a report to find all the storage account and its associated vnet details.
& {
foreach ($storageAccount in Get-AzStorageAccount) {
$storageAccountName = $storageAccount.StorageAccountName
$resourceGroupName = $storageAccount.ResourceGroupName
$context = (Get-AzStorageAccount -StorageAccountName $storageAccountName -ResourceGroupName $resourceGroupName).NetworkRuleSet.VirtualNetworkRules.VirtualNetworkResourceId
$splitarray = $context.Split('/')
$vnetid = $splitarray[8]
$subscriptionid = $splitarray[2]
New-Object psobject -Property #{
Name = $storageAccountName;
Context = $vnetid;
ResourceGroupName = $resourceGroupName
Subscriptionid = $subscriptionid
}
}
} | Format-Table Name, Context, subscriptionid, ResourceGroupName
I am currently getting the below output:
storage account vnet report
As you can see from the output the vnet name is not properly fetched for the storage accounts.
Storage account testfnapp2oct16 has vnet testfnvnet attached, this is correct.
Storage accounts unz2versvaultea, cs1f7b27d61e31e, win10guestdiag954 doesn't have any vnet attached but 'testfnvnet' is repeated until the value changes for a different storage account.
Storage account testfnapp9eb7 has two vnets but only testvnet1 is shown and the value 'testvnet1' is repeated for next storage account.
Any help is much appreciated.

I tried to reproduce the same in my environment it's work me successfully.
I have created sample storage account fetched with vnet and without attached with vnet when I tried, your code I got the same error as below.
To resolve this issue Please utilise the condition I have changed in the code below to execute this issue's solution properly.
& {
foreach ($storageAccount in Get-AzStorageAccount) {
$storageAccountName = $storageAccount.StorageAccountName
$resourceGroupName = $storageAccount.ResourceGroupName
$context = (Get-AzStorageAccount -StorageAccountName $storageAccountName -ResourceGroupName $resourceGroupName).NetworkRuleSet.VirtualNetworkRules.VirtualNetworkResourceId
$ErrorActionPreference = ‘SilentlyContinue’
if ($context -eq $null){
New-Object psobject -Property #{
Name = $storageAccountName;
Context = "Empty" ;
ResourceGroupName = $resourceGroupName
Subscriptionid = $subscriptionid
}
}
else {
$splitarray = $context.Split('/')
$vnetid = $splitarray[8]
$subscriptionid = $splitarray[2]
New-Object psobject -Property #{
Name = $storageAccountName;
Context = $vnetid;
ResourceGroupName = $resourceGroupName
Subscriptionid = $subscriptionid
}
}
}
} | Format-Table Name, Context, subscriptionid, ResourceGroupName
Result:
Now, whichever vnet are not fetched in my storage account it's show Empty like below.

Related

Export Vnet | Subnet

I'd like to export something like the view of the subnets that exist in a VNET like is displayed in the portal. Unfortunately there isn't an option to export this to a CSV. I have found powershell scripts online that can export subnet route tables and the associated subnets. I have also found powershell scripts to export details on vnets subnets. However I haven't been able to find scripts that combine both
Script for Route tables by Aman Sharma
Ignore the synopsis and description I think he left them in from previous scripts
So I'm trying to reverse the logic i.e. get the subnet details and add the route tables for each subnet if it exists. However I'm not sure what I'm doing at this point! the script is erroring with:
Line |
47 | … $routeTables = Get-AzRouteTable -Name $routeTableName -Resour …
| ~~~~~~~~~~~~~~~
| Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
The script ends and the CSV has everything but the route table details. So if you could help a noob I'd be very grateful here is what I have:
$PathToOutputCSVReport = "/path/output.csv"
$subs = Get-AzSubscription
#Checking if the subscriptions are found or not
if(($subs -ne $null) -or ($subs.Count -gt 0))
{
#Creating Output Object
$results = #()
#Iterating over various subscriptions
foreach($sub in $subs)
{
$SubscriptionId = $sub.SubscriptionId
Write-Output $SubscriptionName
#Selecting the Azure Subscription
Select-AzSubscription -SubscriptionName $SubscriptionId
#Getting all Azure Route Tables
$vnets = Get-AzVirtualNetwork
foreach($vnet in $vnets)
{
$vnetName = $vnet.Name
$vnetResourceGroup = $vnet.ResourceGroupName
Write-Output $vnetName
#Fetch Route Subnets
$vnetSubnets = $vnet.Subnets
foreach($vnetSubnet in $vnetSubnets)
{
$subnetName = $vnetSubnet.Name
Write-Output $subnetName
$subnetId = $vnetSubnet.Id
###Getting information
$splitarray = $subnetId.Split('/')
$subscriptionId = $splitarray[2]
$vNetResourceGroupName = $splitarray[4]
$virtualNetworkName = $splitarray[8]
$subnetName = $splitarray[10]
#Fetch the route table details
$routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup
#Fetching the vNet and Subnet details
#$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $vNetResourceGroupName
$subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
$subnetAddressPrefix = $subnet.AddressPrefix[0]
$details = #{
virtualNetworkName=$virtualNetworkName
subnetAddressPrefix=$subnetAddressPrefix
subnetName=$subnetName
routeTableName=$routeTableName
routeResourceGroup=$routeResourceGroup
subscriptionId=$subscriptionId
vNetResourceGroupName=$vNetResourceGroupName
}
$results += New-Object PSObject -Property $details
}
}
}
$results | export-csv -Path $PathToOutputCSVReport -NoTypeInformation
}
else
{
Write-Host -ForegroundColor Red "No Subscription Found"
}
This error you're getting clearly shows that the variable routeTableName is an invalid value for the -Name parameter of the cmdlet Get-AzRouteTable.
Looking at your script, it appears that the variable is not defined anywhere, which explains why its empty, hence invalid to be used with the cmdlet.
To define the variable with the name of the route table associated to a subnet, you can use the following:
$routeTableName = $subnet.RouteTable.Id.Split('/')[8]
It also seems you're not using the following at all and can be removed:
$routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup
Here's how your entire code would look like:
$PathToOutputCSVReport = "/path/output.csv"
$subs = Get-AzSubscription
#Checking if the subscriptions are found or not
if(($subs -ne $null) -or ($subs.Count -gt 0))
{
#Creating Output Object
$results = #()
#Iterating over various subscriptions
foreach($sub in $subs)
{
$SubscriptionId = $sub.SubscriptionId
Write-Output $SubscriptionName
#Selecting the Azure Subscription
Select-AzSubscription -SubscriptionName $SubscriptionId
#Getting all Azure Route Tables
$vnets = Get-AzVirtualNetwork
foreach($vnet in $vnets)
{
$vnetName = $vnet.Name
$vnetResourceGroup = $vnet.ResourceGroupName
Write-Output $vnetName
#Fetch Route Subnets
$vnetSubnets = $vnet.Subnets
foreach($vnetSubnet in $vnetSubnets)
{
$subnetName = $vnetSubnet.Name
Write-Output $subnetName
$subnetId = $vnetSubnet.Id
###Getting information
$splitarray = $subnetId.Split('/')
$subscriptionId = $splitarray[2]
$vNetResourceGroupName = $splitarray[4]
$virtualNetworkName = $splitarray[8]
$subnetName = $splitarray[10]
#Fetch the route table details
#$routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup
#Fetching the vNet and Subnet details
#$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $vNetResourceGroupName
$subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
$routeTableName = $subnet.RouteTable.Id.Split('/')[8]
$subnetAddressPrefix = $subnet.AddressPrefix[0]
$details = #{
virtualNetworkName=$virtualNetworkName
subnetAddressPrefix=$subnetAddressPrefix
subnetName=$subnetName
routeTableName=$routeTableName
routeResourceGroup=$routeResourceGroup
subscriptionId=$subscriptionId
vNetResourceGroupName=$vNetResourceGroupName
}
$results += New-Object PSObject -Property $details
}
}
}
$results | export-csv -Path $PathToOutputCSVReport -NoTypeInformation
}
else
{
Write-Host -ForegroundColor Red "No Subscription Found"
}
We have tried the same and getting the same warning as yours ,
Looking at your script ,you are doing correct but have not provided any variable to read the $routeTableName or $routeResourceGroup at line 47. We have used foreach loop again to retrieve route table details as subnet and vnets you have used, Followed by the below script we are able to run it without any failures:-
$PathToOutputCSVReport = "/mylocalpath/output.csv"
$subs = Get-AzSubscription
#Checking if the subscriptions are found or not
if(($subs -ne $null) -or ($subs.Count -gt 0))
{
#Creating Output Object
$results = #()
#Iterating over various subscriptions
foreach($sub in $subs)
{
$SubscriptionId = $sub.SubscriptionId
Write-Output $SubscriptionName
#Selecting the Azure Subscription
Select-AzSubscription -SubscriptionName $SubscriptionId
#Getting all Azure Route Tables
$vnets = Get-AzVirtualNetwork
foreach($vnet in $vnets)
{
$vnetName = $vnet.Name
$vnetResourceGroup = $vnet.ResourceGroupName
Write-Output $vnetName
#Fetch Route Subnets
$vnetSubnets = $vnet.Subnets
foreach($vnetSubnet in $vnetSubnets)
{
$subnetName = $vnetSubnet.Name
Write-Output $subnetName
$subnetId = $vnetSubnet.Id
###Getting information
$splitarray = $subnetId.Split('/')
$subscriptionId = $splitarray[2]
$vNetResourceGroupName = $splitarray[4]
$virtualNetworkName = $splitarray[8]
$subnetName = $splitarray[10]
#Fetch the route table details
#$routeTables = Get-AzRouteTable -ResourceGroupName $routeResourceGroup -Name $routeTableName instead of this tried below line to fetch the route table details
$routeTables = Get-AzRouteTable
foreach($routeTable in $routeTables)
{
$routeTableName = $routeTable.Name
$routeResourceGroup = $routeTable.ResourceGroupName
Write-Output $routeName
#Fetching the vNet and Subnet details
#$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $vNetResourceGroupName
$subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
$subnetAddressPrefix = $subnet.AddressPrefix[0]
$details = #{
virtualNetworkName=$virtualNetworkName
subnetAddressPrefix=$subnetAddressPrefix
subnetName=$subnetName
routeTableName=$routeTableName
routeResourceGroup=$routeResourceGroup
subscriptionId=$subscriptionId
vNetResourceGroupName=$vNetResourceGroupName
}
$results += New-Object PSObject -Property $details
}
}
}
}
$results | export-csv -Path $PathToOutputCSVReport -NoTypeInformation
}
else
{
Write-Host -ForegroundColor Red "No Subscription Found"
}
Also the link which you have shared it will also list as per your requirement .
OUTPUT DETAILS FOR REFERENCE:-
CSV FILE :-
For more information to fetch the route details in different way as well please refer the below links:-
SO THREAD|Route Table Details ,Output of Azure subnets with links to attached VNet, route table and NSG

Assistance with looping through multiple subscriptions

I'm using the below script from http://vcloud-lab.com/entries/microsoft-azure/get-azure-virtual-machine-backup-reports-using-powershell to pull Azure VM backup details. Currently I have to run the script against each subscription, I would love to have it loop though all subscriptions.
I've been trying to get it working using this example https://www.jpaul.me/2019/05/azure-automation-how-to-quickly-work-with-many-subscriptions/ but I'm new to powershell and am struggling. Any suggestions would be really appreciated.
[CmdletBinding(SupportsShouldProcess=$True,
ConfirmImpact='Medium',
HelpURI='http://vcloud-lab.com',
DefaultParameterSetName = 'AllVirtualMachines'
)]
<#
.SYNOPSIS
Collect Azure VM Backup Information
.DESCRIPTION
This Script collects Azure Virtual Machine Backup Recovery service vault information, This report includes the complete backup status Information of VM.
.PARAMETER AllVirtualMachines
Collect Backup information of the all Azure Virtual Machines, This is default parameter.
.PARAMETER VirtualMachineList
You can specify for which virtual machine you want backup information.
.INPUTS
None. Provides virtual machine information.
.OUTPUTS
Generate Backup information. You can pipe information to Export-CSV.
.EXAMPLE
PS> .\Get-AzVMBackupInformation.ps1
VM_Name : vcloud-lab-vm01
VM_Location : uksouth
VM_ResourceGroupName : VCLOUD-LAB.COM
VM_BackedUp : True
VM_RecoveryVaultName : vault828
VM_RecoveryVaultPolicy : DailyPolicy-kosrnox0
VM_BackupHealthStatus : Passed
VM_BackupProtectionStatus : Healthy
VM_LastBackupStatus : Completed
VM_LastBackupTime : 27-05-2021 19:32:34
VM_BackupDeleteState : NotDeleted
VM_BackupLatestRecoveryPoint : 27-05-2021 19:32:37
VM_Id : /subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/VCLOUD-LAB.COM/providers/Microsoft.Compute/virtualMachines/vcloud-lab-vm01
RecoveryVault_ResourceGroupName : vCloud-lab.com
RecoveryVault_Location : uksouth
RecoveryVault_SubscriptionId : /subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/vCloud-lab.com/providers/Microsoft.RecoveryServices/vaults/vault828
.EXAMPLE
PS> .\Get-AzVMBackupInformation.ps1 -AllVirtualMachines
This produces same result as .\Get-AzVMBackupInformation.ps1 from all VMs
.EXAMPLE
PS> .\Get-AzVMBackupInformation.ps1 -VirtualMachineList
Provide either single virtual machine name or in list
.LINK
Online version: http://vcloud-lab.com
.LINK
Get-AzVMBackupInformation.ps1
#>
Param
(
[parameter(Position=0, ParameterSetName = 'AllVMs' )]
[Switch]$AllVirtualMachines,
[parameter(Position=0, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True, ParameterSetName = 'VM' )]
[alias('Name')]
[String[]]$VirtualMachineList
) #Param
Begin
{
#Collecing Azure virtual machines Information
Write-Host "Collecing Azure virtual machine Information" -BackgroundColor DarkGreen
if (($PSBoundParameters.ContainsKey('AllVirtualMachines')) -or ($PSBoundParameters.Count -eq 0))
{
$vms = Get-AzVM
} #if ($PSBoundParameters.ContainsKey('AllVirtualMachines'))
elseif ($PSBoundParameters.ContainsKey('VirtualMachineList'))
{
$vms = #()
foreach ($vmname in $VirtualMachineList)
{
$vms += Get-AzVM -Name $vmname
} #foreach ($vmname in $VirtualMachineList)
} #elseif ($PSBoundParameters.ContainsKey('VirtualMachineList'))
#Collecing All Azure backup recovery vaults Information
Write-Host "Collecting all Backup Recovery Vault information" -BackgroundColor DarkGreen
$backupVaults = Get-AzRecoveryServicesVault
} #Begin
Process
{
$vmBackupReport = [System.Collections.ArrayList]::new()
foreach ($vm in $vms)
{
$recoveryVaultInfo = Get-AzRecoveryServicesBackupStatus -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Type 'AzureVM'
if ($recoveryVaultInfo.BackedUp -eq $true)
{
Write-Host "$($vm.Name) - BackedUp : Yes"
#Backup Recovery Vault Information
$vmBackupVault = $backupVaults | Where-Object {$_.ID -eq $recoveryVaultInfo.VaultId}
#Backup recovery Vault policy Information
$container = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -VaultId $vmBackupVault.ID -FriendlyName $vm.Name #-Status "Registered"
$backupItem = Get-AzRecoveryServicesBackupItem -Container $container -WorkloadType AzureVM -VaultId $vmBackupVault.ID
} #if ($recoveryVaultInfo.BackedUp -eq $true)
else
{
Write-Host "$($vm.Name) - BackedUp : No" -BackgroundColor DarkRed
$vmBackupVault = $null
$container = $null
$backupItem = $null
} #else if ($recoveryVaultInfo.BackedUp -eq $true)
[void]$vmBackupReport.Add([PSCustomObject]#{
VM_Name = $vm.Name
VM_Location = $vm.Location
VM_ResourceGroupName = $vm.ResourceGroupName
VM_BackedUp = $recoveryVaultInfo.BackedUp
VM_RecoveryVaultName = $vmBackupVault.Name
VM_RecoveryVaultPolicy = $backupItem.ProtectionPolicyName
VM_BackupHealthStatus = $backupItem.HealthStatus
VM_BackupProtectionStatus = $backupItem.ProtectionStatus
VM_LastBackupStatus = $backupItem.LastBackupStatus
VM_LastBackupTime = $backupItem.LastBackupTime
VM_BackupDeleteState = $backupItem.DeleteState
VM_BackupLatestRecoveryPoint = $backupItem.LatestRecoveryPoint
VM_Id = $vm.Id
RecoveryVault_ResourceGroupName = $vmBackupVault.ResourceGroupName
RecoveryVault_Location = $vmBackupVault.Location
RecoveryVault_SubscriptionId = $vmBackupVault.ID
}) #[void]$vmBackupReport.Add([PSCustomObject]#{
} #foreach ($vm in $vms)
} #Process
end
{
$vmBackupReport
} #end
You can try this sample script from the MS doc for looping through multiple subscriptions.
Connect-AzAccount
$SubscriptionList = Get-AzSubscription
foreach ($Id in $SubscriptionList)
{
#Provide the subscription Id where the VMs reside
$subscriptionId = $Id
#Provide the name of the csv file to be exported
$reportName = "myReport.csv"
Select-AzSubscription $subscriptionId
$report = #()
$vms = Get-AzVM
$publicIps = Get-AzPublicIpAddress
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}
foreach ($nic in $nics)
{
$info = "" | Select VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup, subscriptionId
$vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id
foreach($publicIp in $publicIps)
{
if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id)
{
$info.PublicIPAddress = $publicIp.ipaddress
}
}
$info.subscriptionId = $subscriptionId
$info.OsType = $vm.StorageProfile.OsDisk.OsType
$info.VMName = $vm.Name
$info.ResourceGroupName = $vm.ResourceGroupName
$info.Region = $vm.Location
$info.VmSize = $vm.HardwareProfile.VmSize
$info.VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3]
$info.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1]
$info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress
$info.NicName = $nic.Name
$info.ApplicationSecurityGroup = $nic.IpConfigurations.ApplicationSecurityGroups.Id
$report+=$info
}
$report | ft subscriptionId, VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup
$report | Export-CSV "$home/$reportName"
}
References: Collect details about all VMs in a subscription with PowerShell - Azure Virtual Machines | Microsoft Docs , Iterate through subscriptions using an Array to avoid manual work · Issue #50670 · MicrosoftDocs/azure-docs · GitHub and powershell - How to loop through multiple Azure subscriptions on Azure Pipelines Yaml? - Stack Overflow

List all Storage Accounts and containers in Aure w/Powershell

I am a novice at Powershell. I have been tasked with listing all storage accounts, along with all the underlying containers, for one of our Azure subscriptions. So I looked to see if a script existed, but couldn't find one. I then set about heavily modifying one that was originally for something different. Figured it would be a good learning exercise.
So what I am wanting my script to do is to work through the subscription, spit out all the storage accounts, and then go through each storage account to list all the containers within them. There are dozens of accounts to go through, each with multiple containers. Once done, throw it into a formatted CSV.
So far I can get the script to output the storage accounts, but nothing more than that. I am running this on my own test environment before hitting the live one, just in case.
Hoping that someone can throw some pointers as to what I'm doing wrong.
Script as below:
##################################################
# Gather storage account information
# across all subscriptions
##################################################
Write-Host "Gathering storage account information...`n"
[System.Collections.ArrayList]$saUsage = New-Object -TypeName System.Collections.ArrayList
# Loop through each subscription
foreach ($subscriptionId in $SubscriptionIDs) {
# Set context to the subscription
Select-AzSubscription -SubscriptionId $subscriptionID | Out-Null
$context = Get-AzContext
Write-Host "The subscription context is set to: $($context.Name)`n"
$storageAccounts = Get-AzResource -ResourceType 'Microsoft.Storage/storageAccounts'
$containers = Get-AzResource -ResourceType
'Microsoft.Storage/storageAccounts/blobServices/containers'
}
# Check the account can access storage accounts within the subscription
if (!$storageAccounts) {
Write-Host -ForegroundColor Red "Account '$($context.Account.Id)' does not have access to any
storage accounts in subscription '$($context.Name)'"
$storageAccounts | Format-Table StorageAccountName, ResourceGroupName, Location
continue
}
Write-Host "Progress Status:"
[int]$i = 1
Loop through each storage account and gather usage information
foreach ($storageAccount in $storageAccounts) {
Write-Host "[$i of $($storageAccounts.Count)] $($storageAccount.Name)"
$i++
Gather storage account details and store as a PS custom object
$StorageAccountDetails = [ordered]#{
SubscriptionName = $context.Subscription.Name
SubscrpitionID = $context.Subscription.Id
StorageAccountName = $storageAccount.Name
ContainerName = $container.containerName
ResourceGroup = $storageAccount.ResourceGroupName
Location = $storageAccount.Location
}
$saUsage.add((New-Object psobject -Property $StorageAccountDetails)) | Out-Null
}
foreach($container in $storageAccount.BlobContainersOperationsExtensions.get) {
if($container.containerName) {
# Gather storage account container details and store as a PS custom object
$StorageAccountDetails = [ordered]#{
SubscriptionName = $context.Subscription.Name
SubscrpitionID = $context.Subscription.Id
StorageAccountName = $storageAccount.Name
ContainerName = $container.containerName
ResourceGroup = $storageAccount.ResourceGroupName
Location = $storageAccount.Location
}
}
$saUsage.add((New-Object psobject -Property $StorageAccountDetails)) | Out-Null
}
# Output storage account usage results to .CSV
if($saUsage) {
# Output to CSV
$saUsage | Export-Csv -Path $SAOutputPath -NoTypeInformation
Write-Output "`nExported storage account usage report at $SAOutputPath`n"
}
Regarding the issue, please refer to the following script
$context = Get-AzContext
$storageAccounts = Get-AzResource -ResourceType 'Microsoft.Storage/storageAccounts'
[System.Collections.ArrayList]$saUsage = New-Object -TypeName System.Collections.ArrayList
foreach ($storageAccount in $storageAccounts) {
#list containers
$conatiners= Get-AzRmStorageContainer -ResourceGroupName $storageAccount.ResourceGroupName -StorageAccountName $storageAccount.Name
if($conatiners -ne $null){
foreach($container in $conatiners){
$StorageAccountDetails = [ordered]#{
SubscriptionName = $context.Subscription.Name
SubscrpitionID = $context.Subscription.Id
StorageAccountName = $storageAccount.Name
ContainerName = $container.Name
ResourceGroup = $storageAccount.ResourceGroupName
Location = $storageAccount.Location
}
$saUsage.add((New-Object psobject -Property $StorageAccountDetails)) | Out-Null
}
}else{
$StorageAccountDetails = [ordered]#{
SubscriptionName = $context.Subscription.Name
SubscrpitionID = $context.Subscription.Id
StorageAccountName = $storageAccount.Name
ContainerName = $null
ResourceGroup = $storageAccount.ResourceGroupName
Location = $storageAccount.Location
}
$saUsage.add((New-Object psobject -Property $StorageAccountDetails)) | Out-Null
}
}
$saUsage | Export-Csv -Path e:\test.csv -NoTypeInformation

How to get the list of azure servers having Auto-Shutdown disabled using PowerShell?

I want to get the list of azure servers having auto-shutdown disabled on them, I have the below script but the issue with the script is that it gets the list of RG's under the Subscription GUID but repeats the output after every loop.
Import-AzureRmContext -Path "$PSScriptRoot\AzureProfile.json"
Select-AzureRmSubscription -SubscriptionId {subscriptionId}
[array]$ResourceGroupArray = Get-AzureRMVm | Select-Object -Property ResourceGroupName, Name, VmId
foreach ($resourceGroup in $ResourceGroupArray){
$targetResourceId = (Get-AzureRmVM -ResourceGroupName $resourcegroup.ResourceGroupName -Name $resourceGroup.Name).Id
$shutdownInformation = (Get-AzureRmResource -ResourceGroupName $resourcegroup.ResourceGroupName -ResourceType Microsoft.DevTestLab/schedules -Expandproperties).Properties
Write-Host "ID: " $targetResourceId
$shutdownInformation
The output for each VM is displayed in the following format,
What I want is simple, I want the VM name and its status of Auto-shutdown to be displayed on the screen so that its easy for me to find out which all VM have auto-shutdown currently disabled on them.
Any help related to this would be helpful.
You just need to get the microsoft.devtestlab/schedules resource ID using:
/subscriptions/{subscriptionId}/resourceGroups/{rgName}/providers/microsoft.devtestlab/schedules/shutdown-computevm-{vmName}
Then iterate over all your VMs using Get-AzVM, Get the microsoft.devtestlab/schedules resource using Get-AzResource, then output VM name and status into a table using Format-Table.
$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Set-AzContext -SubscriptionId $subscriptionId
& {
foreach ($vm in Get-AzVM) {
try {
$shutdownResource = Get-AzResource `
-ResourceId "/subscriptions/$subscriptionId/resourceGroups/$($vm.ResourceGroupName)/providers/microsoft.devtestlab/schedules/shutdown-computevm-$($vm.Name)" `
-ErrorAction Stop
[PSCustomObject]#{
VMName = $vm.Name
ShutdownStatus = $shutdownResource.Properties.status
}
}
catch {
[PSCustomObject]#{
VMName = $vm.Name
ShutdownStatus = $_.Exception.Message
}
}
}
} | Format-Table -AutoSize
To set the context to the correct subscription, we can use Set-AzContext.
The above however is using the latest Az modules. You can do the same using the equivalent AzureRm modules.
$subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Set-AzureRmContext -SubscriptionId $subscriptionId
& {
foreach ($vm in Get-AzureRmVM) {
try {
$shutdownResource = Get-AzureRmResource `
-ResourceId "/subscriptions/$subscriptionId/resourceGroups/$($vm.ResourceGroupName)/providers/microsoft.devtestlab/schedules/shutdown-computevm-$($vm.Name)" `
-ErrorAction Stop
[PSCustomObject]#{
VMName = $vm.Name
ShutdownStatus = $shutdownResource.Properties.status
}
}
catch {
[PSCustomObject]#{
VMName = $vm.Name
ShutdownStatus = $_.Exception.Message
}
}
}
} | Format-Table -AutoSize
Although I do recommend moving to the Az module since support for AzureRm is ending December 2020. You can read the documentation for more information about this.
The above code should give you an output similar to the following
VMName ShutdownStatus
------ --------------
vm1 Enabled
vm2 Disabled
Update
The Call operator & is used here to run the for loop as a script block. You can read more about this in about_Script_Blocks.
Try something like this to get the auto-shutdown status of all VMs. Instead of trying to get the schedules inside the loop, get all the ones in the subscription and match them based on the VM's full resource Id.
[array]$VMArray = Get-AzureRMVm | Select-Object -Property ResourceGroupName, Name, VmId, Id
$ShutdownInformation = (Get-AzureRmResource -ResourceType Microsoft.DevTestLab/schedules -Expandproperties).Properties
foreach($vm in $VMArray) {
$ShutdownStatus = "Not Configured"
$Schedule = $ShutdownInformation | Where-Object { $_.targetResourceId -eq $vm.Id } | Select -First 1
if($Schedule -ne $null) {
$ShutdownStatus = $Schedule.status
}
Write-Host $vm.VmId $ShutdownStatus
}

Azure deployment slots - Adding it to a virtual network using powershell

I'm trying to add an azure deployment slot to a virtual network. Below is the powershell script I'm currently using for adding the webapp to the Vnet, which is working fine:
#Property array with the SubnetID
$properties = #{
subnetResourceId = "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/resourceGroupName/providers/Microsoft.Network/virtualNetworks/vnetName/subnets/subnetName"
}
#Creation of the VNet integration
$vnetParams = #{
ResourceName = "WebappName/VirtualNetwork"
Location = "South Central US"
ResourceGroupName = "resourceGroupName"
ResourceType = "Microsoft.Web/sites/networkConfig"
PropertyObject = $properties
}
New-AzResource #vnetParams -Force
How do I change the above script to work with the deployment slot of the same webapp?
Thanks in advance.
You could change your code like this. Note the change of ResourceName and ResourceType.
#Property array with the SubnetID
$properties = #{
subnetResourceId = "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/resourceGroupName/providers/Microsoft.Network/virtualNetworks/vnetName/subnets/subnetName"
}
#Creation of the VNet integration
$vnetParams = #{
ResourceName = "WebappName/slot/VirtualNetwork"
Location = "South Central US"
ResourceGroupName = "resourceGroupName"
ResourceType = "Microsoft.Web/sites/slots/networkConfig"
PropertyObject = $properties
}
New-AzResource #vnetParams -Force

Resources