PowerShell In GUI Blog

PowerShell GUI Box Just In A Few Clicks

Posts Tagged ‘PowerShell

Reign over your network with only a Kindle?

with 2 comments

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.

Written by Alexander Petrovskiy

May 19, 2011 at 8:49 pm

An Infinitesimal Update To ObjectBrowser

leave a comment »

Hi, today’s update is really small.

– added a progress bar during the collecting Current AppDomain and GAC. This should do the GAC loading slightly less painful.

– the ability to be run from command line powershell as well as from ISE is now restored. Please use the following instruction to import the module:

Import-Module Add-on.PSDevStudioLite.ObjectBrowser -Force

Other changes relate to internal code structure and not to be seen right now in the GUI. As usual, the download location is here: http://www.box.net/shared/ot56ct7ngl

Preventing Problems In Your PowerShell Code

leave a comment »

Last month a wave of using the Set-StrictMode commandlet flooded the PowerShell Internet landscape.

In receiving occasionally a tweet from powershell.com about using it with the -Version Latest parameter, I re-wrote several my old modules, three plus lines each, to meet the requirement. Needless to say that it helped to fix some deep mistakes in code, causing to reduction of code execution. These modules collect information, so that their execution don’t stop, but the data collected are not complete.

The topmost problem was the use of CSharp-like variable checks:

