PowerShell Scripting – Getting Started

Getting started with PowerShell..

PowerShell is a “new kid on the block” in the scripting arena..

Be sure you have PowerShell installed.  If you run Windows Update, it should be installed automatically..

To start it, you can simply go to a Command Prompt (CMD.EXE) and type in powershell <ENTER>

To call up a script, you can just type in the name of the script and press the <ENTER> key.

For example type in:  rts-extract, or rts-extract.ps1, or can you use “command-line” completion, by pressing the <TAB> key.

Note: There is a caveat to running a PowerShell Script.  After you install it, you cannot run scripts as your computer system by default blocks them after installing PowerShell.

Enable Script Support in PowerShell..

  1. Open PowerShell (if you are running PowerShell on Windows Vista, right-click your PowerShell icon and select Run as administrator. If you don’t do this, you will not be able to enable script support).
  2. Check the current script execution policy by using the Get-ExecutionPolicy cmdlet. To do this, input Get-ExecutionPolicy and press Enter on your keyboard. PowerShell will return a value of Restricted.
  3. To change the script execution policy, use the Set-ExecutionPolicy cmdlet. Input Set-ExecutionPolicy unrestricted and press Enter on your keyboard.
  4. To ensure that the script execution policy has been changed, use the Get-ExecutionPolicy cmdlet again. PowerShell should return a value of Unrestricted.

So, now you will get the results you are looking for in the resulting CSV file.

Here is our code we are using..  Note: The is part of an application I use called RoyalTS and it uses an XML configuration file for the servers I connect to.  I had a need to extract out Server Names, IP, etc. and it was shared with me that this can be done pretty easily in PowerShell.

[xml]$Document=Get-Content ..\PIBConnections.rts;
$Categories=$Document.SelectNodes("/RTSDocument/Categories/RTSCategory");
$Connections = @(
 foreach($Category in $Categories){foreach($Connection in
 $Category.Connections.RTSConnection) {$Connection|Select-Object CategoryName, ConnectionName, Host, User, Domain}}
)
$Connections|Export-Csv -path .\test.csv;

To leave PowerShell, just type in exit and then exit again to leave the CMD prompt.

This is by no means a complete lesson in PowerShell, but a high-level of one of the things you can do with it..

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: