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

栏目: 数据库 · 发布时间: 7年前

内容简介:这次来分享一个自己写的脚本,任何脚本当然都是有原始需求推动的,这个脚本的功能其实很简单,他可以帮助我们快速筛选出Azure账号中SQL Server的VM,写这个脚本的原因也是因为有人问,现在环境中有哪些VM是SQL Server的,通过平台本身的Portal其实很难筛选出来这些信息,所以特地写了一个脚本,当然,这个脚本还是有一些限制,只能筛选出Azure VM+SQL License模式的虚拟机,对于直接在VM内部安装SQL Server的虚拟机,因为平台本身不会记录这类的信息,所以从平台层面是没办法筛选

这次来分享一个自己写的脚本,任何脚本当然都是有原始需求推动的,这个脚本的功能其实很简单,他可以帮助我们快速筛选出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
}

运行的方法非常简单,直接运行命令即可,以下是一些截图

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

运行结束后,会将信息导出到CSV文件中,便于整理

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

因为隐私原因,细节的信息就不展示了哈,各位可以根据需要使用


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Cracking the Coding Interview

Cracking the Coding Interview

Gayle Laakmann McDowell / CareerCup / 2015-7-1 / USD 39.95

Cracking the Coding Interview, 6th Edition is here to help you through this process, teaching you what you need to know and enabling you to perform at your very best. I've coached and interviewed hund......一起来看看 《Cracking the Coding Interview》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具