溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用PowerShell快速獲取Azure中的SQL Server VM

發(fā)布時(shí)間:2020-06-03 05:11:07 來源:網(wǎng)絡(luò) 閱讀:1779 作者:mxy00000 欄目:云計(jì)算

    這次來分享一個(gè)自己寫的腳本,任何腳本當(dāng)然都是有原始需求推動(dòng)的,這個(gè)腳本的功能其實(shí)很簡單,他可以幫助我們快速篩選出Azure賬號(hào)中SQL Server的VM,寫這個(gè)腳本的原因也是因?yàn)橛腥藛?,現(xiàn)在環(huán)境中有哪些VM是SQL Server的,通過平臺(tái)本身的Portal其實(shí)很難篩選出來這些信息,所以特地寫了一個(gè)腳本,當(dāng)然,這個(gè)腳本還是有一些限制,只能篩選出Azure VM+SQL License模式的虛擬機(jī),對于直接在VM內(nèi)部安裝SQL Server的虛擬機(jī),因?yàn)槠脚_(tái)本身不會(huì)記錄這類的信息,所以從平臺(tái)層面是沒辦法篩選出來的


    以下是腳本的內(nèi)容,分享一下


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
}



運(yùn)行的方法非常簡單,直接運(yùn)行命令即可,以下是一些截圖

使用PowerShell快速獲取Azure中的SQL Server VM



運(yùn)行結(jié)束后,會(huì)將信息導(dǎo)出到CSV文件中,便于整理

使用PowerShell快速獲取Azure中的SQL Server VM



因?yàn)殡[私原因,細(xì)節(jié)的信息就不展示了哈,各位可以根據(jù)需要使用

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI