Don't Do This: PSExec'ing a Batch Script Containing a PowerShell Script

This is just broken, but  broken times call for broken measures.  I'm stuck on PSH V1 for reasons best left unmentioned (or maybe it's unmentionable reasons.)  To run remote commands, I don't have remoting at my disposal - I'm stuck with SysInternal's PSExec.exe.

As shown in Lee Holmes' blog,  you have to perform some hackosity to run a script remotely:

\\path\to\psexec.exe \\computerName cmd /c "echo . | powershell \\path\to\script.ps1"

This means you can't use PSExec.exe's -c option to copy the file over, so you have to manually copy the script over.

Here's my 'solution' (to use the term exceptionally loosely):


echo @"
@echo off && copy /y %~f0 %~f0.ps1 >NUL 2>&1
echo . | powershell -nologo -noprofile %~f0.ps1 %*
del /f /q %0.ps1 >NUL 2>&1 && exit /b 0
"@ | out-null;
$args = [Environment]::GetCommandLineArgs();
$scriptName = $args[3];
$args = $args[4..($args.Count - 1)];

"rest of script goes here"
$scriptname
$args


Yeah.  Hack-n-slash scripting.