Last active
August 18, 2025 22:16
-
-
Save Calvindd2f/5fd7350e3468c3ad3e147d5cdca826af to your computer and use it in GitHub Desktop.
Get-ASCIIBytes,Get-Base64ScriptContent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-Base64ScriptContent | |
| { | |
| param($encodeContent, [switch]$RemoveSignature) | |
| if(-not $encodeContent) { return } | |
| try | |
| { | |
| $scriptContent = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($encodeContent)) | |
| if($RemoveSignature -eq $true) | |
| { | |
| $x = $scriptContent.IndexOf("# SIG # Begin signature block") | |
| if($x -gt 0) | |
| { | |
| $scriptContent = $scriptContent.SubString(0,$x) | |
| $scriptContent = $scriptContent + "# SIG # Begin signature block`nSignature data excluded..." | |
| } | |
| } | |
| $scriptContent | |
| } | |
| catch | |
| { | |
| } | |
| } | |
| function Get-ProxyURI | |
| { | |
| if($null -eq $script:proxyURI) | |
| { | |
| $script:proxyUri = Get-SettingValue "ProxyURI" | |
| } | |
| if($null -eq $script:proxyURI) | |
| { | |
| $script:proxyUri = "" | |
| } | |
| return $script:proxyURI | |
| } | |
| function Get-ASCIIBytes | |
| { | |
| param($String) | |
| $bytes = [System.Text.Encoding]::ASCII.GetBytes($String) | |
| if ($bytes[0] -eq 0x2b -and $bytes[1] -eq 0x2f -and $bytes[2] -eq 0x76) | |
| { [Text.Encoding]::UTF7.GetBytes($String) } | |
| elseif ($bytes[0] -eq 0xff -and $bytes[1] -eq 0xfe) | |
| { [Text.Encoding]::Unicode.GetBytes($String) } | |
| elseif ($bytes[0] -eq 0xfe -and $bytes[1] -eq 0xff) | |
| { [Text.Encoding]::BigEndianUnicode.GetBytes($String) } | |
| elseif ($bytes[0] -eq 0x00 -and $bytes[1] -eq 0x00 -and $bytes[2] -eq 0xfe -and $bytes[3] -eq 0xff) | |
| { [Text.Encoding]::UTF32.GetBytes($String) } | |
| elseif ($bytes[0] -eq 0xef -and $bytes[1] -eq 0xbb -and $bytes[2] -eq 0xbf) | |
| { [Text.Encoding]::UTF8.GetBytes($String) } | |
| $bytes | |
| } | |
| function Format-XML | |
| { | |
| param([xml]$xml, $indent = 2) | |
| if(-not $xml) { return } | |
| #From: https://devblogs.microsoft.com/powershell/format-xml/ | |
| $StringWriter = New-Object System.IO.StringWriter | |
| $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter | |
| $xmlWriter.Formatting = "indented" | |
| $xmlWriter.Indentation = $Indent | |
| $xml.WriteContentTo($XmlWriter) | |
| $XmlWriter.Flush() | |
| $StringWriter.Flush() | |
| $StringWriter.ToString() | |
| } | |
| function Get-IsAdmin | |
| { | |
| (New-Object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) | |
| } | |
| function Get-JWTtoken | |
| { | |
| param($token) | |
| if(-not $token) { return } | |
| if(-not $token.StartsWith("eyJ")) | |
| { | |
| Write-Log "Invalid JWT token" 3; return | |
| } | |
| # First part is the header. Second part is the payload. Third part is the signature | |
| $arr = $token.Split(".") | |
| if($arr.Count -lt 2) { Write-Log "Invalid token" 3; return } | |
| $header = $arr[0].Replace('-', '+').Replace('_', '/') # change base64url to base64 | |
| while ($header.Length % 4) { $header += "=" } # Add padding to match required length | |
| $payload = $arr[1].Replace('-', '+').Replace('_', '/') # change base64url to base64 | |
| while ($payload.Length % 4) { $payload += "=" } # Add padding to match required length | |
| return (New-Object PSObject -Property @{ | |
| Header=(([System.Text.Encoding]::UTF8.GetString(([System.Convert]::FromBase64String($header)))) | ConvertFrom-Json) | |
| Payload=(([System.Text.Encoding]::UTF8.GetString(([System.Convert]::FromBase64String($payload)))) | ConvertFrom-Json) | |
| }) | |
| } | |
| function Expand-FileName | |
| { | |
| param($fileName) | |
| [Environment]::SetEnvironmentVariable("Date",(Get-Date).ToString("yyyy-MM-dd"),[System.EnvironmentVariableTarget]::Process) | |
| [Environment]::SetEnvironmentVariable("DateTime",(Get-Date).ToString("yyyyMMdd-HHmm"),[System.EnvironmentVariableTarget]::Process) | |
| [Environment]::SetEnvironmentVariable("Organization",$global:Organization.displayName,[System.EnvironmentVariableTarget]::Process) | |
| $fileName = [Environment]::ExpandEnvironmentVariables($fileName) | |
| foreach($tmpFolder in ([System.Enum]::GetNames([System.Environment+SpecialFolder]))) | |
| { | |
| $fileName = $fileName -replace "%$($tmpFolder)%",([Environment]::GetFolderPath($tmpFolder)) | |
| } | |
| [Environment]::SetEnvironmentVariable("Date",$null,[System.EnvironmentVariableTarget]::Process) | |
| [Environment]::SetEnvironmentVariable("DateTime",$null,[System.EnvironmentVariableTarget]::Process) | |
| [Environment]::SetEnvironmentVariable("Organization",$null,[System.EnvironmentVariableTarget]::Process) | |
| # Remove invalid path characters | |
| $re = "[{0}]" -f [RegEx]::Escape(([IO.Path]::GetInvalidPathChars() -join '')) | |
| $fileName = $fileName -replace $re | |
| $fileName | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment