"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

July 02, 2009

Powershell basics

Windows PowerShell includes new command-line tools that allow you to efficiently complete server administration tasks that are common across all Windows Server 2008 roles, such as services, processes, and storage

Scenarios using powershell
· Deploying and configuring Internet Information Services 7.0
· Terminal Server managementManaging command-line services, processes, registry, and WMI data (get-service, get-process)
Powershell Downloads
Windows PowerShell 1.0 Installation Package for Windows Vista (KB928439) - http://www.microsoft.com/downloads/details.aspx?FamilyId=C6EF4735-C7DE-46A2-997A-EA58FDFCBA63&displaylang=en

Powershell Provider for IIS7 - http://www.iis.net/downloads/default.aspx?tabid=34&g=6&i=1664

PowerShell Scriptomatic - http://www.microsoft.com/technet/scriptcenter/tools/psomatic.mspx
PowerShell GUI Editor - http://powergui.org/index.jspa

Powershell Handson Examples
Example 1
1. Type "Hello " + "World"
2. Result will be Hello World. First Classic Example Over

Example 2
1. Type the below function
function sum ([int]$a,[int]$b) {
return $a + $b
sum 10 15

Example 3
We have cmdlets in powershell equivalent to cmd.exe built in commands.
· Get-Location
· Get-ChildItem
· Get-Date

Example 4
From Powershell cmd prompt type notepad. It will open notepad ( Not a gr8 example though :-))
1. Notepad
2. Type Get-Process
This will list processes that match name notepad
Enough of Examples...What are real world examples...

Example 5
Installing an MSI
2. Created a ps1 file with following commands
$box="TestPC002" #this is the name of your server
$product= [WMICLASS]"\\$box\ROOT\CIMV2:win32_Product"
Write-Host "Installing software on $box"
Do F5, MSI Installed. Easy isn't it..

Example 6
Event Monitoring
# Powershell script to find Error messages in the System eventlog.
get-EventLog system -newest 2000 where {$_.entryType -match "Error"}

Example 7
Writing to EventLog Using Power Shell
function Write-EventLog
param([string]$msg = "Default Message", [string]$type="Information")
$log = New-Object System.Diagnostics.EventLog
write-eventlog "Testing as function" "Information"

Powershell allow you to combine two or more commands into a single custom command that creates an operation known as a pipeline. When commands are chained together in a pipeline, they pass data to each other as objects.

Example 1
Use the Get-Member Cmdlet to discover what types of objects the get-process Cmdlet returns
get-process get-member
Example 2
$a = Get-ChildItem *.txt
foreach ($file in $a)
if ($file.length -gt 10)
Write-Host $file.name

In this example, the results from the Get-ChildItem command are put into the $a variable, which is then used by the foreach loop to iterate through the file information.
PowerShell Scriptomatic
Opened up the tool
1. On the WMI namespace select Root\CIM2. Classes select Win32_BIOS. We see the script populating below data

Below code got populated
$computer = "LocalHost"
$namespace = "root\CIMV2"
Get-WmiObject -class Win32_BIOS -computername $computer -namespace $namespace

Links and References

PowerShell for SQL Server - Basics

FAQ - http://www.microsoft.com/windowsserver2008/en/us/powershell-faq.aspx
Powershell Script Center - http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx http://msdn.microsoft.com/en-us/library/system.management.automation.psobject(VS.85).aspx http://blogs.msdn.com/johan/archive/tags/PowerShell/default.aspx http://blogs.msdn.com/powershell/

No comments: