此次來分享一個本身寫的腳本,任何腳本固然都是有原始需求推進的,這個腳本的功能其實很簡單,他能夠幫助咱們快速篩選出Azure帳號中SQL Server的VM,寫這個腳本的緣由也是由於有人問,如今環境中有哪些VM是SQL Server的,經過平臺自己的Portal其實很難篩選出來這些信息,因此特意寫了一個腳本,固然,這個腳本仍是有一些限制,只能篩選出Azure VM+SQL License模式的虛擬機,對於直接在VM內部安裝SQL Server的虛擬機,由於平臺自己不會記錄這類的信息,因此從平臺層面是沒辦法篩選出來的ide
如下是腳本的內容,分享一下orm
function Write-DateTimeMessage { param ( [parameter(Mandatory = $false)] [switch]$Warning, [parameter(Mandatory = $true)] [string]$Message, [parameter(Mandatory = $false)] [string]$ForegroundColor ) if ($Warning) { Write-Warning ($(Get-Date -UFormat '%Y/%m/%d %H:%M:%S') + " * " + $Message) } else { if ($ForegroundColor) { Write-Host ($(Get-Date -UFormat '%Y/%m/%d %H:%M:%S') + " * " + $Message) -ForegroundColor $ForegroundColor } else { Write-Host ($(Get-Date -UFormat '%Y/%m/%d %H:%M:%S') + " * " + $Message) } } } [pscustomobject[]]$SQLVMObjects = $null $Subscriptions = Get-AzureRmSubscription foreach ($subscription in $Subscriptions) { "Querying subscription:" $SubscriptionID = $Subscription.Id $SubscriptionName = $Subscription.Name Select-AzureRmSubscription -SubscriptionId $SubscriptionID -InformationAction SilentlyContinue Get-AzureRmResourceGroup | %{ $RG = $_ Write-DateTimeMessage -Message "Checking Resource Group $($RG.ResourceGroupName)" $AzureVMs = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName if ($null -ne $AzureVMs) { $AzureVMs | %{ $AzureVM = $_ if($AzureVM.StorageProfile.ImageReference.Publisher -like "*SQLServer*") { Write-DateTimeMessage -Message "Find SQL Server VM $($AzureVM.Name) in resource group $($RG.ResourceGroupName)" -Warning $SQLVMObject = New-Object -TypeName psobject $SQLVMObject | Add-Member -MemberType NoteProperty -Name SubscriptionName -Value $SubscriptionName $SQLVMObject | Add-Member -MemberType NoteProperty -Name SubscriptionID -Value $SubscriptionID $SQLVMObject | Add-Member -MemberType NoteProperty -Name AzureVMName -Value $AzureVM.Name $SQLVMObject | Add-Member -MemberType NoteProperty -Name ResourceGroupName -Value $AzureVM.ResourceGroupName $SQLVMObject | Add-Member -MemberType NoteProperty -Name Location -Value $AzureVM.Location $SQLVMObjects += $SQLVMObject } } } } } $OutputPath = Join-Path -Path ([Environment]::GetFolderPath("Desktop")) -ChildPath ("SQLVM-" + $(Get-Date -Format "yyyyMMdd-HHmmss") + ".csv") if ($null -ne $SQLVMObjects) { $SQLVMObjects | Export-Csv -NoTypeInformation -LiteralPath $OutputPath Write-DateTimeMessage -Message "Please check $OutputPath" -Warning } else { Write-DateTimeMessage "Maybe no SQL VM in the environment or didn't get information, please check" -warning }
運行的方法很是簡單,直接運行命令便可,如下是一些截圖圖片
運行結束後,會將信息導出到CSV文件中,便於整理ip
由於隱私緣由,細節的信息就不展現了哈,各位能夠根據須要使用get