Skip to content

Instantly share code, notes, and snippets.

@jrothmanshore
Created April 9, 2012 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrothmanshore/2346495 to your computer and use it in GitHub Desktop.
Save jrothmanshore/2346495 to your computer and use it in GitHub Desktop.
Powershell script for sending a command to all nodes in a memcached cluster
#
param(
[string] $command = $(throw "command is required. example .\memdata.ps1 ""get SOMEKEYNAME""")
)
#
function readResponse($stream)
{
$encoding = new-object System.Text.AsciiEncoding
$buffer = new-object System.Byte[] 1024
while ($stream.DataAvailable)
{
$read = $stream.Read($buffer, 0, 1024)
Write-Output ($encoding.GetString($buffer, 0, $read))
}
}
#
function callport($remoteHost, $port = 11211)
{
try {
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
if ($socket -eq $null) { return; }
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter($stream)
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse $stream
} catch [Exception] {
$ex = $_.Exception
Write-Output ("Error: {0}" -f $ex.Message)
}
}
# all servers in the cluster
$servers = @( "SERVER1", "SERVER2", "SERVER3")
$servers |% {
$remoteHost = [string]$_
Write-Output $remoteHost
# change value below to your memcached port if not the default of 11211
# and call callport as callport $remoteHost YourPortNumber
callport $remoteHost
}
# script end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment