【问题标题】:I ned to get all the attached resources to VM using powershell我需要使用 powershell 将所有附加资源获取到 VM
【发布时间】:2021-12-29 06:03:22
【问题描述】:

能否请您帮助我在 powershell 脚本中获取附加资源到 VM,例如负载均衡器、存储、公共 Ip、磁盘、StorageAccount 等...

【问题讨论】:

  • 嗨 Nagendra kumar;你的问题对于这个网站来说实在是太宽泛了,它是关于集中的具体问题。但是,如果您已经尝试过一些 powershell 命令并在使用它们时发现了具体问题,那么这里适合提问。
  • 如果回答对您有帮助,请Accept it as an Answer,以便遇到相同问题的其他人可以找到此解决方案并解决他们的问题。

标签: azure azure-powershell


【解决方案1】:

使用 PowerShell Az Module,您可以获得附加到 Azure VM 的资源。

您可以使用以下内容:

Write-Host -NoNewline -ForegroundColor Green "Please enter the VM name you would like to get Details:"
$VMName = Read-Host
$vm = Get-AzVm -Name $VMName
if ($vm) {
$RGName=$vm.ResourceGroupName
Write-Host -ForegroundColor Cyan 'Resource Group Name is identified as-' $RGName
$diagSa = [regex]::match($vm.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?)\.').groups[1].value
$osDiskName = $vm.StorageProfile.OSDisk.Name
Write-Host -ForegroundColor Cyan 'OS Disk Name is identified as-' $OsDiskName
$datadisks = $vm.StorageProfile.DataDisks
$ResourceID = (Get-Azdisk -Name $osDiskName).id
Write-Host -ForegroundColor Cyan 'OS Disk Resource ID is identified as-' $ResourceID
if ($vm.StorageProfile.DataDisks.Count -gt 0) {
    foreach ($datadisks in $vm.StorageProfile.DataDisks){
    $datadiskname=$datadisks.name
    Write-Host -ForegroundColor Cyan 'Data Disk is identified as-' $datadiskname
    $ResourceID = (Get-Azdisk -Name $datadiskname).id
    Write-Host -ForegroundColor Cyan 'Data Disk Disk Resource ID is identified as-' $ResourceID
    }
}
$azResourceParams = @{
 'ResourceName' = $VMName
 'ResourceType' = 'Microsoft.Compute/virtualMachines'
 'ResourceGroupName' = $RGName
}
$vmResource = Get-AzResource @azResourceParams
$vmId = $vmResource.Properties.VmId
$diagContainerName = ('bootdiagnostics-{0}-{1}' -f $vm.Name.ToLower().Substring(0, $i), $vmId)
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
$saParams = @{
  'ResourceGroupName' = $diagSaRg
  'Name' = $diagSa
}
if ($diagSa){
Get-AzStorageAccount @saParams | Get-AzStorageContainer | where {$_.Name-eq $diagContainerName} | Remove-AzStorageContainer -Force
}
else {
Write-Host -ForegroundColor Green "No Boot Diagnostics Disk found attached to the VM!"
}
foreach($nicUri in $vm.NetworkProfile.NetworkInterfaces.Id) {
   $nic = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $nicUri.Split('/')[-1]
   Write-Host -ForegroundColor Cyan 'NIC Resource ID is identified as-' $nicUri
   foreach($ipConfig in $nic.IpConfigurations) {
     if($ipConfig.PublicIpAddress -ne $null){
     Write-Host -ForegroundColor Cyan 'Public IP is identified as-' $ipconfig.PublicIpAddressText
     Write-Host -ForegroundColor Cyan 'Private IP is identified as-' $ipconfig.PrivateIpAddress
     ## if you have app gateway or load balancer then it can be queried here as  $ipConfig.GatewayLoadBalancerText ,$ipconfig.ApplicationGatewayBackendAddressPoolsText ,$ipconfig.LoadBalancerBackendAddressPoolsText
     }
    }
}
}
else{
Write-Host -ForegroundColor Red "The VM name entered doesn't exist in your connected Azure Tenant! Kindly check the name entered and restart the script with correct VM name..."
}

输出:

【讨论】:

    猜你喜欢
    • 2022-01-13
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2021-02-14
    • 1970-01-01
    • 2013-05-21
    • 2013-07-09
    相关资源
    最近更新 更多