【问题标题】:Azure Powershell Question for Virtual Machine虚拟机的 Azure Powershell 问题
【发布时间】:2021-09-21 23:28:42
【问题描述】:

我正在审查一个脚本,该脚本应该删除一个虚拟机以及归属于该虚拟机的所有资源

    Write-Host -NoNewline -ForegroundColor Green "Please enter the VM name you would like to remove:"
$VMName = Read-Host
$vm = Get-AzVm -Name $VMName
if ($vm) {
$RGName=$vm.ResourceGroupName
Write-Host -ForegroundColor Cyan 'Resource Group Name is identified as-' $RGName
#boot diagnostics container auto generated in storage account. Auto delete this storageURI property 
$diagSa = [regex]::match($vm.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?)\.').groups[1].value
Write-Host -ForegroundColor Cyan 'Marking Disks for deletion...'
$tags = @{"VMName"=$VMName; "Delete Ready"="Yes"}
$osDiskName = $vm.StorageProfile.OSDisk.Name
$datadisks = $vm.StorageProfile.DataDisks
$ResourceID = (Get-Azdisk -Name $osDiskName).id
New-AzTag -ResourceId $ResourceID -Tag $tags | Out-Null
if ($vm.StorageProfile.DataDisks.Count -gt 0) {
    foreach ($datadisks in $vm.StorageProfile.DataDisks){
    $datadiskname=$datadisks.name
    $ResourceID = (Get-Azdisk -Name $datadiskname).id
    New-AzTag -ResourceId $ResourceID -Tag $tags | Out-Null
    }
}
if ($vm.Name.Length -gt 9){
    $i = 9
    }
    else
    {
    $i = $vm.Name.Length - 1
}
$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
}
Write-Host -ForegroundColor Cyan 'Removing Boot Diagnostic disk..'
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!"
}
Write-Host -ForegroundColor Cyan 'Removing Virtual Machine-' $VMName 'in Resource Group-'$RGName '...'
$null = $vm | Remove-AzVM -Force
Write-Host -ForegroundColor Cyan 'Removing Network Interface Cards, Public IP Address(s) used by the VM...'
foreach($nicUri in $vm.NetworkProfile.NetworkInterfaces.Id) {
   $nic = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $nicUri.Split('/')[-1]
   Remove-AzNetworkInterface -Name $nic.Name -ResourceGroupName $vm.ResourceGroupName -Force
   foreach($ipConfig in $nic.IpConfigurations) {
     if($ipConfig.PublicIpAddress -ne $null){
     Remove-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName -Name $ipConfig.PublicIpAddress.Id.Split('/')[-1] -Force
     }
    }
}
Write-Host -ForegroundColor Cyan 'Removing OS disk and Data Disk(s) used by the VM..'
Get-AzResource -tag $tags | where{$_.resourcegroupname -eq $RGName}| Remove-AzResource -force | Out-Null
Write-Host -ForegroundColor Green 'Azure Virtual Machine-' $VMName 'and all the resources associated with the VM were removed sucessfully...'
}
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..."
}

我有一个问题:这段代码到底是做什么的:

$diagSa = [regex]::match($vm.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?)\.').groups[1].value

我知道它与存储 uri 匹配,但如何匹配?为什么需要这个?我不确定 .groups[1].value 指的是什么

【问题讨论】:

    标签: azure powershell


    【解决方案1】:

    $diagSa = [正则表达式]::match($vm.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?).').groups[1].value 我知道它匹配存储 uri,但是如何匹配?

    您在上面的表达式中使用了 [regex] 类型的加速器和匹配方法 ()。

    Match() 方法是一种指示 PowerShell 尝试匹配另一个字符串内部的字符串的方法。 Match() 方法有两个参数;你想匹配的字符串和你想测试的正则表达式。

    只要找到匹配项并使用正则表达式组; (),[regex] 类型加速器具有 Captures 属性。然后,此 Captures 属性具有称为 Groups 的属性。这是一个包含许多匹配属性的集合。该集合中的第二个元素包含匹配的实际值。

    .groups[1].value 指的是什么

    groups[1].values 返回启动诊断容器所在的存储帐户名称。

    为什么需要这样做?

    创建 Azure VM 时,您始终可以选择创建启动诊断容器。这对于解决 VM 启动问题很有用,但在删除 VM 时不会被删除。让我们解决这个问题。

    要删除启动诊断容器,您首先需要确定容器所在的存储帐户的名称。要找到该存储帐户,您必须对 VM 上的 DiagnosticsProfile 对象中存在的 storageUri 属性进行一些解析。

    有关 [regex]::match().group[1].value 表达式的更多信息,请参阅以下博客:

    https://mcpmag.com/articles/2015/09/30/regex-groups-with-powershell.aspx

    【讨论】:

      猜你喜欢
      • 2017-01-02
      • 1970-01-01
      • 2015-09-04
      • 2021-05-26
      • 2015-08-03
      • 1970-01-01
      • 2021-01-02
      • 2019-08-30
      • 1970-01-01
      相关资源
      最近更新 更多