Open BorysMariusz opened 1 year ago
Thanks for reporting! Is it possible to share the script (be sure to remove any sensitive data before posting)?
Yes, should I post it here in the issue ?
$accessToken = Get-AzAccessToken -ErrorAction SilentlyContinue
if (!($accessToken )) { Connect-AzAccount }
Write-Output "Set AZ Context to $AzSubscription" try { $Context = Set-AzContext -Subscription $azsub -ErrorAction Stop } catch { Write-Output "Could not set AZ Context. Error message is: $($_.exception.message)" exit }
try { $AzGallery = Get-AzGallery -ErrorAction Stop } catch { Write-Output "Could not collect AZ Galleries. Error message is: $($_.exception.message)" exit }
foreach ($AzGal in $AzGallery) { Write-Output "Gathering Gallery Image Definitions from Azure Gallery name: $($AzGal.Name)" $AzGaldefinitioninfo = Get-AzGalleryImageDefinition -GalleryName $AzGal.Name -ResourceGroupName $AzGal.ResourceGroupName
foreach ($AzGalDefName in $AzGaldefinitioninfo.Name) {
Write-Output "Gathering versions for VM Image definition: $AzGalDefName"
# gather versions of the image sorted
$AzGalDefImageVersions = Get-AzGalleryImageVersion -GalleryName $AzGal.Name -ResourceGroupName $AzGal.ResourceGroupName -GalleryImageDefinitionName $AzGalDefName | Sort-Object -Property Name -Descending # sort for keeping the latest
# variables to check how many image versions is in the Gallery definition
$AzGalDefImageVersionsCounter = $AzGalDefImageVersions.count
$AzGalDefImageVersionsCounter
if ($AzGalDefImageVersionsCounter -gt 0) {
# loop through version numbers and check the target repliaction location
Foreach ($ImageVersion in $AzGalDefImageVersions ) {
write-output "I am here $($ImageVersion.name) of $AzGalDefName"
$targetRegionCount = $ImageVersion.PublishingProfile.TargetRegions.count
#Write-Output "Version: $($ImageVersion.Name) is published on $targetRegionCount regions"
if (($AzGalDefImageVersions.indexof($ImageVersion) -eq 0) -and ($targetRegionCount -eq 8)) {
write-output "Latest element is replicated correctly between $targetRegionCount regions. "
} elseif (($AzGalDefImageVersions.indexof($ImageVersion) -eq 0) -and ($targetRegionCount -lt 8)) {
Write-Output "Latest version is not replicated correctly. More regions will be added."
$region1 = @{Name='East US 2';ReplicaCount=1}
$region2 = @{Name='Central US';ReplicaCount=1}
$region3 = @{Name='West Europe';ReplicaCount=1}
$region4 = @{Name='North Europe';ReplicaCount=1}
$region5 = @{Name='Australia East';ReplicaCount=1}
$region6 = @{Name='Australia Southeast';ReplicaCount=1}
$region7 = @{Name='Canada Central';ReplicaCount=1}
$region8 = @{Name='Canada East';ReplicaCount=1}
$targetRegions = @($region1,$region2,$region3,$region4,$region5,$region6,$region7,$region8)
#Update-AzGalleryImageVersion -ResourceGroupName $AzGal.ResourceGroupName -GalleryName $AzGal.Name -GalleryImageDefinitionName $AzGalDefName -Name $ImageVersion.Name -ReplicaCount 1 -TargetRegion $targetRegions -AsJob -WhatIf
} elseif ($targetRegionCount -gt 1 ){
Write-Output "Version $($ImageVersion.Name) is not latest, has more then one replica. Replicated regions will be set only for East US 2"
$region1 = @{Name='East US 2';ReplicaCount=1}
$targetRegions = @($region1)
#Update-AzGalleryImageVersion -ResourceGroupName $AzGal.ResourceGroupName -GalleryName $AzGal.Name -GalleryImageDefinitionName $AzGalDefName -Name $ImageVersion.Name -ReplicaCount 1 -TargetRegion $targetRegions -AsJob -WhatIf
} else {
Write-Output "Version $($ImageVersion.Name) is published corectly on $targetRegionCount region"
}
Write-Output "-------------------------------------------------------------------------------------------------------"
}
Write-Output "################################# IMAGE VERSION #################################################"
} else {
Write-Output "VM Image definition: $AzGalDefName has no versions"
}
Write-Output "################################# AZ GEL DEFINITION NAME #################################################"
}
}
From what I understand if the variable $AzGalDefImageVersionsCounter has only one item, (image version) the "INDEXOF" method does not exists. When it has more then one item it is ok. I have checked the get-member output for both of them regardles the amount of image version (zero, one or more items) the Type is the same: Microsoft.Azure.Commands.Compute.Automation.Models.PSGalleryImageVersionList
Any update ?
Sorry for late reponse. Let me loop in compute team.
IMO it looks related to the PowerShell design that a cmdlet can return one or more objects. I can think of two potential ways to work around it, first is to handle the case where it returns exactly one object seperately; second is to use piping, e.g. Get-AzGalleryImageVersion | Foreach-Object { ... }
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @Drewm3, @TravisCragg-MSFT, @nikhilpatel909, @sandeepraichura, @hilaryw29, @GabstaMSFT, @ramankumarlive, @ushnaarshadkhan.
Author: | BorysMariusz |
---|---|
Assignees: | isra-fel |
Labels: | `Compute`, `Service Attention`, `bug`, `customer-reported` |
Milestone: | - |
Thank you for feedback. Yes I have code the if statement when there is only 1 object and I am not using indexof method then. Is that "the issue" and will be fixed someday ?
Thanks for reporting, this issue is being investigated currently. I will keep this thread updated on status.
Thank you. !!!!
Sent from Outlookhttp://aka.ms/weboutlook
From: saraic @.> Sent: Wednesday, April 26, 2023 7:57 PM To: Azure/azure-powershell @.> Cc: Mariusz Borys @.>; Author @.> Subject: Re: [Azure/azure-powershell] [Microsoft.Azure.Commands.Compute.Automation.Models.PSGalleryImageVersionList] does not contain a method named 'indexof' (Issue #21175)
Thanks for reporting, this issue is being investigated currently. I will keep this thread updated on status.
— Reply to this email directly, view it on GitHubhttps://github.com/Azure/azure-powershell/issues/21175#issuecomment-1523831113, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AH2PW3CCYDLS533KQQ3CP33XDFOW3ANCNFSM6AAAAAAVWN3O5Q. You are receiving this because you authored the thread.Message ID: @.***>
Assigning to @haagha
Hi @BorysMariusz Powershell allows IndexOf() to be used on strings. In this case it looks like you are trying to invoke the method on a PSObject with a collection.
hm, understood. thank you for the tip. I will investigate that and get back to you 🙂
Sent from Outlookhttp://aka.ms/weboutlook
From: Haider Agha @.> Sent: Friday, June 9, 2023 7:10 PM To: Azure/azure-powershell @.> Cc: Mariusz Borys @.>; Mention @.> Subject: Re: [Azure/azure-powershell] [Microsoft.Azure.Commands.Compute.Automation.Models.PSGalleryImageVersionList] does not contain a method named 'indexof' (Issue #21175)
Hi @BorysMariuszhttps://github.com/BorysMariusz Powershell allows IndexOf() to be used on strings. In this case it looks like you are trying to invoke the method on a PSObject with a collection.
— Reply to this email directly, view it on GitHubhttps://github.com/Azure/azure-powershell/issues/21175#issuecomment-1584901371, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AH2PW3CF75OZNWLRXS33QXDXKNKHZANCNFSM6AAAAAAVWN3O5Q. You are receiving this because you were mentioned.Message ID: @.***>
@BorysMariusz - let us know if the tip worked for you.
Hi @BorysMariusz Powershell allows IndexOf() to be used on strings. In this case it looks like you are trying to invoke the method on a PSObject with a collection.
As I wrote above: From what I understand if the variable $AzGalDefImageVersionsCounter has only one item, (image version) the "INDEXOF" method does not exists. When it has more then one item it is ok. I have checked the get-member output for both of them regardles the amount of image version (zero, one or more items) the Type is the same: Microsoft.Azure.Commands.Compute.Automation.Models.PSGalleryImageVersionList
I understand that IndexOf is for strings, so why it working in the example I described ? when it is more then 1 item in the AzGalDefImage ?
Hello Please find a screen from running of my script .
Description
I got issue with working with the PowerShell script, no matter if it is created in local or in runbook. I try to clean images from image gallery. When the definition has only one version of image, I can not use .indexof method. I am using the indexof method to check which image is the latest as there is no such info in PowerShell or azure cli output.
Issue script & Debug output
Environment data
Module versions
Error output