Skip to content

Instantly share code, notes, and snippets.

@Iristyle
Last active April 21, 2017 23:53
Show Gist options
  • Save Iristyle/effcc874e567cd1798f9217848e280ec to your computer and use it in GitHub Desktop.
Save Iristyle/effcc874e567cd1798f9217848e280ec to your computer and use it in GitHub Desktop.
PowerShell module interactive testing
$namedPipeName = 'dceb7903-d338-4207-938b-f5a1384dcfe8PuppetPsHost'
$client = New-Object System.IO.Pipes.NamedPipeClientStream($namedPipeName)
$client.Connect()
$code = @"
`$params = @{
Code = @'
Start-Process powershell.exe
if (schtasks /query /fo csv 2> $null | ConvertFrom-Csv | Where-Object { $_.TaskName -eq "\wm\DSC\DSC Fact Collector" }) { exit 0;} else { exit 1; }
'@
TimeoutMilliseconds = 300000
WorkingDirectory = "C:\Windows\System32\WindowsPowerShell\v1.0"
}
Invoke-PowerShellUserCode @params
"@
$bytes = [System.Text.Encoding]::UTF8.GetBytes($code)
# send "execute"
$client.WriteByte(1)
# send length of code
$lengthBytes = [BitConverter]::GetBytes($bytes.Length)
$client.Write($lengthBytes, 0, $lengthBytes.Length)
# and actual code
$client.Write($bytes, 0, $bytes.Length)
#place to hold the bytes
$result = New-Object Byte[] 16384
# read length and response
$client.Read($result, 0, 16384)
$client.Read($result, 0, 16384)
#### ways to stop server from running
# ask PS to exit
$code = 'exit'
$bytes = [System.Text.Encoding]::UTF8.GetBytes($code)
# send "execute" command / length of 'exit' / 'exit' command
$client.WriteByte(1)
$lengthBytes = [BitConverter]::GetBytes($bytes.Length)
$client.Write($lengthBytes, 0, $lengthBytes.Length)
$client.Write($bytes, 0, $bytes.Length)
require 'open3'
named_pipe_name = 'dceb7903-d338-4207-938b-f5a1384dcfe8PuppetPsHost'
pipe_path = "\\\\.\\pipe\\#{named_pipe_name}"
pipe = File.open(pipe_path, 'r+b')
code =<<-END
$params = @{
Code = @'
Start-Process powershell.exe
if (schtasks /query /fo csv 2> $null | ConvertFrom-Csv | Where-Object { $_.TaskName -eq "\\wm\\DSC\\DSC Fact Collector" }) { exit 0;} else { exit 1; }
'@
TimeoutMilliseconds = 300000
WorkingDirectory = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0"
}
Invoke-PowerShellUserCode @params
END
def pipe_data(data)
msg = data.encode(Encoding::UTF_8)
# https://ruby-doc.org/core-1.9.3/Array.html#method-i-pack
[msg.bytes.length].pack('V') + msg.force_encoding(Encoding::BINARY)
end
# send "execute"
pipe.syswrite("\x01")
pipe.flush()
# length + command
pipe.syswrite(pipe_data(code))
# read 32-bit length, then bytes of response
l = pipe.readpartial(16384)
l = pipe.readpartial(16384)
#### ways to stop server from running
# ask server to exit
pipe.syswrite("\x00")
pipe.flush()
# close pipe interactively
pipe.close()
# use an at_exit handler so when Ruby process exits
at_exit
{
pipe.close()
}
REM assuming c:\init.ps1 is the file from:
REM https://github.com/puppetlabs/puppetlabs-powershell/blob/master/lib/puppet_x/templates/init_ps.ps1
powershell.exe -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -File "C:\init.ps1" "dceb7903-d338-4207-938b-f5a1384dcfe8PuppetPsHost" -EmitDebugOutput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment