49 changed files with 3621 additions and 3995 deletions
@ -1,4 +0,0 @@
|
||||
::lindex tcl;#\ |
||||
@call "%~dp0..\src\runtime\tclkit86bi.exe" "%~dp0sdx.kit" %* & goto :eof |
||||
# --- --- --- --- --- --- --- --- --- --- --- --- ---begin comments only |
||||
# |
||||
Binary file not shown.
@ -0,0 +1,201 @@
|
||||
# from github.com/dahlbyk/posh-git |
||||
# ------------------------------------------------------------------------------------ |
||||
#Copyright (c) 2010-2018 Keith Dahlby, Keith Hill, and contributors |
||||
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
||||
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
||||
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||||
# ------------------------------------------------------------------------------------ |
||||
|
||||
# Always skip setting the console mode on non-Windows platforms. |
||||
if (($PSVersionTable.PSVersion.Major -ge 6) -and !$IsWindows) { |
||||
function Set-ConsoleMode { |
||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] |
||||
param() |
||||
} |
||||
|
||||
return |
||||
} |
||||
|
||||
$consoleModeSource = @" |
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
public class NativeConsoleMethods |
||||
{ |
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern IntPtr GetStdHandle(int handleId); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool GetConsoleMode(IntPtr hConsoleOutput, out uint dwMode); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool SetConsoleMode(IntPtr hConsoleOutput, uint dwMode); |
||||
|
||||
public static uint GetConsoleMode(bool input = false) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
uint mode; |
||||
if (GetConsoleMode(handle, out mode)) |
||||
{ |
||||
return mode; |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
|
||||
public static uint SetConsoleMode(bool input, uint mode) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
if (SetConsoleMode(handle, mode)) |
||||
{ |
||||
return GetConsoleMode(input); |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
} |
||||
"@ |
||||
|
||||
[Flags()] |
||||
enum ConsoleModeInputFlags |
||||
{ |
||||
ENABLE_PROCESSED_INPUT = 0x0001 |
||||
ENABLE_LINE_INPUT = 0x0002 |
||||
ENABLE_ECHO_INPUT = 0x0004 |
||||
ENABLE_WINDOW_INPUT = 0x0008 |
||||
ENABLE_MOUSE_INPUT = 0x0010 |
||||
ENABLE_INSERT_MODE = 0x0020 |
||||
ENABLE_QUICK_EDIT_MODE = 0x0040 |
||||
ENABLE_EXTENDED_FLAGS = 0x0080 |
||||
ENABLE_AUTO_POSITION = 0x0100 |
||||
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0200 |
||||
} |
||||
|
||||
[Flags()] |
||||
enum ConsoleModeOutputFlags |
||||
{ |
||||
ENABLE_PROCESSED_OUTPUT = 0x0001 |
||||
ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002 |
||||
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 |
||||
} |
||||
|
||||
function Set-ConsoleMode |
||||
{ |
||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] |
||||
param( |
||||
[Parameter(ParameterSetName = "ANSI")] |
||||
[switch] |
||||
$ANSI, |
||||
|
||||
[Parameter(ParameterSetName = "Mode")] |
||||
[uint32] |
||||
$Mode, |
||||
|
||||
[switch] |
||||
$StandardInput |
||||
) |
||||
|
||||
begin { |
||||
# Module import is speeded up by deferring the Add-Type until the first time this function is called. |
||||
# Add the NativeConsoleMethods type but only once per session. |
||||
if (!('NativeConsoleMethods' -as [System.Type])) { |
||||
Add-Type $consoleModeSource |
||||
} |
||||
} |
||||
|
||||
end { |
||||
if ($ANSI) |
||||
{ |
||||
$outputMode = [NativeConsoleMethods]::GetConsoleMode($false) |
||||
$null = [NativeConsoleMethods]::SetConsoleMode($false, $outputMode -bor [ConsoleModeOutputFlags]::ENABLE_VIRTUAL_TERMINAL_PROCESSING) |
||||
|
||||
if ($StandardInput) |
||||
{ |
||||
$inputMode = [NativeConsoleMethods]::GetConsoleMode($true) |
||||
$null = [NativeConsoleMethods]::SetConsoleMode($true, $inputMode -bor [ConsoleModeInputFlags]::ENABLE_VIRTUAL_TERMINAL_PROCESSING) |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
[NativeConsoleMethods]::SetConsoleMode($StandardInput, $Mode) |
||||
} |
||||
} |
||||
} |
||||
function Get-ConsoleMode |
||||
{ |
||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")] |
||||
param( |
||||
[switch] |
||||
$StandardInput |
||||
) |
||||
|
||||
begin { |
||||
# Module import is speeded up by deferring the Add-Type until the first time this function is called. |
||||
# Add the NativeConsoleMethods type but only once per session. |
||||
if (!('NativeConsoleMethods' -as [System.Type])) { |
||||
Add-Type $consoleModeSource |
||||
} |
||||
} |
||||
|
||||
end { |
||||
$mode = [NativeConsoleMethods]::GetConsoleMode($StandardInput) |
||||
write-Output $mode |
||||
return |
||||
} |
||||
} |
||||
function psmain { |
||||
param ( |
||||
[validateSet('enableRaw', 'disableRaw')] |
||||
[string]$Action |
||||
) |
||||
$inputflags = Get-ConsoleMode -StandardInput |
||||
$resultflags = $inputflags #default |
||||
if (($inputflags -band [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -eq [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) { |
||||
#cooked mode |
||||
$initialstate = "cooked" |
||||
if ($action -eq "enableraw") { |
||||
#disable cooked flags |
||||
$disable = [uint32](-bnot [uint32][ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -band ( -bnot [uint32][ConsoleModeInputFlags]::ENABLE_ECHO_INPUT) |
||||
$adjustedflags = $inputflags -band ($disable) |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags) |
||||
} |
||||
} else { |
||||
#raw mode |
||||
$initialstate = "raw" |
||||
if ($action -eq "disableraw") { |
||||
#set cooked flags |
||||
$adjustedflags = $inputflags -bor [ConsoleModeInputFlags]::ENABLE_LINE_INPUT -bor [ConsoleModeInputFlags]::ENABLE_ECHO_INPUT |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags) |
||||
} |
||||
} |
||||
#return in format that can act as a tcl dict |
||||
write-host "startflags: $inputflags initialstate: $initialstate action: $Action endflags: $resultflags" |
||||
} |
||||
psmain @args |
||||
|
||||
#write-host (Get-ConsoleMode) |
||||
#Set-ConsoleMode -ANSI -StandardInput |
||||
#write-host (Get-ConsoleMode) |
||||
|
||||
#test toggle |
||||
#if ((($inputflags -band [ConsoleModeInputFlags]::ENABLE_QUICK_EDIT_MODE)) -eq [ConsoleModeInputFlags]::ENABLE_QUICK_EDIT_MODE) { |
||||
# #quick edit is on |
||||
# write-host "quick edit is on" |
||||
# $adjustedflags = $inputflags -band (-bnot [uint32][ConsoleModeInputFlags]::ENABLE_QUICK_EDIT_MODE) |
||||
# $resultflags = [NativeConsoleMethods]::SetConsoleMode($true, $adjustedflags) |
||||
## |
||||
#} else { |
||||
# #quick edit is off |
||||
# write-host "quick edit is off" |
||||
# $resultflags = [NativeConsoleMethods]::SetConsoleMode($true, $inputflags -bor [ConsoleModeInputFlags]::ENABLE_QUICK_EDIT_MODE) |
||||
#} |
||||
|
||||
|
||||
#todo - parameters so it doesn't act as a toggle |
||||
#we want to be able to explicitly set raw vs cooked |
||||
|
||||
#multi |
||||
|
||||
|
||||
|
||||
@ -0,0 +1,91 @@
|
||||
#!SEMICOLONS must be placed after each command as scriptdata needs to be sent to powershell directly with the -c parameter! |
||||
; |
||||
|
||||
if ($PSVersionTable.PSVersion.Major -le 5) { |
||||
# For Windows PowerShell, we want to remove any PowerShell 7 paths from PSModulePath |
||||
#snipped from https://github.com/PowerShell/DSC/pull/777/commits/af9b99a4d38e0cf1e54c4bbd89cbb6a8a8598c4e |
||||
#Presumably users are supposed to know not to have custom paths for powershell desktop containing a 'powershell' subfolder?? |
||||
; |
||||
$env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object { $_ -notlike '*\powershell\*' }) -join ';'; |
||||
}; |
||||
|
||||
$consoleModeSource = @" |
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
public class NativeConsoleMethods |
||||
{ |
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern IntPtr GetStdHandle(int handleId); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool GetConsoleMode(IntPtr hConsoleOutput, out uint dwMode); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool SetConsoleMode(IntPtr hConsoleOutput, uint dwMode); |
||||
|
||||
public static uint GetConsoleMode(bool input = false) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
uint mode; |
||||
if (GetConsoleMode(handle, out mode)) |
||||
{ |
||||
return mode; |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
|
||||
public static uint SetConsoleMode(bool input, uint mode) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
if (SetConsoleMode(handle, mode)) |
||||
{ |
||||
return GetConsoleMode(input); |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
} |
||||
"@ |
||||
; |
||||
[Flags()] |
||||
enum ConsoleModeInputFlags |
||||
{ |
||||
ENABLE_LINE_INPUT = 0x0002 |
||||
ENABLE_ECHO_INPUT = 0x0004 |
||||
}; |
||||
|
||||
|
||||
function psmain { |
||||
param ( |
||||
[validateSet('enableRaw', 'disableRaw')] |
||||
[string]$Action |
||||
); |
||||
# $inputflags = Get-ConsoleMode -StandardInput; |
||||
if (!('NativeConsoleMethods' -as [System.Type])) { |
||||
Add-Type $consoleModeSource |
||||
} |
||||
$inputFlags = [NativeConsoleMethods]::GetConsoleMode($true); |
||||
$resultflags = $inputflags; |
||||
if (($inputflags -band [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -eq [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) { |
||||
#cooked mode |
||||
$initialstate = "cooked"; |
||||
if ($action -eq "enableraw") { |
||||
#disable cooked flags |
||||
$disable = [uint32](-bnot [uint32][ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -band ( -bnot [uint32][ConsoleModeInputFlags]::ENABLE_ECHO_INPUT); |
||||
$adjustedflags = $inputflags -band ($disable); |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags); |
||||
} |
||||
} else { |
||||
#raw mode |
||||
$initialstate = "raw"; |
||||
if ($action -eq "disableraw") { |
||||
#set cooked flags |
||||
$adjustedflags = $inputflags -bor [ConsoleModeInputFlags]::ENABLE_LINE_INPUT -bor [ConsoleModeInputFlags]::ENABLE_ECHO_INPUT; |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags); |
||||
} |
||||
} |
||||
#return in format that can act as a tcl dict |
||||
#write-host "startflags: $inputflags initialstate: $initialstate action: $Action endflags: $resultflags"; |
||||
}; |
||||
psmain 'enableRaw'; |
||||
|
||||
@ -0,0 +1,144 @@
|
||||
#!SEMICOLONS must be placed after each command as scriptdata needs to be sent to powershell directly with the -c parameter! |
||||
; |
||||
|
||||
if ($PSVersionTable.PSVersion.Major -le 5) { |
||||
# For Windows PowerShell, we want to remove any PowerShell 7 paths from PSModulePath |
||||
#snipped from https://github.com/PowerShell/DSC/pull/777/commits/af9b99a4d38e0cf1e54c4bbd89cbb6a8a8598c4e |
||||
#Presumably users are supposed to know not to have custom paths for powershell desktop containing a 'powershell' subfolder?? |
||||
; |
||||
$env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object { $_ -notlike '*\powershell\*' }) -join ';'; |
||||
}; |
||||
|
||||
$consoleModeSource = @" |
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
public class NativeConsoleMethods |
||||
{ |
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern IntPtr GetStdHandle(int handleId); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool GetConsoleMode(IntPtr hConsoleOutput, out uint dwMode); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool SetConsoleMode(IntPtr hConsoleOutput, uint dwMode); |
||||
|
||||
public static uint GetConsoleMode(bool input = false) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
uint mode; |
||||
if (GetConsoleMode(handle, out mode)) |
||||
{ |
||||
return mode; |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
|
||||
public static uint SetConsoleMode(bool input, uint mode) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
if (SetConsoleMode(handle, mode)) |
||||
{ |
||||
return GetConsoleMode(input); |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
} |
||||
"@ |
||||
; |
||||
[Flags()] |
||||
enum ConsoleModeInputFlags |
||||
{ |
||||
ENABLE_LINE_INPUT = 0x0002 |
||||
ENABLE_ECHO_INPUT = 0x0004 |
||||
}; |
||||
|
||||
if (!('NativeConsoleMethods' -as [System.Type])) { |
||||
Add-Type $consoleModeSource |
||||
} |
||||
|
||||
function psmain { |
||||
param ( |
||||
[validateSet('enableRaw', 'disableRaw')] |
||||
[string]$Action |
||||
); |
||||
# $inputflags = Get-ConsoleMode -StandardInput; |
||||
if (!('NativeConsoleMethods' -as [System.Type])) { |
||||
Add-Type $consoleModeSource |
||||
} |
||||
$inputFlags = [NativeConsoleMethods]::GetConsoleMode($true); |
||||
$resultflags = $inputflags; |
||||
if (($inputflags -band [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -eq [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) { |
||||
#cooked mode |
||||
$initialstate = "cooked"; |
||||
if ($action -eq "enableraw") { |
||||
#disable cooked flags |
||||
$disable = [uint32](-bnot [uint32][ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -band ( -bnot [uint32][ConsoleModeInputFlags]::ENABLE_ECHO_INPUT); |
||||
$adjustedflags = $inputflags -band ($disable); |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags); |
||||
} |
||||
} else { |
||||
#raw mode |
||||
$initialstate = "raw"; |
||||
if ($action -eq "disableraw") { |
||||
#set cooked flags |
||||
$adjustedflags = $inputflags -bor [ConsoleModeInputFlags]::ENABLE_LINE_INPUT -bor [ConsoleModeInputFlags]::ENABLE_ECHO_INPUT; |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags); |
||||
} |
||||
} |
||||
#return in format that can act as a tcl dict |
||||
#write-host "startflags: $inputflags initialstate: $initialstate action: $Action endflags: $resultflags"; |
||||
}; |
||||
# psmain 'enableRaw'; |
||||
|
||||
$consoleid = $args[0]; |
||||
if ([string]::IsNullOrEmpty($consoleid)) { |
||||
$consoleid= "<punkshell_consoleid>" |
||||
}; |
||||
$pipeName = "punkshell_ps_consolemode_$consoleid"; |
||||
"pipename: $pipeName" |
||||
$pipeServer = New-Object System.IO.Pipes.NamedPipeServerStream($pipeName); |
||||
try { |
||||
while ($true) { |
||||
#"Waiting for connection on '$pipeName'"; |
||||
|
||||
$pipeServer.WaitForConnection(); |
||||
#"Connection established"; |
||||
$pipeReader = New-Object System.IO.StreamReader($pipeServer); |
||||
#$pipeWriter = New-Object System.IO.StreamWriter($pipeServer); |
||||
#$pipeWriter.AutoFlush = $true; |
||||
$request = $pipeReader.ReadLine(); |
||||
# "Received request: $request"; |
||||
if ($request -eq "exit") { |
||||
"consolemode_server.ps1 Exiting"; |
||||
exit; |
||||
} elseif ($request -eq "") { |
||||
#"Empty input"; |
||||
$pipeServer.Disconnect(); |
||||
#"Disconnected"; |
||||
continue; |
||||
} elseif ($request -eq $none) { |
||||
"Remote disconnected before sending"; |
||||
$pipeServer.Disconnect(); |
||||
"Disconnected"; |
||||
continue; |
||||
} elseif ($request -eq "enableraw") { |
||||
#$result = psmain 'enableRaw'; |
||||
$null = psmain 'enableRaw' |
||||
# "Sending result: '$result'"; |
||||
#$pipeWriter.Write($result); |
||||
$pipeServer.Disconnect(); |
||||
continue; |
||||
} else { |
||||
"consolemode_server.ps1 ignoring request: $request"; |
||||
$pipeServer.Disconnect(); |
||||
continue; |
||||
} |
||||
} |
||||
} |
||||
finally { |
||||
$pipeServer.Dispose(); |
||||
}; |
||||
|
||||
|
||||
@ -0,0 +1,244 @@
|
||||
#!SEMICOLONS must be placed after each command as scriptdata needs to be sent to powershell directly with the -c parameter! |
||||
; |
||||
|
||||
if ($PSVersionTable.PSVersion.Major -le 5) { |
||||
# For Windows PowerShell, we want to remove any PowerShell 7 paths from PSModulePath |
||||
#snipped from https://github.com/PowerShell/DSC/pull/777/commits/af9b99a4d38e0cf1e54c4bbd89cbb6a8a8598c4e |
||||
#Presumably users are supposed to know not to have custom paths for powershell desktop containing a 'powershell' subfolder?? |
||||
; |
||||
$env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object { $_ -notlike '*\powershell\*' }) -join ';'; |
||||
}; |
||||
|
||||
$helper = @' |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Linq.Expressions; |
||||
using System.Management.Automation.Runspaces; |
||||
|
||||
public class RunspacedDelegateFactory |
||||
{ |
||||
public static Delegate NewRunspacedDelegate(Delegate _delegate, Runspace runspace) |
||||
{ |
||||
Action setRunspace = () => Runspace.DefaultRunspace = runspace; |
||||
return ConcatActionToDelegate(setRunspace, _delegate); |
||||
} |
||||
|
||||
private static Expression ExpressionInvoke(Delegate _delegate, params Expression[] arguments) |
||||
{ |
||||
var invokeMethod = _delegate.GetType().GetMethod("Invoke"); |
||||
return Expression.Call(Expression.Constant(_delegate), invokeMethod, arguments); |
||||
} |
||||
|
||||
public static Delegate ConcatActionToDelegate(Action a, Delegate d) |
||||
{ |
||||
var parameters = |
||||
d.GetType().GetMethod("Invoke").GetParameters() |
||||
.Select(p => Expression.Parameter(p.ParameterType, p.Name)) |
||||
.ToArray(); |
||||
|
||||
Expression body = Expression.Block(ExpressionInvoke(a), ExpressionInvoke(d, parameters)); |
||||
var lambda = Expression.Lambda(d.GetType(), body, parameters); |
||||
var compiled = lambda.Compile(); |
||||
return compiled; |
||||
} |
||||
} |
||||
'@ |
||||
add-type -TypeDefinition $helper |
||||
|
||||
#region console |
||||
|
||||
$consoleModeSource = @" |
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
public class NativeConsoleMethods |
||||
{ |
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern IntPtr GetStdHandle(int handleId); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool GetConsoleMode(IntPtr hConsoleOutput, out uint dwMode); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool SetConsoleMode(IntPtr hConsoleOutput, uint dwMode); |
||||
|
||||
public static uint GetConsoleMode(bool input = false) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
uint mode; |
||||
if (GetConsoleMode(handle, out mode)) |
||||
{ |
||||
return mode; |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
|
||||
public static uint SetConsoleMode(bool input, uint mode) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
if (SetConsoleMode(handle, mode)) |
||||
{ |
||||
return GetConsoleMode(input); |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
} |
||||
"@ |
||||
; |
||||
[Flags()] |
||||
enum ConsoleModeInputFlags |
||||
{ |
||||
ENABLE_LINE_INPUT = 0x0002 |
||||
ENABLE_ECHO_INPUT = 0x0004 |
||||
}; |
||||
|
||||
if (!('NativeConsoleMethods' -as [System.Type])) { |
||||
Add-Type $consoleModeSource |
||||
} |
||||
|
||||
function psmain { |
||||
param ( |
||||
[validateSet('enableRaw', 'disableRaw')] |
||||
[string]$Action |
||||
); |
||||
# $inputflags = Get-ConsoleMode -StandardInput; |
||||
if (!('NativeConsoleMethods' -as [System.Type])) { |
||||
Add-Type $consoleModeSource |
||||
} |
||||
$inputFlags = [NativeConsoleMethods]::GetConsoleMode($true); |
||||
$resultflags = $inputflags; |
||||
if (($inputflags -band [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -eq [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) { |
||||
#cooked mode |
||||
$initialstate = "cooked"; |
||||
if ($action -eq "enableraw") { |
||||
#disable cooked flags |
||||
$disable = [uint32](-bnot [uint32][ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -band ( -bnot [uint32][ConsoleModeInputFlags]::ENABLE_ECHO_INPUT); |
||||
$adjustedflags = $inputflags -band ($disable); |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags); |
||||
} |
||||
} else { |
||||
#raw mode |
||||
$initialstate = "raw"; |
||||
if ($action -eq "disableraw") { |
||||
#set cooked flags |
||||
$adjustedflags = $inputflags -bor [ConsoleModeInputFlags]::ENABLE_LINE_INPUT -bor [ConsoleModeInputFlags]::ENABLE_ECHO_INPUT; |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags); |
||||
} |
||||
} |
||||
#return in format that can act as a tcl dict |
||||
#write-host "startflags: $inputflags initialstate: $initialstate action: $Action endflags: $resultflags"; |
||||
}; |
||||
# psmain 'enableRaw'; |
||||
#endregion console |
||||
|
||||
|
||||
$consoleid = $args[0]; |
||||
if ([string]::IsNullOrEmpty($consoleid)) { |
||||
$consoleid= "<punkshell_consoleid>" |
||||
}; |
||||
$pipeName = "punkshell_ps_consolemode_$consoleid"; |
||||
"pipename: $pipeName"; |
||||
|
||||
$pipeServer = New-Object System.IO.Pipes.NamedPipeServerStream( |
||||
$pipeName, |
||||
[System.IO.Pipes.PipeDirection]::In, |
||||
1, |
||||
[System.IO.Pipes.PipeTransmissionMode]::Byte, |
||||
[System.IO.Pipes.PipeOptions]::Asynchronous |
||||
); |
||||
#$pipeServer = New-Object System.IO.Pipes.NamedPipeServerStream($pipeName); |
||||
; |
||||
|
||||
# Define the callback function for when a client connects |
||||
$callback = [System.AsyncCallback]{ |
||||
param($asyncResult); |
||||
|
||||
$se = $asyncResult.AsyncState.Sync; |
||||
$ps = $asyncResult.AsyncState.Pipeserver; |
||||
$pn = $asyncResult.AsyncState.Pipename; |
||||
Write-Host "Client connected - $pn"; |
||||
|
||||
# End the asynchronous wait operation |
||||
; |
||||
$ps.EndWaitForConnection($asyncResult); |
||||
#?? |
||||
; |
||||
# You can now perform read/write operations with the client |
||||
# For example, create a StreamReader and StreamWriter |
||||
; |
||||
$streamReader = New-Object System.IO.StreamReader($ps); |
||||
#$streamWriter = New-Object System.IO.StreamWriter($pipeServer); |
||||
#$streamWriter.AutoFlush = $true; |
||||
try { |
||||
$message = $streamReader.ReadLine(); |
||||
Write-Host "Received: $message"; |
||||
; |
||||
#$asyncResult.Message = $message; |
||||
; |
||||
} catch { |
||||
Write-Error "Error during communication: $($_.Exception.Message)"; |
||||
} finally { |
||||
# sever connection with client but keep the named pipe |
||||
if ($streamReader -ne $null) { |
||||
Write-Host "streamreader closing"; |
||||
$streamReader.Close(); |
||||
Write-Host "streamreader closed"; |
||||
} |
||||
Write-Host "Client disconnecting. $pn"; |
||||
$ps.Disconnect(); |
||||
Write-Host "Client disconnected. $pn"; |
||||
#$ps.Disconnect(); |
||||
#[System.Console]::Out.Flush(); |
||||
; |
||||
}; |
||||
write-host "HERE"; |
||||
|
||||
$se.set(); |
||||
|
||||
# Optionally, you can call BeginWaitForConnection again to listen for another client |
||||
# if your server is designed for multiple connections over time. |
||||
# $pipeServer.BeginWaitForConnection($callback, $null) |
||||
$ps.BeginWaitForConnection($callback, $null) |
||||
; |
||||
}; |
||||
|
||||
$syncEvent = New-Object System.Threading.ManualResetEvent($false); |
||||
|
||||
$runspacedDelegate = [RunspacedDelegateFactory]::NewRunspacedDelegate($callback, [Runspace]::DefaultRunspace); |
||||
|
||||
$loop = 0; |
||||
while ($loop -lt 15) { |
||||
Write-Host "Waiting for client connection on pipe '$pipeName'..."; |
||||
# Begin the asynchronous wait for a client connection |
||||
; |
||||
$state = [PSCustomObject]@{ |
||||
Loop = $loop |
||||
Sync = $SyncEvent |
||||
Pipeserver = $pipeServer |
||||
Pipename = $pipename |
||||
Message = "" |
||||
}; |
||||
$x = $pipeServer.BeginWaitForConnection($runspacedDelegate, $state ); |
||||
$syncEvent.WaitOne(10000); |
||||
# $x | Get-Member | write-host |
||||
; |
||||
write-host "msg: $(${x}.AsyncState.Message)" |
||||
if ($x.IsCompleted) { |
||||
write-host "completed"; |
||||
}; |
||||
$SyncEvent.reset(); |
||||
|
||||
$loop += 1; |
||||
#$cli = New-Object System.IO.Pipes.NamedPipeClientStream($pipeName); |
||||
#$cli.w |
||||
} |
||||
|
||||
# Keep the script running to allow the asynchronous operation to complete |
||||
# In a real-world scenario, you might have a loop or other logic here. |
||||
#Read-Host "Press Enter to exit the server." |
||||
|
||||
# Clean up |
||||
$pipeServer.Dispose(); |
||||
|
||||
|
||||
@ -0,0 +1,262 @@
|
||||
#!SEMICOLONS must be placed after each command as scriptdata needs to be sent to powershell directly with the -c parameter! |
||||
; |
||||
|
||||
if ($PSVersionTable.PSVersion.Major -le 5) { |
||||
# For Windows PowerShell, we want to remove any PowerShell 7 paths from PSModulePath |
||||
#snipped from https://github.com/PowerShell/DSC/pull/777/commits/af9b99a4d38e0cf1e54c4bbd89cbb6a8a8598c4e |
||||
#Presumably users are supposed to know not to have custom paths for powershell desktop containing a 'powershell' subfolder?? |
||||
; |
||||
$env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object { $_ -notlike '*\powershell\*' }) -join ';'; |
||||
}; |
||||
|
||||
$consoleModeSource = @" |
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
public class NativeConsoleMethods |
||||
{ |
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern IntPtr GetStdHandle(int handleId); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool GetConsoleMode(IntPtr hConsoleOutput, out uint dwMode); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool SetConsoleMode(IntPtr hConsoleOutput, uint dwMode); |
||||
|
||||
public static uint GetConsoleMode(bool input = false) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
uint mode; |
||||
if (GetConsoleMode(handle, out mode)) |
||||
{ |
||||
return mode; |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
|
||||
public static uint SetConsoleMode(bool input, uint mode) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
if (SetConsoleMode(handle, mode)) |
||||
{ |
||||
return GetConsoleMode(input); |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
} |
||||
"@ |
||||
; |
||||
[Flags()] |
||||
enum ConsoleModeInputFlags |
||||
{ |
||||
ENABLE_PROCESSED_INPUT = 0x0001 |
||||
ENABLE_LINE_INPUT = 0x0002 |
||||
ENABLE_ECHO_INPUT = 0x0004 |
||||
ENABLE_WINDOW_INPUT = 0x0008 |
||||
ENABLE_MOUSE_INPUT = 0x0010 |
||||
ENABLE_INSERT_MODE = 0x0020 |
||||
ENABLE_QUICK_EDIT_MODE = 0x0040 |
||||
ENABLE_EXTENDED_FLAGS = 0x0080 |
||||
ENABLE_AUTO_POSITION = 0x0100 |
||||
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0200 |
||||
}; |
||||
|
||||
[Flags()] |
||||
enum ConsoleModeOutputFlags |
||||
{ |
||||
ENABLE_PROCESSED_OUTPUT = 0x0001 |
||||
ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002 |
||||
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 |
||||
}; |
||||
|
||||
if (!('NativeConsoleMethods' -as [System.Type])) { |
||||
Add-Type $consoleModeSource |
||||
} |
||||
|
||||
function rawmode { |
||||
param ( |
||||
[validateSet('enable', 'disable')] |
||||
[string]$Action |
||||
); |
||||
# $inputflags = Get-ConsoleMode -StandardInput; |
||||
if (!('NativeConsoleMethods' -as [System.Type])) { |
||||
Add-Type $consoleModeSource |
||||
} |
||||
$inputFlags = [NativeConsoleMethods]::GetConsoleMode($true); |
||||
$resultflags = $inputflags; |
||||
if (($inputflags -band [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -eq [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) { |
||||
#cooked mode |
||||
$initialstate = "cooked"; |
||||
if ($action -eq "enable") { |
||||
#disable cooked flags |
||||
$disable = [uint32](-bnot [uint32][ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -band ( -bnot [uint32][ConsoleModeInputFlags]::ENABLE_ECHO_INPUT); |
||||
$adjustedflags = $inputflags -band ($disable); |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags); |
||||
} |
||||
} else { |
||||
#raw mode |
||||
$initialstate = "raw"; |
||||
if ($action -eq "disable") { |
||||
#set cooked flags |
||||
$adjustedflags = $inputflags -bor [ConsoleModeInputFlags]::ENABLE_LINE_INPUT -bor [ConsoleModeInputFlags]::ENABLE_ECHO_INPUT; |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags); |
||||
} |
||||
} |
||||
#return in format that can act as a tcl dict |
||||
#write-host "startflags: $inputflags initialstate: $initialstate action: $Action endflags: $resultflags"; |
||||
}; |
||||
# rawmode 'enable'; |
||||
|
||||
$consoleid = $args[0]; |
||||
if ([string]::IsNullOrEmpty($consoleid)) { |
||||
$consoleid= "<punkshell_consoleid>" |
||||
}; |
||||
$pipeName = "punkshell_ps_consolemode_$consoleid"; |
||||
"pipename: $pipeName" |
||||
#$pipeServer = New-Object System.IO.Pipes.NamedPipeServerStream($pipeName); |
||||
|
||||
$sharedData = [hashtable]::Synchronized(@{}) #TSV |
||||
$scriptblock = { |
||||
param($tsv); |
||||
Add-Type -AssemblyName System.IO.Pipes; |
||||
#$pipeServer = New-Object System.IO.Pipes.NamedPipeServerStream( |
||||
# $pipeName, |
||||
# [System.IO.Pipes.PipeDirection]::In, |
||||
# 1, |
||||
# [System.IO.Pipes.PipeTransmissionMode]::Byte, |
||||
# [System.IO.Pipes.PipeOptions]::Asynchronous |
||||
#); |
||||
; |
||||
$serverloop = 0; |
||||
while ($true) { |
||||
$pipeServer = New-Object System.IO.Pipes.NamedPipeServerStream($pipeName); |
||||
$serverloop += 1; |
||||
$pipeServer.WaitForConnection(); |
||||
#write-host "Connection established"; |
||||
$reader = New-Object System.IO.StreamReader($pipeServer); |
||||
if ($reader -ne $null) { |
||||
$message = $reader.ReadLine(); |
||||
$reader.Close(); |
||||
$reader.Dispose(); |
||||
if ($message -ne $null) { |
||||
if ($message -eq "exit") { |
||||
#write-host "consolemode_server.ps1 exiting"; |
||||
$tsv.State = "done"; |
||||
break; |
||||
} elseif ($message -eq "enableraw") { |
||||
#write-host "RECEIVED: $message"; |
||||
$tsv.Message = $message; |
||||
#$tsv.Message = "$pipeName serverloop: $serverloop"; |
||||
|
||||
$tsv.Ping = Get-Date; |
||||
$msync.Set(); |
||||
}; |
||||
} else { |
||||
# write-host "consolemode_server.ps1 null-msg"; |
||||
$tsv.State = "done"; |
||||
break; |
||||
}; |
||||
}; |
||||
$pipeServer.Disconnect(); |
||||
$pipeServer.Dispose(); |
||||
}; |
||||
exit; |
||||
}; |
||||
|
||||
$keepalive_timeout = 20; #number of seconds without ping or other message, after which we terminate the process. |
||||
try { |
||||
$syncEvent = New-Object System.Threading.ManualResetEvent($false); |
||||
|
||||
$runspace = [runspacefactory]::CreateRunspace(); |
||||
[void]$runspace.Open(); |
||||
$runspace.SessionStateProxy.SetVariable("pipeName", $pipeName); |
||||
$runspace.SessionStateProxy.SetVariable("pipeServer", $null); |
||||
$runspace.SessionStateProxy.SetVariable("msync", $syncEvent); |
||||
|
||||
$powershell = [System.Management.Automation.PowerShell]::Create(); |
||||
$powershell.Runspace = $runspace; |
||||
[void]$powershell.Addscript($scriptblock).AddArgument($sharedData); |
||||
|
||||
$sharedData.State = "running"; |
||||
$sharedData.Ping = Get-Date; |
||||
$asyncResult = $powershell.BeginInvoke(); |
||||
|
||||
write-Host "Started named pipe server $pipeName in runspace" |
||||
$loop = 0; |
||||
while ($true) { |
||||
$loop += 1; |
||||
#write-host "loop $loop"; |
||||
[void]$syncEvent.WaitOne(($keepalive_timeout * 1000 / 2)); |
||||
$msg = $sharedData.Message; |
||||
#Write-Host "$pipeName Last message: $msg"; |
||||
$sharedData.Message = ""; |
||||
if ($msg -eq "enableraw") { |
||||
$null = rawmode 'enable' |
||||
} elseif ($msg -eq "disableraw") { |
||||
$null = rawmode 'disable' |
||||
} |
||||
#write-host "STATE: $(${sharedData}.State)" |
||||
if ($(${sharedData}.State) -eq "done") { |
||||
break; |
||||
}; |
||||
$tnow = Get-Date; |
||||
$elapsed = New-TimeSpan -Start $sharedData.Ping -End $tnow; |
||||
if ($elapsed.TotalSeconds -lt $keepalive_timeout) { |
||||
# write-host "ping ok"; |
||||
} else { |
||||
write-host "ping stale for pipe $pipeName - exiting"; |
||||
break; |
||||
} |
||||
[void]$syncEvent.Reset(); |
||||
# start-sleep -Milliseconds 300 |
||||
}; |
||||
} finally { |
||||
# Failing to properly shut down the run process can leave an orphan powershell process |
||||
# We need to use a client for the named pipe to send an exit message. |
||||
# Write-Host "terminating process for $pipeName"; |
||||
try { |
||||
# Write-Host "creating cli for $pipeName"; |
||||
$cli = New-Object System.IO.Pipes.NamedPipeClientStream($pipeName); |
||||
$cli.connect(1000); |
||||
#Write-Host "sending exit for $pipeName"; |
||||
$writer = new-object System.IO.StreamWriter($cli); |
||||
$writer.writeline("exit"); |
||||
$writer.flush(); |
||||
#Write-Host "disposing of cli for $pipeName"; |
||||
$cli.Dispose(); |
||||
} catch { |
||||
write-host "error during cli tidyup"; |
||||
Write-Error "error: $($PSItem.ToString())"; |
||||
Write-Host "Detailed Exception Message: $($PSItem.Exception.Message)"; |
||||
}; |
||||
|
||||
|
||||
try { |
||||
if ($null -ne $runspace) { |
||||
#Write-Host "closing runspace for $pipeName"; |
||||
$runspace.Close(); |
||||
#Write-Host "disposing of runspace for $pipeName"; |
||||
$runspace.Dispose(); |
||||
}; |
||||
} catch { |
||||
write-host "error during runspace tidyup"; |
||||
Write-Error "error: $($PSItem.ToString())"; |
||||
Write-Host "Detailed Exception Message: $($PSItem.Exception.Message)"; |
||||
} finally { |
||||
}; |
||||
|
||||
try { |
||||
if ($null -ne $powershell) { |
||||
#Write-Host "tidying up powershell for $pipeName"; |
||||
$powershell.dispose(); |
||||
}; |
||||
} finally { |
||||
}; |
||||
|
||||
}; |
||||
write-host "consolemode_server_async.ps1 shutdown for pipe $pipeName"; |
||||
exit 0; |
||||
|
||||
|
||||
|
||||
@ -0,0 +1,266 @@
|
||||
#!SEMICOLONS must be placed after each command as scriptdata needs to be sent to powershell directly with the -c parameter! |
||||
; |
||||
|
||||
if ($PSVersionTable.PSVersion.Major -le 5) { |
||||
# For Windows PowerShell, we want to remove any PowerShell 7 paths from PSModulePath |
||||
#snipped from https://github.com/PowerShell/DSC/pull/777/commits/af9b99a4d38e0cf1e54c4bbd89cbb6a8a8598c4e |
||||
#Presumably users are supposed to know not to have custom paths for powershell desktop containing a 'powershell' subfolder?? |
||||
; |
||||
$env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object { $_ -notlike '*\powershell\*' }) -join ';'; |
||||
}; |
||||
|
||||
$consoleModeSource = @" |
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
public class NativeConsoleMethods |
||||
{ |
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern IntPtr GetStdHandle(int handleId); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool GetConsoleMode(IntPtr hConsoleOutput, out uint dwMode); |
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
||||
public static extern bool SetConsoleMode(IntPtr hConsoleOutput, uint dwMode); |
||||
|
||||
public static uint GetConsoleMode(bool input = false) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
uint mode; |
||||
if (GetConsoleMode(handle, out mode)) |
||||
{ |
||||
return mode; |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
|
||||
public static uint SetConsoleMode(bool input, uint mode) |
||||
{ |
||||
var handle = GetStdHandle(input ? -10 : -11); |
||||
if (SetConsoleMode(handle, mode)) |
||||
{ |
||||
return GetConsoleMode(input); |
||||
} |
||||
return 0xffffffff; |
||||
} |
||||
} |
||||
"@ |
||||
; |
||||
[Flags()] |
||||
enum ConsoleModeInputFlags |
||||
{ |
||||
ENABLE_LINE_INPUT = 0x0002 |
||||
ENABLE_ECHO_INPUT = 0x0004 |
||||
}; |
||||
|
||||
if (!('NativeConsoleMethods' -as [System.Type])) { |
||||
Add-Type $consoleModeSource |
||||
} |
||||
|
||||
function psmain { |
||||
param ( |
||||
[validateSet('enableRaw', 'disableRaw')] |
||||
[string]$Action |
||||
); |
||||
# $inputflags = Get-ConsoleMode -StandardInput; |
||||
if (!('NativeConsoleMethods' -as [System.Type])) { |
||||
Add-Type $consoleModeSource |
||||
} |
||||
$inputFlags = [NativeConsoleMethods]::GetConsoleMode($true); |
||||
$resultflags = $inputflags; |
||||
if (($inputflags -band [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -eq [ConsoleModeInputFlags]::ENABLE_LINE_INPUT) { |
||||
#cooked mode |
||||
$initialstate = "cooked"; |
||||
if ($action -eq "enableraw") { |
||||
#disable cooked flags |
||||
$disable = [uint32](-bnot [uint32][ConsoleModeInputFlags]::ENABLE_LINE_INPUT) -band ( -bnot [uint32][ConsoleModeInputFlags]::ENABLE_ECHO_INPUT); |
||||
$adjustedflags = $inputflags -band ($disable); |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags); |
||||
} |
||||
} else { |
||||
#raw mode |
||||
$initialstate = "raw"; |
||||
if ($action -eq "disableraw") { |
||||
#set cooked flags |
||||
$adjustedflags = $inputflags -bor [ConsoleModeInputFlags]::ENABLE_LINE_INPUT -bor [ConsoleModeInputFlags]::ENABLE_ECHO_INPUT; |
||||
$resultflags = [NativeConsoleMethods]::SetConsoleMode($true,$adjustedflags); |
||||
} |
||||
} |
||||
#return in format that can act as a tcl dict |
||||
#write-host "startflags: $inputflags initialstate: $initialstate action: $Action endflags: $resultflags"; |
||||
}; |
||||
# psmain 'enableRaw' |
||||
; |
||||
$consoleid = $args[0]; |
||||
if ([string]::IsNullOrEmpty($consoleid)) { |
||||
$consoleid= "<punkshell_consoleid>" |
||||
}; |
||||
$pipeName = "punkshell_ps_consolemode_$consoleid"; |
||||
"pipename: $pipeName" |
||||
# Create the NamedPipeServerStream |
||||
$pipeServer = New-Object System.IO.Pipes.NamedPipeServerStream( |
||||
$pipeName, |
||||
[System.IO.Pipes.PipeDirection]::In, |
||||
1, |
||||
[System.IO.Pipes.PipeTransmissionMode]::Message, |
||||
[System.IO.Pipes.PipeOptions]::Asynchronous |
||||
); |
||||
; |
||||
#$pipeServer = New-Object System.IO.Pipes.NamedPipeServerStream($pipeName); |
||||
; |
||||
|
||||
|
||||
# Create a synchronization object (ManualResetEvent) to signal completion |
||||
; |
||||
$syncEvent = New-Object System.Threading.ManualResetEvent($false) |
||||
|
||||
# Define the callback function for BeginWaitForConnection |
||||
$connectionCallback = [System.AsyncCallback]{ |
||||
param($ar,$s); |
||||
# Get the NamedPipeServerStream from the AsyncResult object |
||||
; |
||||
$server = $ar.AsyncState; |
||||
# End the asynchronous wait operation |
||||
; |
||||
$server.EndWaitForConnection($ar); |
||||
Write-Host "Client connected!"; |
||||
# Create a new runspace to handle client communication |
||||
; |
||||
$runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace(); |
||||
$runspace.Open(); |
||||
# Create a PowerShell pipeline within the runspace |
||||
; |
||||
$powershell = [System.Management.Automation.PowerShell]::Create(); |
||||
$powershell.Runspace = $runspace; |
||||
|
||||
$scriptBlock = { |
||||
param($pipeStream); |
||||
$reader = New-Object System.IO.StreamReader($pipeStream); |
||||
#$writer = New-Object System.IO.StreamWriter($pipeStream); |
||||
#$writer.AutoFlush = $true; |
||||
|
||||
$message = $reader.ReadLine(); |
||||
Write-Host "Received from client: $message"; |
||||
|
||||
#$response = "Server received: $message" |
||||
#$writer.WriteLine($response) |
||||
#Write-Host "Sent to client: $response" |
||||
|
||||
# Disconnect the pipe to allow new connections if desired |
||||
; |
||||
$pipeStream.Disconnect(); |
||||
}; |
||||
# Add the script block to the PowerShell pipeline and pass the pipe stream |
||||
; |
||||
$powershell.AddScript($scriptBlock).AddArgument($server); |
||||
# Invoke the pipeline asynchronously |
||||
; |
||||
$asyncResult = $powershell.BeginInvoke(); |
||||
# You can do other work here while the client communication happens in the runspace |
||||
; |
||||
; |
||||
# Wait for the pipeline to complete and close the runspace |
||||
$powershell.EndInvoke($asyncResult); |
||||
$powershell.Dispose(); |
||||
$runspace.Close(); |
||||
$runspace.Dispose(); |
||||
|
||||
Write-Host "Client communication handled. Waiting for next connection..."; |
||||
|
||||
$s.Set(); |
||||
|
||||
# Begin waiting for the next connection; |
||||
#$server.BeginWaitForConnection($callback, $server); |
||||
; |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
$global:keep_listening = $true; |
||||
|
||||
|
||||
while ($global:keep_listening) { |
||||
# Begin waiting for a client connection asynchronously |
||||
; |
||||
|
||||
$runspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace(); |
||||
$runspace.Open(); |
||||
# Create a PowerShell pipeline within the runspace |
||||
; |
||||
$powershell = [System.Management.Automation.PowerShell]::Create(); |
||||
$powershell.Runspace = $runspace; |
||||
|
||||
$scriptblock = { |
||||
param($p,$s); |
||||
Write-Host "Waiting for client connection on pipe: $p"; |
||||
$p.BeginWaitForConnection($connectionCallback, $p,$s); |
||||
$s.WaitOne(10000); |
||||
} |
||||
$powershell.AddScript($scriptBlock) |
||||
[void]$powershell.AddParameter('p',$pipeName); |
||||
[void]$powershell.AddParameter('s',$syncEvent); |
||||
$asyncResult = $powershell.BeginInvoke(); |
||||
# You can do other work here while the client communication happens in the runspace |
||||
; |
||||
write-host "interim" |
||||
; |
||||
# Wait for the pipeline to complete and close the runspace |
||||
$powershell.EndInvoke($asyncResult); |
||||
$powershell.Dispose(); |
||||
$runspace.Close(); |
||||
$runspace.Dispose(); |
||||
write-host "looping" |
||||
}; |
||||
|
||||
#$pipeServer.BeginWaitForConnection($connectionCallback, $pipeServer); |
||||
|
||||
Write-Host "Server shutting down."; |
||||
$pipeServer.Dispose(); |
||||
|
||||
|
||||
|
||||
#try { |
||||
# while ($true) { |
||||
# #"Waiting for connection on '$pipeName'"; |
||||
# $pipeServer.WaitForConnection(); |
||||
# #"Connection established"; |
||||
# $pipeReader = New-Object System.IO.StreamReader($pipeServer); |
||||
# #$pipeWriter = New-Object System.IO.StreamWriter($pipeServer); |
||||
# #$pipeWriter.AutoFlush = $true; |
||||
# $request = $pipeReader.ReadLine(); |
||||
# # "Received request: $request"; |
||||
# if ($request -eq "exit") { |
||||
# "consolemode_server.ps1 Exiting"; |
||||
# exit; |
||||
# } elseif ($request -eq "") { |
||||
# #"Empty input"; |
||||
# $pipeServer.Disconnect(); |
||||
# #"Disconnected"; |
||||
# continue; |
||||
# } elseif ($request -eq $none) { |
||||
# "Remote disconnected before sending"; |
||||
# $pipeServer.Disconnect(); |
||||
# "Disconnected"; |
||||
# continue; |
||||
# } elseif ($request -eq "enableraw") { |
||||
# #$result = psmain 'enableRaw'; |
||||
# $null = psmain 'enableRaw' |
||||
# # "Sending result: '$result'"; |
||||
# #$pipeWriter.Write($result); |
||||
# $pipeServer.Disconnect(); |
||||
# continue; |
||||
# } else { |
||||
# "consolemode_server.ps1 ignoring request: $request"; |
||||
# $pipeServer.Disconnect(); |
||||
# continue; |
||||
# } |
||||
# } |
||||
#} |
||||
#finally { |
||||
# $pipeServer.Dispose(); |
||||
#}; |
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1 @@
|
||||
scriptapps targeting the project's bin directory go here. |
||||
Loading…
Reference in new issue