azure Powershell -脚本在函数外部工作,但不在函数内部工作

igsr9ssn  于 2023-10-22  发布在  Shell
关注(0)|答案(1)|浏览(137)

我正在尝试编写一个简单的函数,从Azure获取VM的备份信息,然后输出。我想到了下面这个。如果它没有放在一个函数中,当它放在一个函数中时,它只运行,但变量$vmBackupReport只是空的。我需要把它放在一个函数中,这样我就可以为Prod和Test运行两个单独的查询。脚本运行时不会出现任何错误。

Function createReport {
    Write-Host "Collecing Azure virtual machine Information" -BackgroundColor DarkGreen
        if(!(gc $path)) {
            Write-Host "Please provide a List of the virtual Machines in this file: 'C:\VmListFlurin.txt'" -BackgroundColor Red
            exit 1
        }
        $vms = @()
        foreach ($vmname in gc $path)
        {
            $vms += Get-AzVM -Name $vmname

            if(!(Get-AzVM -Name $vmname)) {
                Write-Host "VM $($vmname) not found, check VM name and Subscription and try again!" -BackgroundColor Red
            }
            
        }
 
        if($vms) {
            Write-Host "Collecting all Backup Recovery Vault information" -BackgroundColor DarkGreen
        }
        else {
            exit 1
        }

        $backupVaults = Get-AzRecoveryServicesVault

    #---------------Part 2

    $vmBackupReport = [System.Collections.ArrayList]::new()
    foreach ($vm in $vms) 
    {
        $recoveryVaultInfo = Get-AzRecoveryServicesBackupStatus -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Type 'AzureVM'
        if ($recoveryVaultInfo.BackedUp -eq $true)
        {
            Write-Host "$($vm.Name) - BackedUp : Yes"

            $vmBackupVault = $backupVaults | Where-Object {$_.ID -eq $recoveryVaultInfo.VaultId}

            $container = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -VaultId $vmBackupVault.ID -FriendlyName $vm.Name 
            $backupItem = Get-AzRecoveryServicesBackupItem -Container $container -WorkloadType AzureVM -VaultId $vmBackupVault.ID

            #---------------Get All available restore points-------------------------#
            $rententionDays = 730
            $vaultName = $vmBackupVault.Name
            $vaultResourceGroup = $vmBackupVault.ResourceGroupName
            $friendlyName = $vm.name

 
            $vault = Get-AzRecoveryServicesVault -ResourceGroupName $vaultResourceGroup -Name $vaultName
            $Container = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -Status Registered -FriendlyName $friendlyName -VaultId $vault.ID
            $BackupItem = Get-AzRecoveryServicesBackupItem -Container $Container -WorkloadType AzureVM -VaultId $vault.ID
 
            $startingPoint = -25
            $finishingPoint = 0
            $jobsArray = @()
 
            Do {
 
            $StartDate = (Get-Date).AddDays($startingPoint)
            $EndDate = (Get-Date).AddDays($finishingPoint)
            $RP = Get-AzRecoveryServicesBackupRecoveryPoint -Item $BackupItem -StartDate $Startdate.ToUniversalTime() -EndDate $Enddate.ToUniversalTime() -VaultId $vault.ID
            $jobsArray += $RP
            $startingPoint = $startingPoint - 25
            $finishingPoint = $finishingPoint -25
            }until($startingPoint -le -($retentionDays))
 
            #$jobsArray | FT -AutoSize -Property RecoveryPointid, RecoveryPointTime, RecoveryPointType
            #---------------Get All available restore points-------------------------#

            [void]$vmBackupReport.Add([PSCustomObject]@{
            VM_Name = $vm.Name
            VM_ResourceGroupName = $vm.ResourceGroupName
            VM_BackedUp = $recoveryVaultInfo.BackedUp
            VM_RecoveryVaultName =  $vmBackupVault.Name
            VM_RecoveryVaultPolicy = $backupItem.ProtectionPolicyName
            VM_BackupHealthStatus = $backupItem.HealthStatus
            VM_BackupProtectionStatus = $backupItem.ProtectionStatus
            VM_LastBackupStatus = $backupItem.LastBackupStatus
            VM_LastBackupTime = $backupItem.LastBackupTime
            VM_BackupDeleteState = $backupItem.DeleteState
            VM_BackupLatestRecoveryPoint = $backupItem.LatestRecoveryPoint
            RecoveryVault_ResourceGroupName = $vmBackupVault.ResourceGroupName
            VM_AllRecoveryPoints = $jobsArray.RecoveryPointTime
        }) 

        

        } 
        else 
        {
            Write-Host "$($vm.Name) - BackedUp : No" -BackgroundColor DarkRed
            $vmBackupVault = $null
            $container =  $null
            $backupItem =  $null
        } 
        
        
    }
}

$path = "C:\VmListTest.txt"
Set-AzContext -Subscription "Test/PreProd" | out-null
createReport;

$path = "C:\VmListProd.txt"
Set-AzContext -Subscription "Produktion" | out-null
createReport;

$vmBackupReport
$vmBackupReport | Out-File -Filepath C:\Backupkontrolle_flurin.txt
pause

我确实已经创建了两个单独的函数,并尝试将Get Data与create Report分开运行,这也没有解决我的错误。
如果我在变量$vmBackupReport之外运行完全相同的代码,看起来像这样(每个VM):

VM_Name                         : VM_Name_value
VM_ResourceGroupName            : VM_ResourceGroupName_value
VM_BackedUp                     : True
VM_RecoveryVaultName            : VM_RecoveryVaultName_value
VM_RecoveryVaultPolicy          : VM_RecoveryVaultPolicy_value
VM_BackupHealthStatus           : Passed
VM_BackupProtectionStatus       : Healthy
VM_LastBackupStatus             : Completed
VM_LastBackupTime               : 04.10.2023 15:20:34
VM_BackupDeleteState            : NotDeleted
VM_BackupLatestRecoveryPoint    : 04.10.2023 15:20:37
RecoveryVault_ResourceGroupName : RecoveryVault_ResourceGroupName_value
VM_AllRecoveryPoints            : {04.10.2023 15:20:37, 03.10.2023 15:20:59, 02.10.2023 15:20:09, 01.10.2023 15:20:27...}

如果我在函数中运行脚本,没有值分配给变量。

z0qdvdin

z0qdvdin1#

问题实际上是,$vmBackupReport是在函数本身而不是外部声明的。我不明白,为什么变量是完全空的,而不是只包含最后一次运行的结果。无论如何,从函数中删除$vmBackupReport = [System.Collections.ArrayList]::new()行并将其向下移动后,它都可以完美地工作。最终脚本:

Function createReport {
    Write-Host "Collecing Azure virtual machine Information" -BackgroundColor DarkGreen
        if(!(gc $path)) {
            Write-Host "Please provide a List of the virtual Machines in this file: 'C:\VmListFlurin.txt'" -BackgroundColor Red
            exit 1
        }
        $vms = @()
        foreach ($vmname in gc $path)
        {
            $vms += Get-AzVM -Name $vmname

            if(!(Get-AzVM -Name $vmname)) {
                Write-Host "VM $($vmname) not found, check VM name and Subscription and try again!" -BackgroundColor Red
            }
            
        }
 
        if($vms) {
            Write-Host "Collecting all Backup Recovery Vault information" -BackgroundColor DarkGreen
        }
        else {
            exit 1
        }

        $backupVaults = Get-AzRecoveryServicesVault

    #---------------Part 2

    
    foreach ($vm in $vms) 
    {
        $recoveryVaultInfo = Get-AzRecoveryServicesBackupStatus -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Type 'AzureVM'
        if ($recoveryVaultInfo.BackedUp -eq $true)
        {
            Write-Host "$($vm.Name) - BackedUp : Yes"

            $vmBackupVault = $backupVaults | Where-Object {$_.ID -eq $recoveryVaultInfo.VaultId}

            $container = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -VaultId $vmBackupVault.ID -FriendlyName $vm.Name 
            $backupItem = Get-AzRecoveryServicesBackupItem -Container $container -WorkloadType AzureVM -VaultId $vmBackupVault.ID

            #---------------Get All available restore points-------------------------#
            $rententionDays = 730
            $vaultName = $vmBackupVault.Name
            $vaultResourceGroup = $vmBackupVault.ResourceGroupName
            $friendlyName = $vm.name

 
            $vault = Get-AzRecoveryServicesVault -ResourceGroupName $vaultResourceGroup -Name $vaultName
            $Container = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -Status Registered -FriendlyName $friendlyName -VaultId $vault.ID
            $BackupItem = Get-AzRecoveryServicesBackupItem -Container $Container -WorkloadType AzureVM -VaultId $vault.ID
 
            $startingPoint = -25
            $finishingPoint = 0
            $jobsArray = @()
 
            Do {
 
            $StartDate = (Get-Date).AddDays($startingPoint)
            $EndDate = (Get-Date).AddDays($finishingPoint)
            $RP = Get-AzRecoveryServicesBackupRecoveryPoint -Item $BackupItem -StartDate $Startdate.ToUniversalTime() -EndDate $Enddate.ToUniversalTime() -VaultId $vault.ID
            $jobsArray += $RP
            $startingPoint = $startingPoint - 25
            $finishingPoint = $finishingPoint -25
            }until($startingPoint -le -($retentionDays))
 
            #$jobsArray | FT -AutoSize -Property RecoveryPointid, RecoveryPointTime, RecoveryPointType
            #---------------Get All available restore points-------------------------#

            [void]$vmBackupReport.Add([PSCustomObject]@{
            VM_Name = $vm.Name
            VM_ResourceGroupName = $vm.ResourceGroupName
            VM_BackedUp = $recoveryVaultInfo.BackedUp
            VM_RecoveryVaultName =  $vmBackupVault.Name
            VM_RecoveryVaultPolicy = $backupItem.ProtectionPolicyName
            VM_BackupHealthStatus = $backupItem.HealthStatus
            VM_BackupProtectionStatus = $backupItem.ProtectionStatus
            VM_LastBackupStatus = $backupItem.LastBackupStatus
            VM_LastBackupTime = $backupItem.LastBackupTime
            VM_BackupDeleteState = $backupItem.DeleteState
            VM_BackupLatestRecoveryPoint = $backupItem.LatestRecoveryPoint
            RecoveryVault_ResourceGroupName = $vmBackupVault.ResourceGroupName
            VM_AllRecoveryPoints = $jobsArray.RecoveryPointTime
        }) 

        

        } 
        else 
        {
            Write-Host "$($vm.Name) - BackedUp : No" -BackgroundColor DarkRed
            $vmBackupVault = $null
            $container =  $null
            $backupItem =  $null
        } 
        
        
    }
}

$vmBackupReport = [System.Collections.ArrayList]::new()
$path = "C:\VmListTest.txt"
Set-AzContext -Subscription "Test/PreProd" | out-null
createReport;

$path = "C:\VmListProd.txt"
Set-AzContext -Subscription "Produktion" | out-null
createReport;

$script:vmBackupReport

相关问题