Last active
June 30, 2026 13:32
-
-
Save ElianFabian/4a74113197417e2e068d7e010e2cc475 to your computer and use it in GitHub Desktop.
PowerShell Utils
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 ConvertTo-DataObject { | |
| param ( | |
| [Parameter(Mandatory, ValueFromPipeline)] | |
| [Object] $InputObject | |
| ) | |
| if ($InputObject -is [Hashtable]) { | |
| $InputObject = [PSCustomObject]$InputObject | |
| } | |
| if ($InputObject -isnot [PSCustomObject]) { | |
| throw "The InputObject must be a Hashtable or a PSCustomObject" | |
| } | |
| $copyInputObject = [PSCustomObject] @{} | |
| foreach ($prop in $InputObject.PSObject.Properties) { | |
| $copyInputObject | Add-Member -MemberType NoteProperty -Name $prop.Name -Value $prop.Value | |
| } | |
| $copyInputObject | Add-Member -MemberType ScriptMethod -Name ToString -Value { | |
| $props = $this.PSObject.Properties | ForEach-Object { "$($_.Name)=$($_.Value)" } | |
| return "{ " + ($props -join ", ") + " }" | |
| } -Force | |
| $copyInputObject | Add-Member -MemberType ScriptMethod -Name Equals -Value { | |
| param ([Object] $other) | |
| if ($null -eq $other -or $other.GetType() -ne $this.GetType()) { return $false } | |
| $thisProps = $this.PSObject.Properties.Name | |
| $otherProps = $other.PSObject.Properties.Name | |
| if ($thisProps.Count -ne $otherProps.Count) { return $false } | |
| foreach ($prop in $thisProps) { | |
| $valueA = $this.$prop | |
| $valueB = $other.$prop | |
| if ($null -eq $valueA -and $null -ne $valueB) { return $false } | |
| if ($null -ne $valueA -and $null -eq $valueB) { return $false } | |
| if ($null -eq $valueA -and $null -eq $valueB) { continue } | |
| if ($valueA -is [array]) { | |
| if ($valueA.Count -ne $valueB.Count) { return $false } | |
| for ($i = 0; $i -lt $valueA.Count; $i++) { | |
| $itemA = $valueA[$i] | |
| $itemB = $valueB[$i] | |
| if (($null -eq $itemA) -ne ($null -eq $itemB)) { return $false } | |
| if ($null -eq $itemA -and $null -eq $itemB) { continue } | |
| if (-not $itemA.Equals($itemB)) { return $false } | |
| } | |
| } | |
| elseif ($valueA -is [double] -or $valueA -is [decimal]) { | |
| if (-not ($valueA -eq $valueB)) { return $false } | |
| } | |
| elseif (-not $valueA.Equals($valueB)) { | |
| return $false | |
| } | |
| } | |
| return $true | |
| } -Force | |
| $copyInputObject | Add-Member -MemberType ScriptMethod -Name Copy -Value { | |
| param ([hashtable] $Overrides = @{}) | |
| $copy = $this.PSObject.Copy() | |
| if ($Overrides) { | |
| foreach ($key in $Overrides.Keys) { | |
| $copy | Add-Member -MemberType NoteProperty -Name $key -Value $Overrides.$key -Force | |
| } | |
| } | |
| return $copy | |
| } -Force | |
| $copyInputObject | Add-Member -MemberType ScriptMethod -Name GetHashCode -Value { | |
| $hash = 17 | |
| foreach ($prop in $this.PSObject.Properties.Name) { | |
| $value = $this.$prop | |
| $valueHash = 0 | |
| if ($null -eq $value) { | |
| $valueHash = 0 | |
| } | |
| elseif ($value -is [array]) { | |
| foreach ($item in $value) { | |
| $itemHash = if ($item) { $item.GetHashCode() } else { 0 } | |
| $valueHash = ($valueHash * 31) + $itemHash | |
| } | |
| } | |
| else { | |
| $valueHash = $value.GetHashCode() | |
| } | |
| $hash = ($hash * 31) + $valueHash | |
| } | |
| return $hash | |
| } -Force | |
| return $copyInputObject | |
| } |
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 ConvertTo-Unicode { | |
| [OutputType([string])] | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory)] | |
| [string] $InputObject | |
| ) | |
| return ($InputObject.GetEnumerator() | ForEach-Object { "\u$(([int] $_).ToString('x4'))" }) -join '' | |
| } |
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
| # From: https://www.powershellgallery.com/packages/JumpCloud/2.2.0/Content/Private%5CNestedFunctions%5CHide-ObjectProperty.ps1 | |
| <# | |
| .DESCRIPTION | |
| This function will digest any object and set configure wether or not its properties will show by default when you call it. | |
| .EXAMPLE | |
| In this example you will need to replace the "$HiddenProperties" array and object name "$SomeObject". | |
| $ObjectOutput += [PSCustomObject]@{ | |
| 'Var1' = 'Hello; | |
| 'Var2' = 'World'; | |
| 'Var3' = '!'; | |
| } | |
| # List values to add to results | |
| $HiddenProperties = @('Var2','Var3') | |
| # Append meta info to results | |
| Get-Variable -Name:($HiddenProperties) | | |
| ForEach-Object { | |
| $Variable = $_ | |
| $SomeObject | | |
| ForEach-Object { | |
| Add-Member -InputObject:($_) -MemberType:('NoteProperty') -Name:($Variable.Name) -Value:($Variable.Value) | |
| } | |
| } | |
| # Set the meta info to be hidden by default | |
| $Results += Hide-ObjectProperty -Object:($Result) -HiddenProperties:($HiddenProperties) | |
| #> | |
| Function Hide-ObjectProperty { | |
| [CmdletBinding()] | |
| Param( | |
| [Parameter(Mandatory = $true, Position = 0)][ValidateNotNullOrEmpty()][Object]$Object, | |
| [Parameter(Mandatory = $true, Position = 1)][ValidateNotNullOrEmpty()][Array]$HiddenProperties | |
| ) | |
| # Set some properties to be hidden in the results | |
| $Object | ForEach-Object { | |
| # Get current object's properties | |
| $ObjectAllProperties = $_.PSObject.Properties.Name | |
| # Write-Host ('ObjectAllProperties:' + ($ObjectAllProperties -join ', ')) -BackgroundColor Gray -ForegroundColor Black | |
| # If current object has PSStandardMembers then get it's default set and add its hidden properties to PropertiesToHide | |
| If ($_.PSStandardMembers) { | |
| $ObjectShowProperties = $_.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames | |
| $PropertiesToHide = @($HiddenProperties + ($ObjectAllProperties | Where-Object { $_ -notin $ObjectShowProperties -or $_ -in $HiddenProperties }) | Select-Object -Unique) | |
| # Write-Host ('ObjectShowProperties:' + ($ObjectShowProperties -join ', ')) -BackgroundColor Yellow -ForegroundColor Black | |
| } Else { | |
| $PropertiesToHide = @($HiddenProperties) | |
| } | |
| # Get list of properties to show | |
| $PropertiesToShow = $ObjectAllProperties | Where-Object { $_ -notin $PropertiesToHide } | |
| # Write-Host ('PropertiesToHide:' + ($PropertiesToHide -join ', ')) -BackgroundColor Yellow -ForegroundColor Black | |
| # Write-Host ('PropertiesToShow:' + ($PropertiesToShow -join ', ')) -BackgroundColor Green -ForegroundColor Black | |
| # Create the default property display set | |
| If ($PropertiesToShow) { | |
| $defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet', [System.String[]]$PropertiesToShow) | |
| $PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet) | |
| # Add the list of standard members | |
| Add-Member -InputObject:($_) -MemberType:('MemberSet') -Name:('PSStandardMembers') -Value:($PSStandardMembers) -Force | |
| } Else { | |
| Throw ('By hiding "' + ($PropertiesToHide -join '", "') + '" there are no properties to show. At least one property must be visitable.') | |
| } | |
| } | |
| If ($Object) { | |
| Return $Object | |
| } | |
| } |
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
| # From: https://github.com/cfalta/PoshRandom/blob/master/ProcessSuspendResume.ps1 | |
| Add-Type -TypeDefinition @" | |
| using System; | |
| using System.Diagnostics; | |
| using System.Runtime.InteropServices; | |
| public static class ntdll | |
| { | |
| [DllImport("ntdll.dll", PreserveSig = false)] | |
| public static extern void NtSuspendProcess(IntPtr processHandle); | |
| [DllImport("ntdll.dll", PreserveSig = false, SetLastError = true)] | |
| public static extern void NtResumeProcess(IntPtr processHandle); | |
| } | |
| public static class kernel32 | |
| { | |
| [DllImport("kernel32.dll", SetLastError = true)] | |
| public static extern IntPtr OpenProcess(ProcessAccessFlags processAccess,bool bInheritHandle,int processId); | |
| [DllImport("kernel32.dll", SetLastError=true)] | |
| public static extern bool CloseHandle(IntPtr hObject); | |
| } | |
| [Flags] | |
| public enum ProcessAccessFlags : uint | |
| { | |
| All = 0x001F0FFF, | |
| Terminate = 0x00000001, | |
| CreateThread = 0x00000002, | |
| VirtualMemoryOperation = 0x00000008, | |
| VirtualMemoryRead = 0x00000010, | |
| VirtualMemoryWrite = 0x00000020, | |
| DuplicateHandle = 0x00000040, | |
| CreateProcess = 0x000000080, | |
| SetQuota = 0x00000100, | |
| SetInformation = 0x00000200, | |
| QueryInformation = 0x00000400, | |
| QueryLimitedInformation = 0x00001000, | |
| Synchronize = 0x00100000 | |
| } | |
| "@ | |
| function Suspend-Process { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory=$true,ValueFromPipeline = $True)] | |
| [ValidateNotNullorEmpty()] | |
| [Alias('ID')] | |
| [string] | |
| $Name | |
| ) | |
| $isPID = $true | |
| $ProcessID = $null | |
| try { | |
| $ProcessID = [int]$Name | |
| } | |
| catch { | |
| $isPID = $false | |
| } | |
| if($isPID) | |
| { | |
| $Process = get-process -Id $ProcessID | |
| } | |
| else { | |
| $Process = get-process -Name $Name | |
| } | |
| if($Process) | |
| { | |
| $handle = [kernel32]::OpenProcess("ALL",$false,($Process.Id)) | |
| [ntdll]::NtSuspendProcess($handle) | |
| [kernel32]::CloseHandle($handle) | |
| } | |
| } | |
| function Resume-Process { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory=$true,ValueFromPipeline = $True)] | |
| [ValidateNotNullorEmpty()] | |
| [Alias('ID')] | |
| [string] | |
| $Name | |
| ) | |
| $isPID = $true | |
| $ProcessID = $null | |
| try { | |
| $ProcessID = [int]$Name | |
| } | |
| catch { | |
| $isPID = $false | |
| } | |
| if($isPID) | |
| { | |
| $Process = get-process -Id $ProcessID | |
| } | |
| else { | |
| $Process = get-process -Name $Name | |
| } | |
| if($Process) | |
| { | |
| $handle = [kernel32]::OpenProcess("ALL",$false,($Process.Id)) | |
| [ntdll]::NtResumeProcess($handle) | |
| [kernel32]::CloseHandle($handle) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment