From 855e7e71f8c9a89c092e5a5ffbbe7dd4978ee824 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 19 Apr 2020 17:30:41 +0100 Subject: [PATCH] Add Get-ByteContent common function --- .../DSCResources/MSFT_ADUser/MSFT_ADUser.psm1 | 28 ++++++------------ .../ActiveDirectoryDsc.Common.psd1 | 1 + .../ActiveDirectoryDsc.Common.psm1 | 29 +++++++++++++++++++ 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/source/DSCResources/MSFT_ADUser/MSFT_ADUser.psm1 b/source/DSCResources/MSFT_ADUser/MSFT_ADUser.psm1 index b26ad5f90..8b04aa2b1 100644 --- a/source/DSCResources/MSFT_ADUser/MSFT_ADUser.psm1 +++ b/source/DSCResources/MSFT_ADUser/MSFT_ADUser.psm1 @@ -41,8 +41,8 @@ $adPropertyMap = (Import-PowerShellDataFile -Path $adPropertyMapPath).Parameters ------------------------------|-------------------------- Get-ADUser | ActiveDirectory Assert-Module | DscResource.Common + New-InvalidOperationException | DscResource.Common Get-ADCommonParameters | ActiveDirectoryDsc.Common - New-InvalidOperationException | ActiveDirectoryDsc.Common Get-ADObjectParentDN | ActiveDirectoryDsc.Common Get-MD5HashString | MSFT_ADUser #> @@ -1833,6 +1833,13 @@ function Get-MD5HashString .OUTPUTS Returns a byte array of the image specified in the parameter ThumbnailPhoto. + + .NOTES + Used Functions: + Name | Module + ------------------------------|-------------------------- + Get-PSEdition | ActiveDirectoryDsc.Common + New-InvalidOperationException | DscResource.Common #> function Get-ThumbnailByteArray { @@ -1851,24 +1858,7 @@ function Get-ThumbnailByteArray if (Test-Path -Path $ThumbnailPhoto) { Write-Verbose -Message ($script:localizedData.LoadingThumbnailFromFile -f $ThumbnailPhoto) - $getContentParms = @{ - Path = $ThumbnailPhoto - } - - # Use Get-Content Encoding Byte for PowerShell Desktop and AsByteStream for PowerShell Core - if ($PSVersionTable.PSEdition -eq 'Desktop') - { - $getContentParms += @{ - Encoding = 'Byte' - } - } - else - { - $getContentParms += @{ - AsByteStream = $true - } - } - $thumbnailPhotoAsByteArray = Get-Content @getContentParms + $thumbnailPhotoAsByteArray = Get-ByteContent -Path $ThumbnailPhoto } else { diff --git a/source/Modules/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.psd1 b/source/Modules/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.psd1 index 69d233c8a..5d4d89ab1 100644 --- a/source/Modules/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.psd1 +++ b/source/Modules/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.psd1 @@ -53,6 +53,7 @@ 'Get-CurrentUser' 'Test-Password' 'Test-PrincipalContextCredentials' + 'Get-ByteContent' ) # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. diff --git a/source/Modules/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.psm1 b/source/Modules/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.psm1 index f717d5b4a..403d3bc44 100644 --- a/source/Modules/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.psm1 +++ b/source/Modules/ActiveDirectoryDsc.Common/ActiveDirectoryDsc.Common.psm1 @@ -2063,3 +2063,32 @@ function Test-PrincipalContextCredentials return $result } + +<# + .SYNOPSIS + Returns the contents of a file as a byte array + + .PARAMETER Path + Specifies the path to an item. +#> +function Get-ByteContent +{ + [CmdletBinding()] + [OutputType([System.Byte[]])] + param( + [Parameter(Mandatory = $true)] + [System.String] + $Path + ) + + if ($PSVersionTable.PSEdition -eq 'Core') + { + $content = Get-Content -Path $Path -AsByteStream + } + else + { + $content = Get-Content -Path $Path -Encoding 'Byte' + } + + return $content +}