Here is the general workflow over a PSRemoting session over SSH, for example spawned with Invoke-Command -HostName foo { 'test' }
:
sequenceDiagram
box Client Machine
actor C as Pwsh Client
participant S1 as ssh client
end
# Copyright: (c) 2025, Jordan Borean (@jborean93) <[email protected]> | |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) | |
Function Get-LapsADUpdateTime { | |
<# | |
.SYNOPSIS | |
Gets the Windows LAPS Update Time. | |
.DESCRIPTION | |
Gets the Windows LAPS Update Time for the specified computer account. The output value is a DateTime object representing the update time as a UTC date time. |
# Copyright: (c) 2025, Jordan Borean (@jborean93) <[email protected]> | |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) | |
Function New-CommonTaskTriggerProp { | |
[OutputType([hashtable])] | |
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[switch] | |
$Disable, |
$k32 = New-CtypesLib Kernel32.dll | |
$free = $total = $totalFree = [int64]0 | |
$res = $k32.SetLastError().GetDiskFreeSpaceExW( | |
$k32.MarshalAs('C:\', 'LPWStr'), | |
[ref]$free, | |
[ref]$total, | |
[ref]$totalFree) | |
if (-not $res) { | |
throw [System.ComponentModel.Win32Exception]::new($k32.LastError) |
using System; | |
using System.Management.Automation.Remoting; | |
using System.Reflection; | |
using System.Threading; | |
using System.Threading.Tasks; | |
#nullable enable | |
/* | |
This code uses internal APIs of the PowerShell remoting system to create the |
#Requires -RunAsAdministrator | |
#Requires -Version 7.4 | |
using namespace System.IO | |
using namespace System.Formats.Asn1 | |
using namespace System.Globalization | |
using namespace System.Security.Cryptography | |
using namespace System.Security.Cryptography.Pkcs | |
using namespace System.Security.Cryptography.X509Certificates | |
using namespace System.Text |
<# | |
This must be run in Windows PowerShell (5.1). This will not work in PowerShell 7.x | |
as Add-Type cannot generate an output executable. | |
#> | |
Add-Type -OutputType ConsoleApplication -OutputAssembly kill.exe -TypeDefinition @' | |
using System; | |
using System.ComponentModel; | |
using System.Runtime.InteropServices; |
# Server 2025 fails to run Get-AppxPackage and other DISM module commands in | |
# a PSRemoting (psrp) session as it has a dependency on some dll's not present | |
# in the GAC and only in the powershell.exe directory. As PSRP runs through | |
# wsmprovhost.exe, it fails to find those dlls. This hack will manually load | |
# the 4 required dlls into the GAC. This is a hack and should be removed in the | |
# future if MS fix their bug on 2025. | |
Add-Type -AssemblyName "System.EnterpriseServices" | |
$publish = [System.EnterpriseServices.Internal.Publish]::new() |
Function Get-ValidatedScriptBlock { | |
[OutputType([ScriptBlock])] | |
param ( | |
[Parameter(Mandatory)] | |
[string] | |
$Name, | |
[Parameter(Mandatory)] | |
[string] | |
$ScriptAsBase64 |
# Copyright: (c) 2024, Jordan Borean (@jborean93) <[email protected]> | |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) | |
Function New-ScheduledTaskSession { | |
<# | |
.SYNOPSIS | |
Creates a PSSession for a process running as a scheduled task. | |
.DESCRIPTION | |
Creates a PSSession that can be used to run code inside a scheduled task |