I have been thinking about a great real-world example of a script to do a simple launcher..
You can go as simple as a batch file or a full-featured AutoIT Script that is compiled into an executable..
We’ll keep this simple and just use Windows notepad.exe and calc.exe for now..
In Batch, we have (Note: in ASE, you will need to change the behavior to “Execute via shell”) :
@echo off
Color e0
Cls
:menu
@Ping 127.0.0.1 -n 3 -w 1000 > nul
echo.
echo Menu options:
echo.
echo 1 Notepad
echo 2 Calculator
echo.
echo x Exit
echo.
Set /p userc= What would you like to DO?
If "%userc%"=="1" Goto notepadrun
If "%userc%"=="2" Goto calcrun
If /i "%userc%"=="x" Goto exitnow
echo %userc% is an invalid choice
Goto menu
:notepadrun
notepad.exe
echo.
echo Returning to Main Menu..
@Ping 127.0.0.1 -n 1 -w 1000 > nul
Cls
Goto menu
:calcrun
calc.exe
echo.
echo Returning to Main Menu..
@Ping 127.0.0.1 -n 1 -w 1000 > nul
Cls
Goto menu
:exitnow
echo Exiting menu..
@Ping 127.0.0.1 -n 1 -w 1000 > nul
Cls
Exit
Even though that is a simplistic example, there is a lot of code there..
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..
- 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).
- 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.
- To change the script execution policy, use the Set-ExecutionPolicy cmdlet. Input Set-ExecutionPolicy unrestricted and press Enter on your keyboard.
- 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..
Comments