if ($Variable -ne $null){

Owing to a habit of using such a style, I left intact these checks, only outermost try-catch statements were added:

try{$null = $Variable;
if ($Variable -ne $null){

}}catch{}

where catch instruction empty or contains the error-handling function, just depending on a situation. Especialy, if what is meant to be $Variable is something like $Variable.Property.

Bringing the first story to an end, I need to say that my style of error-handling is formed mostly on recommendations given in John Robbins’s book about .NET debugging, the second edition if I recall it right. He insisted on testing return values and input parameters rather than on generating exceptions, so that I picked up this style somewhere in this millennium.

The example above is typical of such an approach: try statement is used as a protection level 1 from error thrown by Set-StrictMode, at the same time, the if statement defends the furthermost code from a useless value of a variable, forming level 2 of protection. In common, it works well, but weights heavy.

Not so much time went out since I finished tayloring mycode to support the Set-StrictMode cmdlet, here came a new issue I must investigate: http://www.windowsitpro.com/blogs/powershell-with-a-purpose/entryid/76307/is-there-an-option-explicit-in-powershell

Don warned on scopes. Personally, someday I had a problem like:

for($i = 0; $i -lt 10; $i++)
{
    #some code
    for($i = 0; $i -lt 10; $i++)
    {
        #some code
    }
}

In this example, the index variable omits several states as a result of being increment in the inner cycle:

for($i = 0; $i -lt 10; $i++)
{
    Write-Host $i;
    for($i = 0; $i -lt 10; $i++)
    {
        Write-Host $i;
    }
}

0

0

1

2

3

4

5

6

7

8

9

The following sample code does the same:

for($private:i = 0; $private:i -lt 10; $private:i++)
{
    Write-Host $private:i;
    for($private:i = 0; $private:i -lt 10; $private:i++)
    {
        Write-Host $private:i;
    }
}

To heal the situation, you need use as many unique names of variables as you can afford. I suppose, you might have tens of cycles in your code. The following example is that you need to cure the preceding one:

for($private:i = 0; $private:i -lt 10; $private:i++)
{
    Write-Host $private:i;
    for($private:i2 = 0; $private:i2 -lt 10; $private:i2++)
    {
        Write-Host $private:i2;
    }
}

There may be offered a dubious solution, that some people used in VBScript programming: to declare ALL the script variables at a begin of every script/module file.

The argument that there is a great memory consumption is not serious due to a usual great memory consumption of many PowerShell scripts callingmodules and gathering some data.

Following the previously mentioned, the protected code is something like:

[int]$private:i = 0;
[int]$private:i2 = 0;
#
#amount of code
#
for($private:i = 0; $private:i -lt 10; $private:i++)
{
    Write-Host $private:i;
    for($private:i2 = 0; $private:i2 -lt 10; $private:i2++)
    {
        Write-Host $private:i2;
    }
}

Along with this ‘scope problem’, there’s another scope problem, namely ’embedded variable problem’. Notice, that variables $_, $Error and several other might contain various values at the execution time.

I saw the code where these variables worked differently on a module load and in time module functions are called. This was because of using Import-Module with the -Force parameter. Global value of $_ can be easily spoilt, so can be spoilt $script:_, but this is observed not so frequently.

The last example of today’s article demonstrates how we can spoil the $global:_ variable. It’s supposed that you have a couple of module *.psm1 files in paths stored in the $PSMOdulePath variable. If you does, let’s see how values of $global:_ and $script:_ are changed:

cls
[int]$counter = 0;
$env:PSModulePath.Split(";") |  %{Get-ChildItem -Path $_ -Include *.psm1 -Recurse | %{Write-Host '1' $_;
        $counter++;
        if ($counter -eq 1)
        {

$module = "${env:temp}\test.psm1"
@'
function Out-Items
{    param([string]$Comment)
    [string]$private:regPath = `
        'Registry::HKCU\Keyboard Layout';
    Get-ChildItem $private:regPath | `
        Select-Object -First 1 | `
        %{Write-Host 'Registry' $Comment '$_ :' $_;
          Write-Host 'Registry' $Comment '$private:_ :' $private:_;
          Write-Host 'Registry' $Comment '$local:_ :' $local:_;
          Write-Host 'Registry' $Comment '$script:_ :' $script:_;
          Write-Host 'Registry' $Comment '$global:_ :' $global:_;};
}
Out-Items 'on loading the module';
Export-ModuleMember -Function Out-Items
'@ > $module

Import-Module $module -Force;            

        }
        Write-Host '2' $_;
        }}

Out-Items "called from the console";

The salvation is in using bracketing with {}.GetNewclosure() method as shown below:

cls
[int]$counter = 0;
$env:PSModulePath.Split(";") |  %{Get-ChildItem -Path $_ -Include *.psm1 -Recurse | %{Write-Host '1' $_;
        $counter++;
        if ($counter -eq 1)
        {

$module = "${env:temp}\test.psm1"
@'
function Out-Items
{    param([string]$Comment)
    [string]$private:regPath = `
        'Registry::HKCU\Keyboard Layout';
    Get-ChildItem $private:regPath | `
        Select-Object -First 1 | `
        %{Write-Host 'Registry' $Comment '$_ :' $_;
          Write-Host 'Registry' $Comment '$private:_ :' $private:_;
          Write-Host 'Registry' $Comment '$local:_ :' $local:_;
          Write-Host 'Registry' $Comment '$script:_ :' $script:_;
          Write-Host 'Registry' $Comment '$global:_ :' $global:_;}.GetNewclosure();
}
Out-Items 'on loading the module';
Export-ModuleMember -Function Out-Items
'@ > $module

Import-Module $module -Force;            

        }
        Write-Host '2' $_;
        }}

Out-Items "called from the console";


Written by Alexander Petrovskiy

April 13, 2011 at 5:17 pm

ObjectBrowser has migrated to Set-StrictMode successfully

leave a comment »

Today, as I sincerely hope, I fixed the last but major bug stemmed from moving to the use of the Set-StrictMode statement. This bug prevented ObjectBrowser GUI from displaying properties, events and fields of classes. A major one, need to admit.

As a result, list of signatures of methods became a simple list of method names, so that all clas members now can be sorted by name, member type (i.e. property, method, etc), and a return value or property, saying about methods and fields.

Also, types of event handlers are shown after this fix.

To conclude, you might now instead of typing something like

[System.Windows.Forms.Form]$frm = 
    New-Object System.Windows.Forms.Form;
#never run it by Ctrl+F7 
#and never expand 
#the $frm variable in the Variables frame

simply navigate to the System.Windows.Forms.dll assembly, through the System.Windows.Forms namespace, after that click on the Form class and get the full list of its members.

Written by Alexander Petrovskiy

March 30, 2011 at 6:55 pm

Which Icons Are Shipped With Object Browser?

leave a comment »

Actually, what is the easiest way to display icons? Create a control like ListView? Maybe. Let’s create a handful of windows, just to observe how short the code might be.

First, import both modules, SuiteCore and ObjectBrowser (here and thereafter the PSDevStudioLite modules set is in use).

After that you are able to use the function set shipped with the package. The following simple code creates windows, which in turn demostrate icons.

cls
function createWindows
{
    for($private:i = 0; $private:i -lt $imagesList.Count; $private:i++)
    {
        $tw2 = New-SEToolWindow -Control (([System.Windows.Forms.ListBox]$lb = New-Object System.Windows.Forms.ListBox) | %{$lb;}) `
            -TitleBarText ("aaa" + $private:i) `
            -WindowName ("aaaaa" + $private:i) `
            -Image $imagesList[$imagesList.Keys[$private:i]] `
            -Visible $false -State TabbedDocument;
        Write-Host $private:i $imagesList.Keys[$private:i]
        $se.ToolWindows[("aaaaa" + $private:i)].Visible = $true;
    }
}

createWindows;

In the GUI you might easily see all of them by clicking a triangle, allowing you to make a list of all open documents:

To clean up the document list is also simply, even somewhat simpler that to fill up:

function removeWindows
{
for($private:i = 0; $private:i -lt $imagesList.Count; $private:i++)
{
Remove-SEToolWindow -WindowName ("aaaaa" + $private:i);
}
}

removeWindows;

 

Requirements: PSDevStudioLite.SuiteCore 1.0.0.4 Beta and higher, PSDevStudioLite.ObjetBrowser 1.0.0.4 Beta ang higher.

http://www.box.net/shared/ot56ct7ngl

Note: all above are never be supported by quest, wasn’t and won’t be.

OB 1.0.0.3

leave a comment »

Object Browser 1.0.0.3 is uploaded to the link http://www.box.net/shared/ot56ct7ngl

This version comprises of two small improvements. The first one is a moving to be used in Strict mode. The second, and resulting the first one, is an improvement in showing private members.

Written by Alexander Petrovskiy

March 21, 2011 at 7:41 pm

OB 1.0.0.2

leave a comment »

As I languidly tested what was released recently as ObjectBrowser 1.0.0.1 July, 12th 2010, I remembered a couple of bugx I always wanted to fix. Today’s 1.0.0.2 shipped with

– search by name

– PleaseWait window

both fixed. As usual, the direct link is the same: ObjectBrowser

Written by Alexander Petrovskiy

March 18, 2011 at 7:49 pm