Welcome to 

    mickyj.com

   


















     

   
 

    

    


PowerShell

Have you ever written a BAT file, VBS or Perl script before? It's time you looked at Windows PowerShell instead! (Previously codenamed Monad)

 

It's a powerful tool for writing automation scripts like build or pre-processing, etc. It's extremely fun to use it since it's .Net based and supports Xml and objects.

Microsoft Windows PowerShell command line shell and scripting language helps IT Professionals achieve greater productivity. All your scheduling, reporting and data collecting scripting needs can be fulfilled by PowerShell.

 

Using it's admin-focused scripting language, you have access to more than 130 standard command line tools (cmdlets), and consistent syntax and utilities, Windows PowerShell allows IT Professionals to more easily control system administration and accelerate automation.

 

Windows PowerShell is easy to adopt, learn, and use, because it works with your existing IT infrastructure and existing script investments, and because it runs on Windows XP, Windows Vista, Windows Server 2003 and Windows Server 2008, Exchange Server 2007, System Center Operations Manager 2007, System Center Data Protection Manager V2, and System Center Virtual Machine Manager leverage Windows PowerShell to improve efficiency and productivity.

Basically PowerShell exposes the power of .NET via a command line environment and scripting language, it allows you to pipe objects from one command to another and provides powerful support for XML handling and regular expressions.

 

... Cool !

 

You can download it or use it in Server 2008.

(it is also a core part of Exchange 2007, Windows Server 2008, and SQL Server 2008)

 

Start using it by Clicking Start -> Programs-> Windows PowerShell. Choose to always accept Microsoft signed code.

 

The command to execute is powershell.exe

 

find out what you can do with PowerShell by typing

PowerShell with the option “-?” or “-Help”.

 

What else can you do with PowerShell? Take a look at these examples.

 

You can run PowerShell over SSH, refer here.

 

Remember that PowerShell makes configuration changes, good and bad. Practice your commands and test them thoroughly before using them on a production device.

You might consider using the “-confirm” parameter to test configurations before execution.

 

Windows 2008 Server

PowerShell is included with Windows Server 2008, but you do need to install it. Here are some steps you need to take to get PowerShell up and running on your server.

 

    

Select Features

 

 

 

Add a new feature

 

Choose PowerShell

 

 

Once it is installed, it will appear on your start menu

 

 

What are some cool things I can do with PowerShell?

 

Firstly, everything you can do at the normal command prompt (CMD.exe). You can abandon the normal command prompt for this enhanced one.

 

Do you backup using external disks? Do you have a receptionist changing disks and just yanking the USB cable out before the device is released safely? Now you can write a script to disconnect the USB drive and check what is still plugged in much like the script I used to use.

 

To find out what USB devices you have:

gwmi Win32_USBControllerDevice -computername SERVER1 |fl Antecedent,Dependent


(Note the pipe command before the fl)

gwmi makes a WMI call to retrieve the USB devices installed in a local or remote system.

 

Have you ever had to stop a stuck service? SMTP comes to mind when IIS is under a heavy load. Normally you have to use a third party tool like Process Explorer. Task Manager is not powerful enough.

 

Lets assume we have an executable named "stuckservice.exe". We can use wildcards in our query

get-process stuckser*

 

this will come back with information about his thread and the ID (Process ID).

 

Stop the thread with "stop-process -id (ID Number)". Very cool. You don't have to reboot or wait it out. The process is no longer running allowing you to restart it if you need to.

 

Have you ever wanted to look at the permissions of folder structures under NTFS and Xcacls was just to much? I remember writing a batch file to use Xcacls recursively through a folder system and it was a nightmare.

 

Get-ChildItem G:Company -recurse | Get-Acl | export-csv c:\permissions.csv

(Note the Pipe command before Get-Acl and export-csv).

 

This will look through the entire G:\Company path and display the permissions (ACLs) for the contents of the path. The Get-ChildItem provides an inventory of the file system objects, and that collection is passed to Get-Acl to provide the results for each item. The final result is captured into a CSV file you can later open in Excel or work magic on with VBS script or other PowerShell functions. If you were a heavy command line user of the past, you would recall the older pipe commands. You can still use them here and output the console text into a raw text file.

 

