Skip to content

Commit

Permalink
Add -AsBase64 to Get-HaloAttachment. 1.17.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
homotechsual committed Dec 19, 2023
1 parent b709978 commit 5abece4
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
Empty file.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2023-12-19 - Version 1.17.0

* Add `-AsBase64` parameter to `Get-HaloAttachment` to allow fetching single attachments as base64 encoded strings.

## 2023-11-17 - Version 1.16.0

* Refactor variables in `Invoke-HaloRequest` to avoid scope confusion.
Expand Down
8 changes: 5 additions & 3 deletions HaloAPI.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,11 @@ if ($UpdateManifest) {
Import-Module -Name PlatyPS

# Find Latest Version in Change log.
$CHANGELOG = Get-Content -Path "$($PSScriptRoot)\CHANGELOG.md"
$CHANGELOG = Get-Content -Path "$($PSScriptRoot)\CHANGELOG.md" -Raw
$MarkdownObject = [Markdown.MAML.Parser.MarkdownParser]::new()
[regex]$Regex = '\d\.\d\.\d'
[regex]$ReleaseRegex = '#{2}.*\d*\.\d*\.\d*$/m'
$Releases = ($ReleaseRegex.Matches($MarkdownObject.ParseString($CHANGELOG).Children.Spans.Text))
[regex]$VersionRegex = '\d*\.\d*\.\d*'
$Versions = $Regex.Matches($MarkdownObject.ParseString($CHANGELOG).Children.Spans.Text) | ForEach-Object { $_.Value }
$ChangeLogVersion = ($Versions | Measure-Object -Maximum).Maximum

Expand All @@ -236,7 +238,7 @@ if ($UpdateManifest) {
# Update Manifest file with Release Notes
$CHANGELOG = Get-Content -Path "$($PSScriptRoot)\CHANGELOG.md"
$MarkdownObject = [Markdown.MAML.Parser.MarkdownParser]::new()
$ReleaseNotes = ((($MarkdownObject.ParseString($CHANGELOG).Children.Spans.Text) -Match '\d\.\d\.\d') -Split ' - ')[1]
$ReleaseNotes = ((($MarkdownObject.ParseString($CHANGELOG).Children.Spans.Text) -Match '#{2}.*\d*\.\d*\.\d') -Split ' - ')[1]

# Update Module with new version
Update-ModuleManifest -ModuleVersion $ChangeLogVersion -Path "$($PSScriptRoot)\$ModuleName.psd1" -ReleaseNotes $ReleaseNotes
Expand Down
2 changes: 1 addition & 1 deletion HaloAPI.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"powershell.codeFormatting.whitespaceBetweenParameters": false,
"powershell.codeFormatting.whitespaceInsideBrace": true,
"editor.codeActionsOnSave": {
"source.fixAll.markdownlint": true
"source.fixAll.markdownlint": "explicit"
}
}
}
4 changes: 2 additions & 2 deletions HaloAPI.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = '.\HaloAPI.psm1'

# Version number of this module.
ModuleVersion = '1.16.0'
ModuleVersion = '1.17.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -268,7 +268,7 @@
IconUri = 'https://3c3br937rz386088k2z3qqdi-wpengine.netdna-ssl.com/wp-content/uploads/2020/04/HaloIcon-300x300.png'

# ReleaseNotes of this module
ReleaseNotes = 'Refactor `Invoke-HaloRequest` to avoid reusing variable names and keep the scope clearer. Fixes breakage with PowerShell 7.4.0.'
ReleaseNotes = 'Add -AsBase64 to Get-HaloAttachment to return the attachment as a base64 encoded string.'

# Prerelease string of this module
# Prerelease = 'Beta1'
Expand Down
10 changes: 8 additions & 2 deletions Public/Get/Get-HaloAttachment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ function Get-HaloAttachment {
[String]$OutFile,
# Allow Writing Directly to File, using the specified path and the original file name eg c:\temp\
[Parameter( ParameterSetName = 'SinglePath', Mandatory = $True )]
[String]$OutPath
[String]$OutPath,
# Return the attachment as a base64 encoded string
[Parameter( ParameterSetName = 'Single' )]
[Switch]$AsBase64
)
Invoke-HaloPreFlightCheck
$CommandName = $MyInvocation.MyCommand.Name
Expand Down Expand Up @@ -88,11 +91,14 @@ function Get-HaloAttachment {
Write-Verbose "Attempting to output to file $OutFile"
$Path = $OutFile
}

Write-Verbose "Writing File $Path"
$File = [System.IO.FileStream]::new($Path, [System.IO.FileMode]::Create)
$File.write($AttachmentResults.Content, 0, $AttachmentResults.RawContentLength)
$File.close()
} elseif ($AsBase64) {
Write-Verbose 'Returning base64 encoded string'
$Base64String = [System.Convert]::ToBase64String($AttachmentResults.Content)
Return $Base64String
} else {
return $AttachmentResults.Content
}
Expand Down
18 changes: 11 additions & 7 deletions Public/Invoke-HaloRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ function Invoke-HaloRequest {
try {
Write-Verbose "Making a $($WebRequestParams.Method) request to $($WebRequestParams.Uri)"
$Response = Invoke-WebRequest @WebRequestParams -Headers $RequestHeaders -ContentType 'application/json; charset=utf-8'
Write-Debug "Response headers: $($Response.Headers | Out-String)"
Write-Debug "Raw Response: $($Response | Out-String)"
Write-Debug "Response Members: $($Response | Get-Member | Out-String)"
$Success = $True
if ($RawResult) {
$Results = $Response
if ($Response) {
Write-Debug "Response headers: $($Response.Headers | Out-String)"
Write-Debug "Raw Response: $($Response | Out-String)"
Write-Debug "Response Members: $($Response | Get-Member | Out-String)"
$Success = $True
if ($RawResult) {
$Results = $Response
} else {
$Results = ($Response.Content | ConvertFrom-Json -Depth 100)
}
} else {
$Results = ($Response.Content | ConvertFrom-Json -Depth 100)
Write-Debug 'Response was null.'
}
} catch [Microsoft.PowerShell.Commands.HttpResponseException] {
$Success = $False
Expand Down

0 comments on commit 5abece4

Please sign in to comment.