Skip to content

Commit

Permalink
Merge pull request #873 from FH-Inway/fix-get-d365azurestoragefile
Browse files Browse the repository at this point in the history
🐛 Fix Get-D365AzureStorageFile
  • Loading branch information
FH-Inway authored Dec 7, 2024
2 parents c5087cf + 4d64acf commit b0d5f2f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
16 changes: 8 additions & 8 deletions d365fo.tools/bin/d365fo.tools-index.json
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@
},
{
"CommandName": "Get-D365AzureStorageFile",
"Description": "Get all files from an Azure Storage Account",
"Description": "Get information for files from an Azure Storage Account",
"Tags": [
"Azure",
"Azure Storage",
Expand All @@ -1790,7 +1790,7 @@
"Params": [
[
"AccountId",
"Storage Account Name / Storage Account Id where you want to look for files",
"Storage Account Name / Storage Account Id where file information should be retrieved from",
"",
false,
"false",
Expand All @@ -1806,31 +1806,31 @@
],
[
"SAS",
"The SAS key that you have created for the storage account or blob container",
"The SAS key for the storage account or blob container",
"",
false,
"false",
"$Script:AzureStorageSAS"
],
[
"Container",
"Name of the blob container inside the storage account you want to look for files",
"Name of the blob container inside the storage account where file information should be retrieved from",
"Blobname,Blob",
false,
"false",
"$Script:AzureStorageContainer"
],
[
"Name",
"Name of the file you are looking for\nAccepts wildcards for searching. E.g. -Name \"Application*Adaptor\"\nDefault value is \"*\" which will search for all packages",
"Name of the files information should be retrieved for\nAccepts wildcards for searching. E.g. -Name \"Application*Adaptor\"\nDefault value is \"*\" which will search for all files",
"FileName",
false,
"false",
"*"
],
[
"Latest",
"Instruct the cmdlet to only fetch the latest file from the Azure Storage Account",
"Instruct the cmdlet to only fetch the information of the latest file from the Azure Storage Account",
"GetLatest",
true,
"false",
Expand All @@ -1839,10 +1839,10 @@
],
"Alias": "",
"Author": "Mötz Jensen (@Splaxi)",
"Synopsis": "Get a file from Azure",
"Synopsis": "Get file information from Azure Storage",
"Name": "Get-D365AzureStorageFile",
"Links": null,
"Examples": "-------------------------- EXAMPLE 1 --------------------------\nPS C:\\\u003eGet-D365AzureStorageFile -AccountId \"miscfiles\" -AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" -Container \"backupfiles\"\nThis will get all files in the blob container \"backupfiles\".\r\nIt will use the AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" to gain access.\n-------------------------- EXAMPLE 2 --------------------------\nPS C:\\\u003eGet-D365AzureStorageFile -AccountId \"miscfiles\" -AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" -Container \"backupfiles\" -Latest\nThis will get the latest (newest) file from the blob container \"backupfiles\".\r\nIt will use the AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" to gain access to the container.\n-------------------------- EXAMPLE 3 --------------------------\nPS C:\\\u003eGet-D365AzureStorageFile -AccountId \"miscfiles\" -AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" -Container \"backupfiles\" -Name \"*UAT*\"\nThis will get all files in the blob container \"backupfiles\" that fits the \"*UAT*\" search value.\r\nIt will use the AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" to gain access to the container.\n-------------------------- EXAMPLE 4 --------------------------\nPS C:\\\u003eGet-D365AzureStorageFile -AccountId \"miscfiles\" -SAS \"sv2018-03-28\u0026siunlisted\u0026src\u0026sigAUOpdsfpoWE976ASDhfjkasdf(5678sdfhk\" -Container \"backupfiles\" -Latest\nThis will get the latest (newest) file from the blob container \"backupfiles\".\r\nIt will use the SAS key \"sv2018-03-28\u0026siunlisted\u0026src\u0026sigAUOpdsfpoWE976ASDhfjkasdf(5678sdfhk\" to gain access to the container.",
"Examples": "-------------------------- EXAMPLE 1 --------------------------\nPS C:\\\u003eGet-D365AzureStorageFile -AccountId \"miscfiles\" -AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" -Container \"backupfiles\"\nThis will get information for all files in the blob container \"backupfiles\".\r\nIt will use the AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" to gain access.\n-------------------------- EXAMPLE 2 --------------------------\nPS C:\\\u003eGet-D365AzureStorageFile -AccountId \"miscfiles\" -AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" -Container \"backupfiles\" -Latest\nThis will get information for the latest (newest) file from the blob container \"backupfiles\".\r\nIt will use the AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" to gain access to the container.\n-------------------------- EXAMPLE 3 --------------------------\nPS C:\\\u003eGet-D365AzureStorageFile -AccountId \"miscfiles\" -AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" -Container \"backupfiles\" -Name \"*UAT*\"\nThis will get information for all files in the blob container \"backupfiles\" that fits the \"*UAT*\" search value.\r\nIt will use the AccessToken \"xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51\" to gain access to the container.\n-------------------------- EXAMPLE 4 --------------------------\nPS C:\\\u003eGet-D365AzureStorageFile -AccountId \"miscfiles\" -SAS \"sv2018-03-28\u0026siunlisted\u0026src\u0026sigAUOpdsfpoWE976ASDhfjkasdf(5678sdfhk\" -Container \"backupfiles\" -Latest\nThis will get information for the latest (newest) file from the blob container \"backupfiles\".\r\nIt will use the SAS key \"sv2018-03-28\u0026siunlisted\u0026src\u0026sigAUOpdsfpoWE976ASDhfjkasdf(5678sdfhk\" to gain access to the container.",
"Syntax": "Get-D365AzureStorageFile [-AccountId \u003cString\u003e] [-AccessToken \u003cString\u003e] [-SAS \u003cString\u003e] [-Container \u003cString\u003e] [-Name \u003cString\u003e] [\u003cCommonParameters\u003e]\nGet-D365AzureStorageFile [-AccountId \u003cString\u003e] [-AccessToken \u003cString\u003e] [-SAS \u003cString\u003e] [-Container \u003cString\u003e] -Latest [\u003cCommonParameters\u003e]"
},
{
Expand Down
52 changes: 30 additions & 22 deletions d365fo.tools/functions/get-d365azurestoragefile.ps1
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@

<#
.SYNOPSIS
Get a file from Azure
Get file information from Azure Storage
.DESCRIPTION
Get all files from an Azure Storage Account
Get information for files from an Azure Storage Account
.PARAMETER AccountId
Storage Account Name / Storage Account Id where you want to look for files
Storage Account Name / Storage Account Id where file information should be retrieved from
.PARAMETER AccessToken
The token that has the needed permissions for the search action
.PARAMETER SAS
The SAS key that you have created for the storage account or blob container
The SAS key for the storage account or blob container
.PARAMETER Container
Name of the blob container inside the storage account you want to look for files
Name of the blob container inside the storage account where file information should be retrieved from
.PARAMETER Name
Name of the file you are looking for
Name of the files information should be retrieved for
Accepts wildcards for searching. E.g. -Name "Application*Adaptor"
Default value is "*" which will search for all packages
Default value is "*" which will search for all files
.PARAMETER Latest
Instruct the cmdlet to only fetch the latest file from the Azure Storage Account
Instruct the cmdlet to only fetch the information of the latest file from the Azure Storage Account
.EXAMPLE
PS C:\> Get-D365AzureStorageFile -AccountId "miscfiles" -AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" -Container "backupfiles"
This will get all files in the blob container "backupfiles".
This will get information for all files in the blob container "backupfiles".
It will use the AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" to gain access.
.EXAMPLE
PS C:\> Get-D365AzureStorageFile -AccountId "miscfiles" -AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" -Container "backupfiles" -Latest
This will get the latest (newest) file from the blob container "backupfiles".
This will get information for the latest (newest) file from the blob container "backupfiles".
It will use the AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" to gain access to the container.
.EXAMPLE
PS C:\> Get-D365AzureStorageFile -AccountId "miscfiles" -AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" -Container "backupfiles" -Name "*UAT*"
This will get all files in the blob container "backupfiles" that fits the "*UAT*" search value.
This will get information for all files in the blob container "backupfiles" that fits the "*UAT*" search value.
It will use the AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" to gain access to the container.
.EXAMPLE
PS C:\> Get-D365AzureStorageFile -AccountId "miscfiles" -SAS "sv2018-03-28&siunlisted&src&sigAUOpdsfpoWE976ASDhfjkasdf(5678sdfhk" -Container "backupfiles" -Latest
This will get the latest (newest) file from the blob container "backupfiles".
This will get information for the latest (newest) file from the blob container "backupfiles".
It will use the SAS key "sv2018-03-28&siunlisted&src&sigAUOpdsfpoWE976ASDhfjkasdf(5678sdfhk" to gain access to the container.
.NOTES
Tags: Azure, Azure Storage, Token, Blob, File, Container
Author: Mötz Jensen (@Splaxi)
Author: Florian Hopfner (@FH-Inway)
#>
function Get-D365AzureStorageFile {
[CmdletBinding(DefaultParameterSetName = 'Default')]
Expand All @@ -79,9 +80,9 @@ function Get-D365AzureStorageFile {
[switch] $Latest
)

if (([string]::IsNullOrEmpty($AccountId) -eq $true) -or
([string]::IsNullOrEmpty($Container)) -or
(([string]::IsNullOrEmpty($AccessToken)) -and ([string]::IsNullOrEmpty($SAS)))) {
$connectionInformation = ($AccountId -and $Container)
$authenticationInformation = ($AccessToken -or $SAS)
if (-not ($connectionInformation -and $authenticationInformation)) {
Write-PSFMessage -Level Host -Message "It seems that you are missing some of the parameters. Please make sure that you either supplied them or have the right configuration saved."
Stop-PSFFunction -Message "Stopping because of missing parameters"
return
Expand All @@ -102,21 +103,28 @@ function Get-D365AzureStorageFile {
}

try {
$files = Get-AzStorageBlob -Container $($Container.ToLower()) -Context $storageContext | Sort-Object -Descending { $_.Properties.LastModified }
$files = Get-AzStorageBlob -Container $($Container.ToLower()) -Context $storageContext |
Sort-Object -Descending { $_.LastModified }

$selectParams = @{
TypeName = "D365FO.TOOLS.Azure.Blob"
Property = "Name", "Length as Size to PSFSize", "LastModified"
}
if ($Latest) {
$files | Select-Object -First 1 | Select-PSFObject -TypeName D365FO.TOOLS.Azure.Blob "name", @{Name = "Size"; Expression = { [PSFSize]$_.Length } }, @{Name = "LastModified"; Expression = { [Datetime]::Parse($_.LastModified) } }
$files |
Select-Object -First 1 |
Select-PSFObject @selectParams
}
else {

foreach ($obj in $files) {
if ($obj.Name -NotLike $Name) { continue }

$obj | Select-PSFObject -TypeName D365FO.TOOLS.Azure.Blob "name", @{Name = "Size"; Expression = { [PSFSize]$_.Length } }, @{Name = "LastModified"; Expression = { [Datetime]::Parse($_.LastModified) } }
$filteredFiles = $files | Where-Object { $_.Name -Like $Name }
foreach ($fileInfo in $filteredFiles) {
$fileInfo | Select-PSFObject @selectParams
}
}
}
catch {
Write-PSFMessage -Level Warning -Message "Something broke" -ErrorRecord $_
}

Invoke-TimeSignal -End
}
25 changes: 13 additions & 12 deletions docs/Get-D365AzureStorageFile.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ schema: 2.0.0
# Get-D365AzureStorageFile

## SYNOPSIS
Get a file from Azure
Get file information from Azure Storage

## SYNTAX

Expand All @@ -25,7 +25,7 @@ Get-D365AzureStorageFile [-AccountId <String>] [-AccessToken <String>] [-SAS <St
```

## DESCRIPTION
Get all files from an Azure Storage Account
Get information for files from an Azure Storage Account

## EXAMPLES

Expand All @@ -34,37 +34,37 @@ Get all files from an Azure Storage Account
Get-D365AzureStorageFile -AccountId "miscfiles" -AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" -Container "backupfiles"
```

This will get all files in the blob container "backupfiles".
This will get information for all files in the blob container "backupfiles".
It will use the AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" to gain access.

### EXAMPLE 2
```
Get-D365AzureStorageFile -AccountId "miscfiles" -AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" -Container "backupfiles" -Latest
```

This will get the latest (newest) file from the blob container "backupfiles".
This will get information for the latest (newest) file from the blob container "backupfiles".
It will use the AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" to gain access to the container.

### EXAMPLE 3
```
Get-D365AzureStorageFile -AccountId "miscfiles" -AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" -Container "backupfiles" -Name "*UAT*"
```

This will get all files in the blob container "backupfiles" that fits the "*UAT*" search value.
This will get information for all files in the blob container "backupfiles" that fits the "*UAT*" search value.
It will use the AccessToken "xx508xx63817x752xx74004x30705xx92x58349x5x78f5xx34xxxxx51" to gain access to the container.

### EXAMPLE 4
```
Get-D365AzureStorageFile -AccountId "miscfiles" -SAS "sv2018-03-28&siunlisted&src&sigAUOpdsfpoWE976ASDhfjkasdf(5678sdfhk" -Container "backupfiles" -Latest
```

This will get the latest (newest) file from the blob container "backupfiles".
This will get information for the latest (newest) file from the blob container "backupfiles".
It will use the SAS key "sv2018-03-28&siunlisted&src&sigAUOpdsfpoWE976ASDhfjkasdf(5678sdfhk" to gain access to the container.

## PARAMETERS

### -AccountId
Storage Account Name / Storage Account Id where you want to look for files
Storage Account Name / Storage Account Id where file information should be retrieved from

```yaml
Type: String
Expand Down Expand Up @@ -94,7 +94,7 @@ Accept wildcard characters: False
```
### -SAS
The SAS key that you have created for the storage account or blob container
The SAS key for the storage account or blob container
```yaml
Type: String
Expand All @@ -109,7 +109,7 @@ Accept wildcard characters: False
```
### -Container
Name of the blob container inside the storage account you want to look for files
Name of the blob container inside the storage account where file information should be retrieved from
```yaml
Type: String
Expand All @@ -124,13 +124,13 @@ Accept wildcard characters: False
```
### -Name
Name of the file you are looking for
Name of the files information should be retrieved for
Accepts wildcards for searching.
E.g.
-Name "Application*Adaptor"
Default value is "*" which will search for all packages
Default value is "*" which will search for all files
```yaml
Type: String
Expand All @@ -145,7 +145,7 @@ Accept wildcard characters: False
```
### -Latest
Instruct the cmdlet to only fetch the latest file from the Azure Storage Account
Instruct the cmdlet to only fetch the information of the latest file from the Azure Storage Account
```yaml
Type: SwitchParameter
Expand All @@ -170,5 +170,6 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable
Tags: Azure, Azure Storage, Token, Blob, File, Container
Author: Mötz Jensen (@Splaxi)
Author: Florian Hopfner (@FH-Inway)
## RELATED LINKS

0 comments on commit b0d5f2f

Please sign in to comment.