Skip to content

Instantly share code, notes, and snippets.

@kennystrawnmusic
Last active January 7, 2026 08:32
Show Gist options
  • Select an option

  • Save kennystrawnmusic/b5fc2043606a077769e55c6d8b8ed9ba to your computer and use it in GitHub Desktop.

Select an option

Save kennystrawnmusic/b5fc2043606a077769e55c6d8b8ed9ba to your computer and use it in GitHub Desktop.
function Invoke-RemoteCustomTool {
[CmdletBinding(DefaultParameterSetName="PasswordAuth")]
param(
[Parameter(ParameterSetName="PasswordAuth", Mandatory=$true)]
[System.Management.Automation.PSCredential]$Credential,
[Parameter(Mandatory=$true)]
[string]$ComputerName,
[Parameter(Mandatory=$true)]
[string]$ProgName,
[Parameter(Mandatory=$true)]
[string]$SourcePath,
[Parameter(Mandatory=$true)]
[string]$ArgumentList,
[Parameter(Mandatory=$true)]
[string]$Arch,
[Parameter(ParameterSetName="PassTheTicket")]
[switch]$PTT
)
if (-not (Get-Command MSBuild.exe -ErrorAction SilentlyContinue)) {
Write-Host "Installing Visual Studio Build Tools from WinGet..."
winget install -e --scope=machine --id=Microsoft.VisualStudio.BuildTools
Write-Host "Installing Visual Studio Locator (vswhere)..."
winget install -e --scope=machine --id=Microsoft.VisualStudio.Locator
Write-Host "Using Visual Studio Locator to find MSBuild path..."
$msbPath = vswhere -latest -find MSBuild\**\Bin\MSBuild.exe | select-object -first 1
Write-Host "Found MSBuild at: $msbPath"
Write-Host "Adding MSBuild to PATH for the current script..."
$parentDir = Split-Path $msbPath -Parent
$env:PATH = "$parentDir;$env:PATH"
Write-Host "It would be wise to use sysdm.cpl to permanently add MSBuild to your system PATH."
}
$csproj = Get-ChildItem -Path $SourcePath -Include '*.csproj' -Recurse | Select-Object -ExpandProperty FullName -First 1
MSBuild $csproj -t:Clean -p:"Configuration=Release;Platform=$Arch" | Out-Null
MSBuild $csproj -p:"Configuration=Release;Platform=$Arch;OutputType=Library" | Out-Null
$dll = Get-ChildItem -Path (Split-Path $csproj) -Filter '*.dll' -Recurse | Where-Object { $_.FullName -like "*\bin\$Arch\Release\*" } | Select-Object -ExpandProperty FullName -First 1
Add-Type -Path $dll
$obj = (New-Object $ProgName.Program) -as [PSCustomObject]
$conf = $ProgName + 'Config'
Register-PSSessionConfiguration -Name $conf -ModulesToImport $obj -Force
$block = {
($Using:obj)::Main($Using:ArgumentList)
}
if ($PTT) {
Invoke-Command -ComputerName $ComputerName -ConfigurationName $conf -Authentication Kerberos -ScriptBlock $block
} else {
Invoke-Command -ComputerName $ComputerName -ConfigurationName $conf -Credential $Credential -ScriptBlock $block
}
Unregister-PSSessionConfiguration -Name $conf -Force
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment