使用PowerShell快速获取Azure中的SQL Server VM
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这次来分享一个自己写的脚本,任何脚本当然都是有原始需求推动的,这个脚本的功能其实很简单,他可以帮助我们快速筛选出Azure账号中SQL Server的VM,写这个脚本的原因也是因为有人问,现在环境中有
千家信息网最后更新 2025年12月02日使用PowerShell快速获取Azure中的SQL Server VM
这次来分享一个自己写的脚本,任何脚本当然都是有原始需求推动的,这个脚本的功能其实很简单,他可以帮助我们快速筛选出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-AzureRmSubscriptionforeach ($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文件中,便于整理

因为隐私原因,细节的信息就不展示了哈,各位可以根据需要使用
脚本
信息
平台
运行
原因
选出
原始
内容
办法
功能
命令
层面
截图
文件
方法
模式
环境
细节
账号
还是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
嵌入式兼职平台软件开发
奉贤区创新金融网络技术服务好处
政务网数据库
我的世界服务器怎么确定了
中兴软件开发做什么
东莞市来思网络技术有限公司
数据库引擎组件作用是什么
sql数据库工作时用的多么
我的世界秋之城服务器
马云会不会软件开发
江苏互联网养老软件开发公司
大连高级软件开发培训
can软件开发计划
在数据库中0默认为什么
数据库索引会占用服务器内存吗
服务器三秒一滴
路由器无法解析服务器的dns地址
天津rust服务器托管虚拟主机
和县网络安全哪家好
临沂量化积分管理软件开发公司
软件开发组织方式的演示
服务器为什么有别名
深圳市旭昇互联网科技有限
数据库引擎组件作用是什么
笔记本电脑代理服务器有问题
面试软件开发二面
对中文数据库学习
数据库修改后多久可以同步
鹤壁市网络安全培训
李忘生 服务器