Get-ChildItem G:Company -recurse | Get-Acl > c:\permissions.txt

 

The -recurse command can collect a lot of data. It might take a while and create large files.

(Ever run a "Dir /a /s" on a servers C:\ drive? it can easily be over 15 mb of data if you pipe it into a text file).

 

Have you ever written your own backup scripts? Maybe items that are triggered in task scheduler and do you need some verbose output with timestamps ?

You can have a timestamp entered in series so you can determine how long a single step occurs or to use as a logging mechanism for your scripts.

To insert a timestamp, enter one of the following commands as a single line within your .ps1 file (Windows PowerShell only executes scripts if the file extension is .ps1)

As the PowerShell scripts are saved as .ps1 files, it easy to modify, import, and transfer scripts across systems.

Command Output example
“$(Get-Date -format g) Start logging” 1/3/2008 9:30 AM
“$(Get-Date -format F) Start logging” Saturday, March 05, 2008 9:30:15 AM
“$(Get-Date -format o) Start logging” 2008-03-05T9:30:15.0366710-09:30

There are many other formats for the Get-Date command so take a look around under the hood.

So you are starting to realize the power of PowerShell. You are also noticing results can fly past on the screen very quickly. In the old days we would pipe the output through More.exe.
(| more). Now we have a more sophisticated control over console data.

If you are not exporting the results to a file, it may be impossible to view the console text. The  Get-ChildItem cmdlet can return many results depending on your path contents and is useful here. Lets make a function that behaves like "more" using Get-ChildItem. It will view the results onscreen by displaying one line every half-second.

function moretool { process { $_; Start-Sleep -seconds .5}}

To make a PowerShell command use the moretool function, call it with a pipe at the end of the command and then the function name as shown below:

Get-ChildItem G:Company | moretool

(Note the Pipe command)

One of the coolest parts of PowerShell allows you to traverse the Registry just like a file system.

The PSDrive command lets you view objects of the Windows environment. It can do way more than show you network, local, or removable drives. My favorite task is using the HKLM PSDrive to view the HKEY_LOCAL_MACHINE top-level hive of the registry. To get into the registry, enter the following command:

cd HKLM:

You are then in the registry hive and can view and even delete items. This is more difficult and typing than using Regedit but it is also programmatically simple and efficient.

Have you ever used Linux and used the command line to put processes into the background so you can continue to work? PowerShell can do this. You can send a series of commands to execute at once and let them complete on their own schedule.

The command to launch a background job leads with the start-psjob -command parameter. You can query PowerShell on the status of any of the jobs with the following command:

get-psjob

You can remove any failed jobs by running the following command:

remove-psjob (id)
 


 

So what comes after PowerShell V1?  V2 of course.

 

V2 comes with a Graphical interface. (That should make people who live in the GUI feel a little better ..... having something to click).

 

Whilst we wait for the release of V2 here are some things it includes; a graphical interface, Graphical PowerShell, and many new undisclosed features.

 

If you want to evaluate PowerShell 2.0, note that the WS-MAN v1.1 package is required, and if you want to use the graphical interface, Microsoft .NET Framework 3.0 is required.

Note: You have to configure the execution policy through PowerShell (non-graphical version) before using the tool. Configure one of the following execution policies:

Set-ExecutionPolicy Restricted      (check only)
Set-ExecutionPolicy AllSigned       (most secure)
Set-ExecutionPolicy RemoteSigned    (medium secure)
Set-ExecutionPolicy Unrestricted    (least secure)

using the PowerShell graphical interface you can select a single line or multiple lines and execute them by pressing the F5 key. The normal shortcuts we all use (In Microsoft Office and generally in Windows)  Ctrl + S to save, Ctrl + Z to undo, Ctrl + C to copy, and Ctrl + V to paste are available.

 

 

 

Utility that writes Windows Instrumentation Management (WMI) scripts for system administration.

 

 

 

 

 

    

 

     ( )

 

 

 

 

                                                             This page was written and designed by Michael Jenkin 2011 ©