Saturday, January 16, 2010

Rudimentary PowerShell wrapper (CMD.exe replacement)

PowerShell rocks, but CMD.exe sucks is too limited for my tastes. I'm going to try making a wrapper/CMD.exe replacement for PowerShell. And what better language to write it in than PowerShell script itself? I'm calling it "IggyPosh" for now (POwer SHell).

Here's an early draft. No prompt or command history, but you can type commands and they are executed, so it's a start.


function test($str) {
$str = $rs.CreatePipeline($str).Invoke() | Out-String
$history.Text += $str -replace "\s+$([Environment]::NewLine)",[Environment]::NewLine
$history.SelectionStart = $history.Text.Length
$history.ScrollToCaret()
}

# Prepare the window
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form = New-Object Windows.Forms.Form
$form.Text = "IggyPosh"
$form.Size = New-Object Drawing.Point 600,400

$history = New-Object Windows.Forms.TextBox
$history.Multiline = $true
$history.ReadOnly = $true
$history.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical
$history.Dock = [System.Windows.Forms.DockStyle]::Fill
$history.BackColor = [System.Drawing.Color]::FromArgb(0,64,0)
$history.ForeColor = [System.Drawing.Color]::FromArgb(224,224,224)
$history.Font = New-Object System.Drawing.Font("Consolas", 9)
$form.Controls.Add($history)

$command = New-Object Windows.Forms.TextBox
$command.Dock = [System.Windows.Forms.DockStyle]::Bottom
$command.add_KeyDown({
if ($_.KeyCode -eq "Enter") { test($command.Text); $command.Text="" }
})
$form.Controls.Add($command)

# Try to init a powershell instance we can talk to
$rs = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
$rs.Open()

$form.ShowDialog()

No comments: