2023-01-20 20:36:28 +00:00
|
|
|
# This PowerShell script connects to a Named Pipe server,
|
|
|
|
# sends one message and then disconnects again.
|
|
|
|
# The first argument is the name of the pipe.
|
|
|
|
# The second argument is the message to send.
|
|
|
|
if ($args.length -lt 2) {
|
|
|
|
Write-Output "Usage: ./send_named_pipe.ps1 <pipename> <message> [message] ... [message]"
|
2023-09-05 11:50:03 +00:00
|
|
|
exit -1
|
2023-01-20 20:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$Wrapper = [pscustomobject]@{
|
|
|
|
Pipe = new-object System.IO.Pipes.NamedPipeClientStream(
|
|
|
|
".",
|
|
|
|
$args[0],
|
|
|
|
[System.IO.Pipes.PipeDirection]::InOut,
|
|
|
|
[System.IO.Pipes.PipeOptions]::None,
|
|
|
|
[System.Security.Principal.TokenImpersonationLevel]::Impersonation
|
|
|
|
)
|
|
|
|
Reader = $null
|
|
|
|
Writer = $null
|
|
|
|
}
|
2023-09-05 11:50:03 +00:00
|
|
|
try {
|
|
|
|
$Wrapper.Pipe.Connect(5000)
|
|
|
|
$Wrapper.Reader = New-Object System.IO.StreamReader($Wrapper.Pipe)
|
|
|
|
$Wrapper.Writer = New-Object System.IO.StreamWriter($Wrapper.Pipe)
|
|
|
|
$Wrapper.Writer.AutoFlush = $true
|
|
|
|
for ($i = 1; $i -lt $args.length; $i++) {
|
|
|
|
$Wrapper.Writer.WriteLine($args[$i])
|
|
|
|
}
|
|
|
|
# Wait for pipe contents to be read.
|
|
|
|
$Wrapper.Pipe.WaitForPipeDrain()
|
|
|
|
# Dispose the pipe, which also calls Flush and Close.
|
|
|
|
$Wrapper.Pipe.Dispose()
|
|
|
|
# Explicity set error level 0 for success, as otherwise the current error level is kept.
|
|
|
|
exit 0
|
|
|
|
} catch [TimeoutException] {
|
|
|
|
Write-Output "Timeout connecting to pipe"
|
|
|
|
exit 1
|
|
|
|
} catch [System.IO.IOException] {
|
|
|
|
Write-Output "Broken pipe"
|
|
|
|
exit 2
|
2023-01-20 20:36:28 +00:00
|
|
|
}
|