My 5th guest blog on Microsoft’s “Hey, Scripting Guy!” is out today .In this article I talk about using background jobs and scaling them to perform a monitored reboot of systems.
Link to the article is here.
Enjoy!
My 5th guest blog on Microsoft’s “Hey, Scripting Guy!” is out today .In this article I talk about using background jobs and scaling them to perform a monitored reboot of systems.
Link to the article is here.
Enjoy!
Yep, you guessed it! My appearance on the PowerScripting Podcast is available for download at the link below.
Link to site:
http://powerscripting.wordpress.com/2011/09/25/episode-160-boe-prox-on-poshpaig/
Direct link to audio:
http://traffic.libsyn.com/powerscripting/PSPodcast-160.mp3
Enjoy!
01010000 01101111 01110111 01100101 01110010 01010011 01101000 01100101 01101100 01101100 00100000 01010110 00110011 00100000 01010010 01101111 01100011 01101011 01110011 00100001
So what did I type here? Well, you can either copy this text and go find a binary-to-text converter online… Or you can use the Fun Text Converter that I wrote in PowerShell using ShowUI.
First thing is first, you need to download ShowUI and load up the module. You can download the latest version (1.3 at the time of this writing) from here. Once you have it downloaded and copied to your module folder, you can then import the module using the following command:
Import-Module ShowUI
Ok, now we are ready to go.
First thing I do is set up the window by using a hash table that will hold the parameters for the Window.
$Windowparam = @{
Width = 500
Height = 400
Title = 'Fun Text Converter'
Background = '#C4CBD8'
WindowStartupLocation = 'CenterScreen'
AsJob = $True
}
For those of you unfamiliar with this technique, it is called “Splatting” and it is very useful and looks cleaner when used in a command (as you will see soon).
Ok, lets take a look at our window with the given parameters:
New-Window @Windowparam -asjob
Ok, nothing too spectacular here, but you can see that the Window is being displayed just as I configured it to using splatting. All you have to do is run the command and add the hash table, in this case @Windowparam. Note that you do not use $windowparam.
Now lets add the rest of the components needed to make this UI work.
#Create Window
New-Window @Windowparam {
New-Grid -Rows *,Auto,*,Auto -Children {
New-TextBox -Row 0 -Name InputBox -TextWrapping Wrap -VerticalScrollBarVisibility Auto
New-Grid -Row 1 -Columns *,*,Auto,Auto,Auto,*,* -Children {
New-Button -Column 2 -Name ConvertButton -Width 65 -Height 25 -Content Translate -On_Click {
If ($ComboBox.Text -eq 'TextToBinary') {
$OutputBox.Text = Convert-TextToBinary $InputBox.Text
} ElseIf ($ComboBox.Text -eq 'BinaryToText') {
$OutputBox.Text = Convert-BinaryToText $InputBox.Text
} ElseIf ($ComboBox.Text -eq 'TextToHex') {
$OutputBox.Text = Convert-TextToHex $InputBox.Text
} ElseIf ($ComboBox.Text -eq 'HexToText') {
$OutputBox.Text = Convert-HexToText $InputBox.Text
} ElseIf ($ComboBox.Text -eq 'BinaryToHex') {
$OutputBox.Text = Convert-BinaryToHex $InputBox.Text
} ElseIf ($ComboBox.Text -eq 'HexToBinary') {
$OutputBox.Text = Convert-HexToBinary $InputBox.Text
} ElseIf ($ComboBox.Text -eq 'ReverseInput') {
$OutputBox.Text = Convert-TextToReverseText $InputBox.Text
}
}
New-Label -Column 3
New-ComboBox -Name ComboBox -Column 4 -IsReadOnly:$True -SelectedIndex 0 -Items {
New-TextBlock -Text TextToBinary
New-TextBlock -Text BinaryToText
New-TextBlock -Text TextToHex
New-TextBlock -Text HexToText
New-TextBlock -Text BinaryToHex
New-TextBlock -Text HexToBinary
New-TextBlock -Text ReverseInput
}
}
New-TextBox -Row 2 -Name OutputBox -IsReadOnly:$True -TextWrapping Wrap `
-VerticalScrollBarVisibility Auto
New-StackPanel -Row 3 -Orientation Horizontal {
New-Button -Name CopyTextButton -Width 65 -Height 25 -HorizontalAlignment Left -Content CopyText -On_Click {
$OutputBox.text | clip
}
New-Label
New-Button -Name ClearTextButton -Width 65 -Height 25 -HorizontalAlignment Left -Content ClearText -On_Click {
$OutputBox.Text=$Null
}
}
}
}
Ok, now I have a nice UI that includes 3 buttons, 2 textboxes and a combo box. By not supplying a height and width for each of the textboxes, they will automatically adjust their size when you re-size the window.
Now that I have the front-end stuff done, it is time to knock out the backend things to make this converter work like I want it to. You may have noticed the functions that were being called when you click on the Translate button. Well, that is the next part of this little piece. Since I am running the UI as a job, I opted to include the functions when the Window is loaded. This is done by adding the –On_Loaded parameter.
I decided that I wanted to accomplish the following types of conversions in this UI. They include:
Using the following functions, I was able to accomplish each of these items:
Edit: Changed [string]::Join() to –Join. Thanks Robert (Twitter | Blog) for keeping me honest!
Function Convert-TextToBinary {
[cmdletbinding()]
Param (
[parameter(ValueFromPipeLine='True')]
[string]$Text
)
Begin {
#Create binary empty collection
[string[]]$BinaryArray = @()
}
Process {
#Convert text to array
$textarray = $text.ToCharArray()
#Convert each item to binary
ForEach ($a in $textarray) {
$BinaryArray += ([convert]::ToString([int][char]$a,2)).PadLeft(8,"0")
}
}
End {
#Write out binary string
$BinaryArray -Join " "
}
}
Function Convert-BinaryToText {
[cmdletbinding()]
Param (
[parameter(ValueFromPipeLine='True')]
[string]$Binary
)
Begin {
#Create binary empty collection
[string[]]$TextArray = @()
}
Process {
#Split Binary string into array
$BinaryArray = $Binary -split "\s"
#Convert each item to Char
ForEach ($a in $BinaryArray) {
$TextArray += [char]([convert]::ToInt64($a,2))
}
}
End {
#Write out text string
$TextArray -Join ""
}
}
Function Convert-TextToHex {
[cmdletbinding()]
Param (
[parameter(ValueFromPipeLine='True')]
[string]$Text
)
Begin {
#Create hex empty collection
[string[]]$HexArray = @()
}
Process {
#Convert text to array
$textarr = $text.ToCharArray()
#Convert each item to binary
ForEach ($a in $textarr) {
$HexArray += "0x$(([convert]::ToString([int][char]$a,16)).PadLeft(8,'0'))"
}
}
End {
#Write out hex string
$HexArray -Join " "
}
}
Function Convert-HexToText {
[cmdletbinding()]
Param (
[parameter(ValueFromPipeLine='True')]
[string]$Hex
)
Begin {
#Create text empty collection
[string[]]$textarr = @()
}
Process {
#Split Binary string into array
$HexArray = $Hex -split "\s"
#Convert each item to Char
ForEach ($a in $HexArray) {
$textarr += [char]([convert]::ToInt64($a.TrimStart('x0'),16))
}
}
End {
#Write out text string
$textarr -join ""
}
}
Function Convert-HexToBinary {
[cmdletbinding()]
Param (
[parameter(ValueFromPipeLine='True')]
[string]$Hex
)
Begin {
#Create binary empty collection
[string[]]$binarr = @()
}
Process {
#Split Binary string into array
$HexArray = $Hex -split "\s"
#Convert each item to Char
ForEach ($a in $HexArray) {
$a = ([char]([convert]::ToInt64($a.TrimStart('x0'),16)))
$binarr += ([convert]::ToString([int][char]$a,2)).PadLeft(8,"0")
}
}
End {
#Write out binary string
$binarr -join " "
}
}
Function Convert-BinaryToHex {
[cmdletbinding()]
Param (
[parameter(ValueFromPipeLine='True')]
[string]$Binary
)
Begin {
#Create binary empty collection
[string[]]$TextArray = @()
}
Process {
#Split Binary string into array
$BinaryArray = $Binary -split "\s"
#Convert each item to Char
ForEach ($a in $BinaryArray) {
$a = [char]([convert]::ToInt64($a,2))
$TextArray += "0x$(([convert]::ToString([int][char]$a,16)).PadLeft(8,'0'))"
}
}
End {
#Write out hex string
$TextArray -Join " "
}
}
Function Convert-TextToReverseText {
[cmdletbinding()]
Param (
[parameter(ValueFromPipeLine='True')]
[string]$InputString
)
Begin {
}
Process {
#Convert text to array
$inputarray = $InputString -split ""
#Reverse array
[array]::Reverse($inputarray)
}
End {
#Write out reverse string
$inputarray -join ""
}
}
Putting this altogether gives us a fully functional text converter.
.\ShowUI_TextConverter.ps1
So… Lets find out what it was that I posted earlier on in this article:
That is right….PowerShell V3 Rocks!
Using the converter is pretty simple. Type or paste text or whatever into the top window and then use the combobox in the middle right of the UI to select the type of conversion to use and the click on Translate to start the conversion. The output of the converted is displayed in the bottom window. This window is read-only and you can copy the text by highlighting it….or you can use the CopyText button to copy it for you. Clicking on ClearText will only clear the output window when clicked.
Let’s see a couple more conversions:
(remove .doc extension)
A few more things I wanted to touch on about PowerShell V3 before calling it a night:
Finally, we have Intellisense in the ISE! This has been one of the most wanted things (for me at least) that was missing in the original ISE.
You can also see a new Command pane on the right hand side that lets you pick commands and run them. Another cool feature brought to use by the PowerShell team!
One last thing that has been brought up by other members of the community is the need to set the $psISE.Options.UseEnterToSelectInCommandPaneIntellisense to $True so it is easier to work with Intellisense in the command pane.
$psISE.Options.UseEnterToSelectInCommandPaneIntellisense = $True
Hopefully this gets changed to being the default by the time the production version of PowerShell V3 is out.
Code folding is also included free of charge, which really helps out when you’re working on a large script and need to lessen the view to just the area that you need to use. With code folding, you can minimize (hide) a section of code while you are working on a different section. Very nice feature to include!
As it says above, you no longer need to use the curly brackets when filtering with Where-Object or looping with ForEach. Another nice thing is you no longer need to use the $_. variable for filtering. Just use the property name.
Get-Process | Where Name -like 'ie*' | ForEach Stop-Process -Whatif
Something else that is pretty cool is you can now filter for Directories, Files, various file/folder attributes (Read,System,Hidden,Archive) using Get-ChildItem which saves the time of using Where-Object to make this happen.
Available switches:
-Directory
-File
-Attribute (Can use other attributes like Archive)
-System
-Hidden
-ReadOnly
Remember my article about building a function to download web site information? Well, kiss that technique goodbye because the PowerShell team has brought in Invoke-WebRequest to make life a lot easier for you. Here is a quick example to pull an RSS feed from the Hey, ScriptingGuy! blog.
([xml](Invoke-WebRequest http://blogs.technet.com/b/heyscriptingguy/rss.aspx).content).rss.channel.item | Select Title, Link
So easy it is just craziness!
After you download and install PowerShell V3, be sure to check out the stuff in the zip file. There is a treasure trove of information and free samples that you can check out and explore with.
Just one place to check out:
WMF3-CTP1-x64\Samples\WindowsPowerShell
I’m looking forward to checking out some of the workflow stuff in there as well and see what kind of stuff I can do with that. There just so many things that it would be impossible to list here. But you can keep with stuff via Twitter by using the #powershell and #poshv3 hash tags. Also, many more members of the PowerShell community are blogging it up with all of the new features as well.
You heard it! PowerShell V3 CTP 1 is out in the wild and ready for downloaded right here!
Bear in mind that this is only for Windows 7 and Windows 2008R2. It does require a reboot to complete the installation but on a good note, you do not have to uninstall anything to get this working. Also required is .NET 4.0 as well.
A couple of great things that I have found now that I can play with this on a Windows 7 machine is that Export-CSV has a –Append parameter!
This means we can finally add to an existing csv file as long as the headers are the same.
Get-Process | Export-Csv report.csv -NoTypeInformation Get-Process | Export-Csv report.csv -NoTypeInformation -append
Great stuff! Now what happens if we try to use the –append with mis-matched properties?
Get-Process | Export-Csv report.csv -NoTypeInformation Get-Service | Export-Csv report.csv -NoTypeInformation -Append
You can use the –Force parameter, but understand that you are pretty much ruining the csv for future use.
Get-Process | Export-Csv report.csv -NoTypeInformation Get-Service | Export-Csv report.csv -NoTypeInformation -append -force Import-Csv report.csv
Yea, not so pretty, is it?
So we have a very useful addition to an already great cmdlet, Export-Csv!
So what are you waiting for? Download PowerShell V3 and get to exploring because there is soooo much more to find!