Azure 自动化帐户 Powershell Runbook 与 Azure 快照发生异常错误

问题描述 投票:0回答:1

我正在尝试编写一个脚本,通过使用托管标识完成的身份验证部分查找标签来自动启动或停止资源组中的 Azure VM。此外,当虚拟机启动时,脚本将拍摄其操作系统磁盘的快照。 一切工作正常,直到我添加了与快照部分链接的所有内容。

您将在下面找到我使用的整个脚本:

Param(
[Parameter(Mandatory=$true)]
[Boolean]
$Shutdown,
[Parameter(Mandatory=$true)]
[String]
$ResourceGroup,
[Parameter(Mandatory=$true)]
[String]
$TagName,
[Parameter(Mandatory=$true)]
[String]
$TagValue,
[Parameter(Mandatory=$true)]
[String]
$ManagedIdentityId
) 

$location = 'West Europe'
$datetime = Get-Date -Format "dd-MM-yyyy"

try
{
# Logging into Azure using Managed Identity
    Connect-AzAccount -Identity -AccountId $ManagedIdentityId
}
catch 
{ 
    Write-Error -Message $_.Exception 
    throw $_.Exception 
}
"Getting VMs with appropriate tags"
# We get all azure resources inside a specific resource group having a specific tag (Key and value). After that we apply a filter to get only virtual machines.     
$vms =  Get-AzResource -ResourceGroupName $ResourceGroup -TagName $TagName -TagValue $TagValue | where {$_.ResourceType -like "Microsoft.Compute/virtualMachines"}

# Test if the script has to stop or start these virtual machines that we previously got. This test is based on the parameter Shutdown ( Shutdown == true -> Stop VMs | Shutdown == false -> start VMs) 
foreach ($vm in $vms) {     
    if($Shutdown){
        Write-Output "Stopping $($vm.Name)";     
        Stop-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Force;
    }
    else {    
        $snapshotConfig =  New-AzSnapshotConfig -SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id -Location $location -CreateOption copy -SkuName Standard_LRS
        $snapshotName = $vm.Name + 'OsDisk_snapshot_' + $datetime"  
        Write-Output "Taking Snapshots of the $($vm.Name) OS disk";
        New-AzSnapshot `
            -SnapshotName $snapshotName `
            -Snapshot $snapshotConfig `
            -ResourceGroupName $ResourceGroup
        Write-Output "Starting $($vm.Name)"; 
        Start-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name;
    }
}

当我尝试启动脚本时,出现以下错误:

At line:44 char:66 + $snapshotName = $vm.Name + 'OsDisk_snapshot_' + $datetime" + ~ Unexpected token '" Write-Output "' in expression or statement. At line:45 char:23 + Write-Output "Taking Snapshots of the $($vm.Name) OS disk"; + ~~~~~~ Unexpected token 'Taking' in expression or statement. At line:50 char:43 + Write-Output "Starting $($vm.Name)"; + ~~ The string is missing the terminator: ". At line:42 char:10 + else { + ~ Missing closing '}' in statement block or type definition. At line:37 char:23 + foreach ($vm in $vms) { + ~ Missing closing '}' in statement block or type definition.

我不太理解这些错误,因为在我添加这段代码之前它工作得很好(虚拟机可以毫无问题地启动和停止):

        $snapshotConfig =  New-AzSnapshotConfig -SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id -Location $location -CreateOption copy -SkuName Standard_LRS
        $snapshotName = $vm.Name + 'OsDisk_snapshot_' + $datetime"  
        Write-Output "Taking Snapshots of the $($vm.Name) OS disk";
        New-AzSnapshot `
            -SnapshotName $snapshotName `
            -Snapshot $snapshotConfig `
            -ResourceGroupName $ResourceGroup

我在这里遗漏了什么吗?

我尝试删除 Write-Output 命令,但当我这样做时,我遇到了其他异常错误,其中我没有更改的其他代码行缺少意外标记的字符,所以我不太明白。

azure powershell azure-automation
1个回答
0
投票

错误与所提供代码中

$datetime
之后添加的额外双引号 (") 有关。

删除下一行末尾的双引号并执行如下所示。

$snapshotName = $vm.Name + 'OsDisk_snapshot_' + $datetime 

修改后的PS脚本:

Param(
[Parameter(Mandatory=$true)]
[Boolean]
$Shutdown,
[Parameter(Mandatory=$true)]
[String]
$ResourceGroup,
[Parameter(Mandatory=$true)]
[String]
$TagName,
[Parameter(Mandatory=$true)]
[String]
$TagValue
) 

$location = 'eastus'
$datetime = Get-Date -Format "dd-MM-yyyy"
     
$vms =  Get-AzResource -ResourceGroupName $ResourceGroup -TagName $TagName -TagValue $TagValue | where {$_.ResourceType -like "Microsoft.Compute/virtualMachines"}

foreach ($vm in $vms) {     
    if($Shutdown){
        Write-Output "Stopping $($vm.Name)";     
        Stop-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Force
    }
    else{    
        $snapshotConfig =  New-AzSnapshotConfig -SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id -Location $location -CreateOption copy -SkuName Standard_LRS
        $snapshotName = $vm.Name + 'OsDisk_snapshot_' + $datetime
        Write-Output "Taking Snapshots of the $($vm.Name) OS disk"
        New-AzSnapshot `
            -SnapshotName $snapshotName `
            -Snapshot $snapshotConfig `
            -ResourceGroupName $ResourceGroup
        Write-Output "Starting $($vm.Name)"
        Start-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
    }
}

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.