MSH> "Hello, World!" | Out-Clipboard
MSH> Get-Clipboard
Hello, World!
MSH>
MSH> get-process | Out-Clipboard
MSH> Get-Clipboard
System.Diagnostics.Process (ahnsd)
System.Diagnostics.Process (ahnsdsv)
...
System.Diagnostics.Process (winlogon)
System.Diagnostics.Process (wmiprvse)
MSH>
function set-clipboard {$input | out-string | out-clipboard}
MSH> set-alias installutil $env:windir\Microsoft.NET\Framework\v2.0.50727\installutilNow, ClipboardSnapin is loaded and you are good to go.
MSH> cd [go to Snapin assembly directory]
MSH> installutil Snapins.dll
MSH> get-mshsnapin -r
Name : ClipboardSnapin
MshVersion : 1.0
Description : Provides retrieving/sending data from/to Clipboard
MSH> add-mshsnapin ClipboardSnapin
using System;
using System.Text;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using System.Management.Automation;
namespace D2D.Snapins
{
/// <summary>
/// Define basic information of this snap in
/// </summary>
[RunInstaller(true)]
public class ClipboardSnapin : MshSnapIn
{
public ClipboardSnapin() : base()
{
}
public override string Name
{
get { return "ClipboardSnapin"; }
}
public override string Vendor
{
get { return "DontBotherMeWithSpam"; }
}
public override string Description
{
get { return "Provides retrieving/sending data from/to Clipboard"; }
}
}
/// <summary>
/// Cmdlet to return clipboard text to pipe
/// </summary>
/// <remarks>
/// Original Author: Keith Hill (http://spaces.msn.com/keithhill)
/// Original Source URL: http://home.comcast.net/~rkeithhill/MSH/Clipboard.cs
/// Modified by: DBMwS
/// Date: 01/31/2006 @ 12:59AM
///
/// Removed, "timeout" feature
/// </remarks>
[Cmdlet("Get", "Clipboard")]
public class GetClipboardCommand : Cmdlet
{
private Exception ex;
private string dataObject = "";
protected override void BeginProcessing()
{
Thread t = new Thread(new ThreadStart(GetObject));
try
{
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
}
catch (Exception ex)
{
ErrorRecord err = new ErrorRecord(ex, "GetClipboardError",
ErrorCategory.NotSpecified, t);
WriteError(err);
}
if (this.ex != null)
{
WriteError(new ErrorRecord(this.ex,
"GetClipboard::GetObjectError",
ErrorCategory.NotSpecified, null));
}
WriteVerbose(string.Format("Sending '{0}' to pipe",
this.dataObject.ToString()));
WriteObject(this.dataObject,
(this.dataObject.GetType().IsArray));
}
private void GetObject()
{
try
{
this.dataObject = Clipboard.GetText();
}
catch (Exception ex)
{
this.ex = ex;
}
}
}
/// <summary>
/// send output string to clipboard
/// </summary>
[Cmdlet("Out", "Clipboard")]
public class OutClipboardCommand : Cmdlet
{
/// <summary>
/// Unhandled exception
/// </summary>
private Exception ex;
/// <summary>
/// Actuall worker thread
/// </summary>
private Thread t;
/// <summary>
/// Holds all the necessary input data to send to Clipboard
/// </summary>
private StringBuilder sb = new StringBuilder();
/// <summary>
/// Input data received either from pipe or as an argument
/// </summary>
private string []inputData;
/// <summary>
/// Input data to send to clipboard
/// </summary>
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
public string []InputData
{
get { return inputData; }
set { inputData = value; }
}
/// <summary>
/// Create a STA thread to start later on.
/// </summary>
protected override void BeginProcessing()
{
WriteVerbose("Begin Processing(Creating a new STA thread)...");
t = new Thread(new ThreadStart(SetObject));
t.SetApartmentState(ApartmentState.STA);
}
/// <summary>
/// Append all input lines into one
/// (with a newline appended for each input data)
/// </summary>
protected override void ProcessRecord()
{
WriteVerbose("Processing Record...");
try
{
if (this.InputData != null)
{
WriteVerbose(
string.Format("\tAppending {0}", this.InputData));
foreach (string line in this.InputData)
{
this.sb.AppendLine(line);
}
}
}
catch (Exception ex)
{
// Well I don't know what happened,
// so send the error to the host...
ErrorRecord err = new ErrorRecord(ex, "OutClipboardError",
ErrorCategory.NotSpecified, t);
WriteError(err);
}
}
/// <summary>
/// Start a thread to send input data to Clipboard
/// </summary>
protected override void EndProcessing()
{
try
{
t.Start();
t.Join();
}
catch (Exception ex)
{
ErrorRecord err = new ErrorRecord(ex, "OutClipboardError",
ErrorCategory.NotSpecified, t);
WriteError(err);
}
// Handle error occurred while sending text to clipboard
// Well, try out something as stupid as "clear-host | out-clipboard"...
if (this.ex != null)
{
WriteError(new ErrorRecord(this.ex,
"OutClipboard::SetObjectError",
ErrorCategory.NotSpecified, null));
}
WriteVerbose("End Processing...");
}
/// <summary>
/// Send input data to clipboard
/// </summary>
private void SetObject()
{
try
{
// Before sending the inputData to clipboard,
// we need to drop the last newline character
string text = this.sb.ToString();
text = text.Substring(0,
(text.Length - System.Environment.NewLine.Length));
Clipboard.SetText(text);
}
catch (Exception ex)
{
this.ex = ex;
}
}
}
}
MSH>Out-Clipboard "Hello, World"
Begin Processing...
Processing Record...
Setting String
End Processing...
MSH>"Hello, World" | Out-Clipboard
Begin Processing...
Processing Record...
Setting MshObject
End Processing...
using System;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;
using System.Management.Automation;
using System.Collections.Generic;
using System.Text;
namespace snapins
{
[RunInstaller(true)]
public class OutClipboardSnapin : System.Management.Automation.MshSnapIn
{
public OutClipboardSnapin() : base()
{
}
public override string Name
{
get { return "OutClipboardSnapin"; }
}
public override string Vendor
{
get { return "DontBotherMeWithSpam"; }
}
public override string Description
{
get { return "This snapin receives piped output into Clipboard"; }
}
}
[Cmdlet("Out", "Clipboard")]
public class OutClipboardCommand : Cmdlet
{
private Thread t;
private object inputData;
/// <summary>
/// Input data to send to clipboard
/// </summary>
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline=true)]
public object InputData
{
get { return inputData; }
set { inputData = value; }
}
protected override void BeginProcessing()
{
t = new Thread(new ThreadStart(SetObject));
t.SetApartmentState(ApartmentState.STA);
WriteObject("Begin Processing...");
}
protected override void ProcessRecord()
{
WriteObject("Processing Record...");
WriteObject("Setting " + this.InputData.GetType().Name);
t.Start();
}
protected override void EndProcessing()
{
t.Join();
WriteObject("End Processing...");
}
private void SetObject()
{
if (this.InputData.GetType() == typeof(string))
{
Clipboard.SetText(this.InputData.ToString());
}
else
{
Clipboard.SetDataObject(this.InputData);
}
}
}
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
function using-library {
param($ns = $(throw "Enter Namespace to import"), [bool]$SuppressOutput = $false)
trap { throw "Could not import Namespace $($ns)" }
if ($SuppressOutput) {
[void][System.Reflection.Assembly]::LoadWithPartialName($ns)
} else {
[System.Reflection.Assembly]::LoadWithPartialName($ns)
}
}
MSH>Set-Alias using using-library
The default implementation for get-credential now uses the Windows CredUI
functionality for collecting credentials. This is a requirement for being
complaint with Windows Common Criteria. We still retain the console mode
which can be enabled by setting the registry key "ConsolePrompting" under
HLKM:\SOFTWARE\Microsoft\MSH\1\ShellIds to "True" (string)
MSH> SetConsoleCredPrompt
Your Credential Prompting Mode has been changed to "Console" Mode.
MSH> SetConsoleCredPrompt($false)
Your Credential Prompting Mode has been changed to "Gui" Mode.
# author: DBMwS
# date: 01/12/2005 @ 11:14 PM
# description: "Get-Credential" prompting method between "console" and "CredUI"
# note: Works only for Beta 3 of Monad
function SetConsoleCredPrompt() {
# default is $private:false since in Monad Beta 3, CredUI is the default way to
# prompt end-user for entering username and password
# But should i throw an Exception here or not... That's the problem...
param([bool] $ConsoleMode = $private:true)
# Ready for the unexpected...(such as running this script for non beta 3 version of Monad
trap { Write-Host "Some Unknown error has occurred, Aborting Mission..."; return; }
# MSH Beta 3 ShellID
$private:path = [string] "HKLM:\SOFTWARE\Microsoft\MSH\1\ShellIds"
# A new key name to create.
$private:key = [string] "ConsolePrompting"
# Check if the location we need to reach exists or not, else abort the script
if (test-path $private:path) {
set-property $private:path -Property $private:key -Type String -Value $ConsoleMode.ToString()
} else {
throw "Could not find Registry Path($($private:path)) to change Creditial Prompt Setting`nMaybe you are running pre-beta3 MSH?"
}
Write-host -n "Your Credential Prompting Mode has been changed to `""
if($ConsoleMode) { Write-host -n -f "red" "Console" } else { Write-Host -n -f "green" "Gui" }
Write-host "`" Mode."
}
# group them per user
"`n$Extension Per User"
$Userfiles | group Username | select @{expression={$_.Name};Name="User"},
@{expression={($_.group | measure-object).count};Name="Count"},
@{expression={($_.group | measure-object -property RoundedSize -sum).sum};Name="MB"}
User Count MB
---- ----- --
Usr001 141 998,52
Usr002 547 3746,62
$Userfiles | group Username | select @{e={$_.Name};N="User"},
@{e={($_.group | measure-object).count};N="Count"},
@{e={($_.group | measure-object -property RoundedSize -sum).sum};N="MB"}
# author: dance2die
# title: Ghetto SONY Rootkit Revealer~ :)
# date: 01/08/2005 @ 20:05
# comment: LOL, this is quite funny...
function RevealSONYRootkit {
# create a file(in a current dir)
# that starts with "$sys$" which is what SONY used to hide their files.
$private:testFile = ".\`$sys`$test.txt"
trap [System.IO.FileNotFoundException] {
# well something happend so i am guessing that the a rootkit is on the machine..
Write-Host -foregroundColor "red" -backgroundcolor "white" "You might have SONY rootkit installed... I am chickening out..."
if ([System.IO.File]::Exists($private:TestFile)) remove-item $private:testFile
break
}
If (![System.IO.File]::Exists($private:testFile)) {
# i am not using [void] here since new-item will display the file created on console... to make sure that people get to see the result
new-item -type file $private:testFile
} else {
Write-Host -foregroundColor "green" -backgroundcolor "white" "Your system is clean..."
# i don't usually like to have multiple exit points in a function but wth...
return
}
if ([System.IO.File]::Exists($private:testFile)) {
Write-Host -foregroundColor "green" -backgroundcolor "white" "Your system is clean..."
} else {
Write-Host -foregroundColor "red" -backgroundcolor "white" "FATAL:You have a SONY Rootkit installed!!!`nReinstall your Windows!!!"
}
remove-item $private:testFile
}
$strComputer = "."
$colItems = get-wmiobject -class "Win32_Process" -namespace "root\CIMV2" `
-computername $strComputer | write-object
foreach ($objItem in $colItems) {
write-host $objItem.Name, $objItem.WorkingSetSize -foregroundcolor "magenta"
}
MSH>get-process | foreach { write-host $_.name, $_.WorkingSet -foregroundcolor "magenta" }
Experimenting with a different format of blogs...