您好,登錄后才能下訂單哦!
這次來分享一個自己寫的腳本,任何腳本當然都是有原始需求推動的,這個腳本的功能其實很簡單,他可以幫助我們快速篩選出Azure賬號中SQL Server的VM,寫這個腳本的原因也是因為有人問,現在環境中有哪些VM是SQL Server的,通過平臺本身的Portal其實很難篩選出來這些信息,所以特地寫了一個腳本,當然,這個腳本還是有一些限制,只能篩選出Azure VM+SQL License模式的虛擬機,對于直接在VM內部安裝SQL Server的虛擬機,因為平臺本身不會記錄這類的信息,所以從平臺層面是沒辦法篩選出來的
以下是腳本的內容,分享一下
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文件中,便于整理
因為隱私原因,細節的信息就不展示了哈,各位可以根據需要使用
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。