|
| 1 | +function Get-CIPPAzStorageContainer { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Lists Azure Storage blob containers using Shared Key auth. |
| 5 | + .DESCRIPTION |
| 6 | + Uses New-CIPPAzStorageRequest to call the Blob service list API. |
| 7 | + - Uses server-side 'prefix' when Name ends with a single trailing '*'. |
| 8 | + - Builds container URIs from the connection string (standard, provided endpoint, emulator). |
| 9 | + - Passes through container Properties returned by the service. |
| 10 | + .PARAMETER Name |
| 11 | + Container name filter. Supports wildcards (e.g., 'cipp*'). Defaults to '*'. |
| 12 | + When the pattern ends with a single trailing '*' and contains no other wildcards, |
| 13 | + a server-side 'prefix' is used for listing; otherwise client-side filtering is applied. |
| 14 | + .PARAMETER ConnectionString |
| 15 | + Azure Storage connection string. Defaults to $env:AzureWebJobsStorage |
| 16 | + .EXAMPLE |
| 17 | + Get-CIPPAzStorageContainer -Name 'cipp*' |
| 18 | + #> |
| 19 | + [CmdletBinding()] |
| 20 | + param( |
| 21 | + [Parameter(Mandatory = $false, Position = 0)] |
| 22 | + [string]$Name = '*', |
| 23 | + |
| 24 | + [Parameter(Mandatory = $false)] |
| 25 | + [string]$ConnectionString = $env:AzureWebJobsStorage |
| 26 | + ) |
| 27 | + |
| 28 | + begin { |
| 29 | + function Parse-ConnString { |
| 30 | + param([string]$Conn) |
| 31 | + $map = @{} |
| 32 | + if (-not $Conn) { return $map } |
| 33 | + foreach ($part in ($Conn -split ';')) { |
| 34 | + $p = $part.Trim() |
| 35 | + if ($p -and $p -match '^(.+?)=(.+)$') { $map[$matches[1]] = $matches[2] } |
| 36 | + } |
| 37 | + $map |
| 38 | + } |
| 39 | + |
| 40 | + function Get-BlobBaseInfo { |
| 41 | + param([hashtable]$ConnParams) |
| 42 | + $service = 'blob' |
| 43 | + $svcCap = [char]::ToUpper($service[0]) + $service.Substring(1) |
| 44 | + $endpointKey = "${svcCap}Endpoint" |
| 45 | + $provided = $ConnParams[$endpointKey] |
| 46 | + $useDev = ($ConnParams['UseDevelopmentStorage'] -eq 'true') |
| 47 | + $account = $ConnParams['AccountName'] |
| 48 | + |
| 49 | + if ($provided) { |
| 50 | + $u = [System.Uri]::new($provided) |
| 51 | + return [PSCustomObject]@{ |
| 52 | + Scheme = $u.Scheme |
| 53 | + Host = $u.Host |
| 54 | + Port = $u.Port |
| 55 | + Path = $u.AbsolutePath.TrimEnd('/') |
| 56 | + Mode = 'ProvidedEndpoint' |
| 57 | + Account = $account |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if ($useDev) { |
| 62 | + return [PSCustomObject]@{ |
| 63 | + Scheme = 'http' |
| 64 | + Host = '127.0.0.1' |
| 65 | + Port = 10000 |
| 66 | + Path = $null |
| 67 | + Mode = 'Emulator' |
| 68 | + Account = ($account ?? 'devstoreaccount1') |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + $suffix = $ConnParams['EndpointSuffix'] |
| 73 | + if (-not $suffix) { $suffix = 'core.windows.net' } |
| 74 | + $scheme = $ConnParams['DefaultEndpointsProtocol'] |
| 75 | + if (-not $scheme) { $scheme = 'https' } |
| 76 | + return [PSCustomObject]@{ |
| 77 | + Scheme = $scheme |
| 78 | + Host = "$account.blob.$suffix" |
| 79 | + Port = -1 |
| 80 | + Path = $null |
| 81 | + Mode = 'Standard' |
| 82 | + Account = $account |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + function Build-ContainerUri { |
| 87 | + param([pscustomobject]$BaseInfo, [string]$ContainerName) |
| 88 | + $ub = [System.UriBuilder]::new() |
| 89 | + $ub.Scheme = $BaseInfo.Scheme |
| 90 | + $ub.Host = $BaseInfo.Host |
| 91 | + if ($BaseInfo.Port -and $BaseInfo.Port -ne -1) { $ub.Port = [int]$BaseInfo.Port } |
| 92 | + switch ($BaseInfo.Mode) { |
| 93 | + 'ProvidedEndpoint' { |
| 94 | + $prefixPath = $BaseInfo.Path |
| 95 | + if ([string]::IsNullOrEmpty($prefixPath)) { $ub.Path = "/$ContainerName" } |
| 96 | + else { $ub.Path = ("$prefixPath/$ContainerName").Replace('//', '/') } |
| 97 | + } |
| 98 | + 'Emulator' { $ub.Path = "$($BaseInfo.Account)/$ContainerName" } |
| 99 | + default { $ub.Path = "/$ContainerName" } |
| 100 | + } |
| 101 | + $ub.Uri.AbsoluteUri |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + process { |
| 106 | + $connParams = Parse-ConnString -Conn $ConnectionString |
| 107 | + $baseInfo = Get-BlobBaseInfo -ConnParams $connParams |
| 108 | + |
| 109 | + # Determine server-side prefix optimization |
| 110 | + $serverPrefix = $null |
| 111 | + $pattern = $Name |
| 112 | + if ([string]::IsNullOrEmpty($pattern)) { $pattern = '*' } |
| 113 | + $canUsePrefix = $false |
| 114 | + if ($pattern.EndsWith('*') -and $pattern.IndexOfAny([char[]]@('*', '?')) -eq ($pattern.Length - 1)) { |
| 115 | + $serverPrefix = $pattern.Substring(0, $pattern.Length - 1) |
| 116 | + $canUsePrefix = $true |
| 117 | + } |
| 118 | + |
| 119 | + $listParams = @{ Service = 'blob'; Component = 'list'; ConnectionString = $ConnectionString } |
| 120 | + if ($canUsePrefix -and $serverPrefix) { $listParams['QueryParams'] = @{ prefix = $serverPrefix } } |
| 121 | + |
| 122 | + $containers = New-CIPPAzStorageRequest @listParams |
| 123 | + if (-not $containers) { return @() } |
| 124 | + |
| 125 | + # Normalize to array of {Name, Properties} |
| 126 | + $items = @() |
| 127 | + foreach ($c in $containers) { |
| 128 | + if ($null -ne $c -and $c.PSObject.Properties['Name']) { |
| 129 | + $items += [PSCustomObject]@{ Name = $c.Name; Properties = $c.Properties } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + # Client-side wildcard filtering when needed |
| 134 | + if (-not $canUsePrefix) { |
| 135 | + $items = $items | Where-Object { $_.Name -like $pattern } |
| 136 | + } |
| 137 | + |
| 138 | + $results = @() |
| 139 | + foreach ($it in $items) { |
| 140 | + $uri = Build-ContainerUri -BaseInfo $baseInfo -ContainerName $it.Name |
| 141 | + $results += [PSCustomObject]@{ |
| 142 | + Name = $it.Name |
| 143 | + Uri = $uri |
| 144 | + Properties = $it.Properties |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + # Optional banner for UX parity when displayed directly |
| 149 | + if ($results.Count -gt 0 -and $baseInfo.Account) { |
| 150 | + Write-Host "\n Storage Account Name: $($baseInfo.Account)\n" -ForegroundColor DarkGray |
| 151 | + } |
| 152 | + |
| 153 | + $results |
| 154 | + } |
| 155 | +} |
0 commit comments