Archive for the ‘Powershell’ Category
Reign over your network with only a Kindle?
Although any kind of advertisement is not the intent I am writing on these pages, the latest buzz motivated me too to drop a word on web powershell.
Have you ever thought about how to manage your network(s) laying languidly upon the beach, tanning and using the worldwide free Internet access? I have. Now I’ll be showing the facts and you the reader is the judge if it is possible to administer maximally remotely from a resort or not yet.
The check list of preparations is of three items (with exception of course sandals, towels and so forth):
- a Kindle connected to a some network (Wi-Fi or GSM 3G is what is needed) from $114
- a Mobile Shell box – from one 3G KIndle or two Wi-Fi special offer’s Kindles apiece
- some scripts, especially changing security settings or touching the file system your boss’s host, to make the life breathtakingly sharper.
At first, buy and register a Kindle and download your copy of MobileShell here. While the latter can be downloaded on a monthly trial basis, the former needs defraying (you may try to use Amazon Return policy, though. If your test failed only). Go throught the text below if you have already checked all items above.
Install the software as said here (won’t you read the manual going to a beach?) and out, out to the beach. Any tests on a Kindle can be performed from there, for what else reason you sponsored the development of Pearl screens buying it?
Let’s connect to the MobileShell host. The only note here is that you need use strictly the mobile version by typing https://host_name_or_address/MobileShell/mobile , otherwise the Kindle bravely tries to swallow the desktop version, its browser got swollen and died until the device restarts.
After Kindle said you your rights, you are connected and may or may not see the choice if somebody knowing your login and password, you for example, was last night here:
We are leaving out the Favorites list
since we wanted to run a script or a piece of code. Probably unlike you, I forgot to write scripts and will run something useless. Meet the Kindle PowerShell Code Editor:
Of two colors, black and white, isn’t it? The very conservative design, easier than ‘vi’, though.
Some English IntelliSense helped us and our script is ready!
The first run is in a second, take a sip of what you drink on the sand and press the Run button!
Oops, the folder where I stored screenshots is where I’m sitting and I’m sitting not on the MobileShell host. But it would have found the folder if I’d written right, beyond the doubts.
Kindle for PowerShellers. Part 1. Notes and Highlighting
This post is the first one of the small series regarding how to use a Kindle to be deeply involved in PowerShell. All the written in most cases are applicable to any technology or language, or environment, but since the theme of this blog is PowerShell, so that the series is named ‘Kindle for PowerShellers’.
The first idea to share is how to have with you your thoughts that came at the time you’ve been reading something PowerShell-ing. Kindle is a great instrument to do that. When I’m reading I often use highlighting to spare ideas, phrases, code samples and all I’ve found noticeable.
Since the version 3.1 got available on February, Amazon supports public highlighting. What’s it? If you wish, you might not only save your selections, but make them public by pressing Alt + Enter. The figure below demonstrates highlights done by the Kindle owner:
The following highlight is ready to be published:
At last, this one is how you can see others’ highlights:
The result of selecting the View popular Highlights menu item as well as what you see during the reading is a one or more underlined rows and the number of users which highlighted the excerpt.
In addition, there is a way to say something about you’ve read a moment ago by adding a note (I don’t like typing on a Kindle :), but somebody possibly does). Thus, there is also a way to see what others wrote by selecting the View Notes & Marks menu item. It brings to you not only your bookmarks, highlights and notes, but also others’.
The second amazing thing is that you might access all of these almost everywhere. Kindle for most popular platforms can be downloaded here.
That means that you are not obliged to buy a hardware Kindle (I sincerely advise to if you love reading comfortable for eyes and a half of pound is not a weight for you), instead you may use, for example, Kindle for PC (but not with every corporate proxy, though).
The last but not least, you may access your notes as well as other readers by using an Internet connection. To perform this, go to the Kindle page, log in and navigate to or search for books you are interesting in. The Your books section is a starting point for that. All your notes are available in a browser window! The direct link to the full collection is https://kindle.amazon.com/your_highlights .

From my point of view, the web access to your or other readers’ notes is the best way if you want to conspect the book or remember key points.
Also, there is the possibility to publish your notes and highlights in case that they are private yet. Not being a native speaker, I highlight much of the book (all the phrases I found sound or interesting) so that I almost never share mine:
The pleasant thing here is that you may add new notes at the web page, using your favorite full-size desktop keyboard or notebook’s one instead of Kindle keyboard approximately of a mobile phone size.
Having trouble with your head’s RAM? There is the Daily Review flashcards helping you scoop up the knowledge from the book currently in reading.
Finally, the experimental Kindle for the Web is available on this remarkable page – with this tool you are given the opportunity to eye into the books you are only licking your lips at:
Bringing this long panegyric to an end, can I omit the Sync to Furthest Page Read menu item? I can’t, this does the reading on several devices and desktops even more comfortable. Have I forgotten to say that with any Kindle application you are given (especially if you like me are not trueborn English speaker) with two Oxford English-to-English dictionaries and one Duden Wörterbuch as well?
Many thanks to the books starring in this post: 🙂
Mortal combat: Coloring Masters Cup 1/2 final
Who is the winner? Give the answer at last, may ask the intrigued reader. Well, the testing is almost over and soon comes the final. Two fightings were conducted, the honesty and independence is kept by using the same test script.
The first is between PowerGUI 2.4 (Copy as HTML) and IsePack of PowerShellPack:
https://powershelldevtools.wordpress.com/2011/05/06/test-code-2-2/
Next, this is a note that IsePack of PowerShellPack works well with multi-string here-strings:
https://powershelldevtools.wordpress.com/2011/05/06/test-code-3-isepack-vs-here-string/
What? It spoils multi-string here-strings too? This is obviously the result of sequential savings of the post. The WordPress’s bug most likely.
At last, the bout between twins, SparkPlug for PowerGUI and SparkPlug for ISE:
The twins are equally strong, but SparkPlug for PowerGUI was declared the winner on points due to the real mess-up in the ISE menu if you have previously installed and run IsePack.
All the post protected with the password 123 so as not to harm too young and too impressive powershellers, and to preserve the Google Environment from duplicated search spam too.
We have a leader! Currently the strongest fighter of all the desktop applications and the common candidate is SparkPlug of Script-Automating!
What’s about the final? I’d expect that the delegate of the WordPress web-services (grown up on the Internet users’ needs, please note) are trying and be fit to fight sooner or later. Really couln’t help waiting, the simplicity of copy-paste and sourcecode tag and the ability to highlight row against the embedded desktop heavyweight is tickling my nerves.
In response to the comment about using IsePack
A recommendation for using PowerShellPack’s IsePack Copy-ColoredAsHtml has been made in the comment to my post. I’m not a big fan of this huge package, but this is worth testing for publishing.
I use the same code copy-pasted from an ISE tab:
cls
#region WordPress posting code test
#this is a test of Powershell code coloring
[string]$stringVar1 = "string 1";
[string]$private:stringVar2 = 'string 2';
[string]$script:stringVar3=
@'
string data
'@
[scriptblock]$global:sb = {{Write-Host scriptblock}.Invoke();};
function
write1{Write-Host $stringVar1;}
function private:write2
{param([string]$str2 = '')
Write-Host $str2;}
function script:write3
#this is a function
{
Write-Host $script:stringVar3;
}
function global:Print-SB
{
<#
.SYNOPSIS
This is a code coloring test.
.DESCRIPTION
This test function represents an advanced Powershell function syntax.
.PARAMETER Param
Demonstrates how a scriptblock can be passed as a reference.
.EXAMPLE
PS C:\> Print-SB ([ref]$sb)
#>
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true)]
[ref]$Param
)
Begin{}
Process{$Param.Value.Invoke()}
End{}
}
write1
private:write2 $private:stringVar2;
script:write3
Print-SB ([ref]$global:sb)
#endregion WordPress posting code test
Below is the result of IsePack code preparation:
cls #region WordPress posting code test #this is a test of Powershell code coloring [string]$stringVar1 = "string 1"; [string]$private:stringVar2 = 'string 2'; [string]$script:stringVar3= @' string data '@ [scriptblock]$global:sb = {{Write-Host scriptblock}.Invoke();}; function write1{Write-Host $stringVar1;} function private:write2 {param([string]$str2 = '') Write-Host $str2;} function script:write3 #this is a function { Write-Host $script:stringVar3; } function global:Print-SB { Print-SB ([ref]$sb) #> [CmdletBinding()] param( [Parameter(Position=0, Mandatory=$true)] [ref]$Param ) Begin{} Process{$Param.Value.Invoke()} End{} } write1 private:write2 $private:stringVar2; script:write3 Print-SB ([ref]$global:sb) #endregion WordPress posting code test
This looks great and very similar to the hand-made colored code from my post. But who has stolen my advanced function’s comment?! 🙂 All the text between <# and C:\> inclusively erased as a cow licked out.
A hand-made script-to-form converter (sooner a sample than a tool)
With eternal love to automation, I began today a small project aiming to help creation of PowerShell GUI forms. This post is the first step, will or won’t I do the next is not clearly even to me.
A year ago I wrote something like this. It was a PropertiesBrowser module/add-on, which I used as the Watches window, typical of any IDE, sometimes. It hasn’t been published, though.
Having today morning an hour or slightly more, I decided to write a post mostly to test the new smartbook I own the third day, but at the same time I caught what I want. A simple form generator.
How should it work? Almost every script produces an output. Strings, numbers, boolean, objects, all what may return a PowerShell code. Why don’t allow the scripter to put it on a form without an effort? Sound like a GUI modeling tool (but looks much more uglyer).
Here it is, the code giving some output and used as a sample:
cls
Set-StrictMode -Version Latest
Import-Module FormGen -Force
[int]$i = 3;
Set-OutputToForm $i
Set-OutputToForm $i -SuppressLabel $true
Set-OutputToForm $i -Description "int"
$f = New-Object System.Windows.Forms.Form
Set-OutputToForm $f;
$a = @("s", "T");
Set-OutputToForm $a $false "Array"
Get-ChildItem | Select-Object -First 3 | %{Set-OutputToForm $_ -Description "fileinfo of $($_.FullName)";}
1..2 | %{Set-OutputToForm $_;}
Get-ChildItem | Select-Object -First 1 | %{Set-OutputToForm $_ -Description "fileinfo of $($_.FullName)" -ControlType 'System.Windows.Forms.ListView';}
Get-ChildItem | Select-Object -First 1 | %{Set-OutputToForm $_ -Description "fileinfo of $($_.FullName)" -ControlType 'System.Windows.Forms.TreeView';}
Get-ChildItem | Select-Object -First 1 | %{Set-OutputToForm $_ -Description "fileinfo of $($_.FullName)" -ControlType 'System.Windows.Forms.ComboBox';}
Show-Result;
As can be seen, there is the FormGen module and two functions exported from it, Set-OutputToForm and Show-Result. While the former adds data to a form still invisible, the latter, to be used once, displays the form.
Set-StrictMode -Version Latest
[int]$script:topPosition = 0;
[System.Windows.Forms.Form]$script:frmMain = `
New-Object System.Windows.Forms.Form;
$script:frmMain.Width = 500;
function Set-OutputToForm
{
Set-OutputToForm $data
PS> 1..10 | %{Set-OutputToForm $_;}
PS> Get-ChildItem | %{Set-OutputToForm $_ -SuppressLabel $true; $null;}
.Notes
Author: Alexander Petrovskiy
#>
[CmdletBinding()]
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
$InputObject,
[bool]$SuppressLabel = $false,
[string]$Description = "",
[string]$ControlType = ""
)
Begin{}
Process{
[bool]$result = $false;
try{
if (-not $SuppressLabel)
{
[System.Windows.Forms.Label]$private:label = `
New-Object System.Windows.Forms.Label;
$script:topPosition += 10;
$private:label.Top = $script:topPosition;
$private:label.Left = 50;
$private:label.Width = $script:frmMain.Width - 50;
$private:label.Visible = $true;
if ($Description.Length -gt 0)
{
$private:label.Text = $Description;
}
else
{
$private:label.Text = $InputObject.GetType().ToString();
}
$script:frmMain.Controls.Add($private:label);
$script:topPosition += $private:label.Height;
}
[bool]$private:useCustomControl = $false;
[System.Windows.Forms.Control]$private:ctrl = `
New-Object System.Windows.Forms.Control;
if ($ControlType.Length -gt 0)
{
try{[System.Windows.Forms.Control]$private:ctrl =
New-Object $ControlType;
$private:useCustomControl = $true;
}catch{$private:useCustomControl = $false;}
}
#of which type these data?
#an array?
if ($InputObject -is [System.Array])
{
if (-not $private:useCustomControl){
[System.Windows.Forms.ListBox]$private:ctrl = `
New-Object System.Windows.Forms.ListBox;}
for($private:i = 0; $private:i -lt $InputObject.Length;
$private:i++)
{
if ($private:ctrl -is [System.Windows.Forms.ListBox] -or `
$private:ctrl -is [System.Windows.Forms.ComboBox] -or `
$private:ctrl -is [System.Windows.Forms.ListView]){
$private:ctrl.Items.Add($InputObject[$private:i]);}
if ($private:ctrl -is [System.Windows.Forms.TreeView]){
$private:ctrl.Nodes.Add($InputObject[$private:i]);}
}
}
#a string? int one? other simple type?
elseif ($InputObject.GetType().BaseType.Name -eq 'ValueType')
{
if (-not $private:useCustomControl){
[System.Windows.Forms.Label]$private:ctrl = `
New-Object System.Windows.Forms.Label;}
if ($private:ctrl -is [System.Windows.Forms.ListBox] -or `
$private:ctrl -is [System.Windows.Forms.ComboBox] -or `
$private:ctrl -is [System.Windows.Forms.ListView]){
$private:ctrl.Items.Add($InputObject.ToString());}
if ($private:ctrl -is [System.Windows.Forms.TreeView]){
$private:ctrl.Nodes.Add($InputObject.ToString());}
if ($private:ctrl -is [System.Windows.Forms.Label] -or `
$private:ctrl -is [System.Windows.Forms.TextBox]){
$private:ctrl.Text = $InputObject.ToString();}
}
else
{
if (-not $private:useCustomControl){
[System.Windows.Forms.PropertyGrid]$private:ctrl = `
New-Object System.Windows.Forms.PropertyGrid;}
if ($private:ctrl -is [System.Windows.Forms.PropertyGrid]){
$private:ctrl.SelectedObject = $InputObject;}
if ($private:ctrl -is [System.Windows.Forms.ListBox] -or `
$private:ctrl -is [System.Windows.Forms.ComboBox] -or `
$private:ctrl -is [System.Windows.Forms.ListView]){
$private:ctrl.Items.Add($InputObject.ToString());}
if ($private:ctrl -is [System.Windows.Forms.TreeView]){
$private:ctrl.Nodes.Add($InputObject.ToString());}
}
#common properties
$private:ctrl.Left = 50;
$private:ctrl.Top = $script:topPosition;
$private:ctrl.Visible = $true;
$script:frmMain.Controls.Add($private:ctrl);
$script:topPosition += $private:ctrl.Height;
}catch{}
$result = $true;
return $result;}
End{}
}
function Show-Result
{
$script:frmMain.Height = $script:topPosition + 40;
$script:frmMain.ShowDialog() | Out-Null;
}
Export-ModuleMember -Function Set-OutputToForm, Show-Result;
Need to repeat, this is sooner a concept than a ready-to-use tool, so that use it as is. Probably, I’ll update it. The current version attached here.
Although it’s not an advantage for powershellers, I’m proud that at least a half of this code was written on my Android smartbook. This means that I can write from more places than I could before. I have had for two years a netbook, but without 3G and its keyboard was damaged on travel. Gladly, now I can write on the go again, with the actually mobile device.



