DCSIMG
July 2008 - Posts - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

July 2008 - Posts

Video: Advanced Microsoft SQL Server PowerShell Tips and Tricks

Dan Jones, delves into the details of how to use SQL Server PowerShell and its cmdlets to administer SQL Server in an optimal fashion. He also describes the architecture and implementation of the SQL Server PowerShell.

http://www.microsoft.com/emea/spotlight/sessionh.aspx?videoid=984

PowerShell scripting contest! Win $5000!

VMware is challenging the world’s VI administrators to a contest of skill, creativity and raw scripting talent. If you think you have what it takes to be the best and an all expenses paid trip to VMworld Las Vegas is your cup of tea – this is for you.
You win $5,000, or a trip to VMworld 2008!

Read all about it on the contest announcement page and good luck!

Marco Shaw on NetCmdlets

Mark your calendar, Marco is going to give a presentation on /n software NetCmdlets at the next UK
PowerShell User Group meeting.

http://marcoshaw.blogspot.com/2008/07/uk-user-group-presentation-netcmdlets.html

BGInfo - Custom information

 

With BGInfo, from SysInternals, you can display various system configuration values on screen, such as the computer name, IP address, operating system version etc. What if you want to display values from other sources, like the registry or from WMI or even from your own custom script?

BGInfo is flexible enough to get/set this values for you with a few mouse clicks. For example, on our servers I wanted to add the server model and Serial number.

This is the main BGInfo window. You can edit which items to show, just delete the lines you don't need. To add new items, highlight the item you want in the 'Fields' select box and click the Add button.

 

 

To add WMI properties, click the 'Custom' button. In the next dialog click 'New':

 

You can see that BGInfo has more options to offer, other than WMI or Registry values. To add the SerialNumber WMI property, type a descriptive name in 'Identifier' text box and click the WMI Query radio button, then click browse. It can take some time for BGInfo to load all the WMI classes but it's worth while because the next dialog is actually a WMI browser!

  

 

Find your class and the property you want and that's it, pretty cool!

 

Here I've added two properties from different classes, the Model and SerialNumber properties.

 

Now, locate the new created properties in the 'Fields' select box and double click each one to add it. I made a few more adjustments to the main dialog (separating the properties with the equal characters).

 

 

Click the 'Preview' button to see the end result on your screen. Finally, you can place a shortcut to BGInfo in the startup folder so that it runs every boot/logon, or even configure it to display as the background for the logon screen. For more information see the 'Installation and Use' section in the help file.

Get your Exchange organization name

 

In Exchange 2007:

PS > Get-OrganizationConfig | select name

Name 
----
First Organization

 

In Exchange 2003:

function Get-ExchangeOrgName{ 
   $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() 
   $ddn = $domain.GetDirectoryEntry().distinguishedName 
   $config = [ADSI]"LDAP://CN=Microsoft Exchange,CN=Services,CN=Configuration,$ddn" 
   $orgName = $config.psbase.children | where {$_.objectClass -eq 'msExchOrganizationContainer'} 
   $orgName.name 
}

PS > Get-ExchangeOrgName
First Organization

IIS7 PowerShell Provider LiveMeeting


The IIS Team invites you to attend the IIS7 PowerShell Provider CTP2 Overview online meeting on Tuesday, July 15th 2008 at 11am PST


Agenda:

30 minutes: IIS7 PowerShell Provider CTP2 Overview
15 minutes: Q&A

Get all the info HERE.

Webcast: Operations Manager – PowerShell


Operations Manager – Powershell

Start Date: Wednesday, July 30, 2008 12:00 PM Eastern Time (US & Canada)

The Operations Manager UI is great but many times you want to do things quickly and with some automation – that’s where the Powershell cmdlets of OM come in! During this session we will examine some of the commonly used cmdlets and see how to build out a script which uses the Operations Manager SDK to create a custom subscription.

Technet Spotlight On Demand Video


Ben Pearce - Windows, PowerShell, and Windows Management Instrumentation: Unveiling Microsoft's Best Kept Secret

Without doubt the most underused and underrated technology out there today is Windows Management Instrumentation (WMI). Since Windows 2000, Microsoft has included scriptable remote administration in all operating systems.

For years people didn't understand WMI, but then Windows PowerShell was born, WMI became accessible, and then WMI became the best kept secret at Microsoft. This session is aimed at giving system administrators ideas, tips, and tricks on how they can use PowerShell to massively reduce their administrative overhead. From reviewing Hotfixes to managing Network Cards, from monitoring Virtual Machines to managing Failover Clustering, we delve into the power of WMI, and see how easy to use it really is.

This session introduces the basic architectural concepts of WMI, details how to utilize WMI from PowerShell, and finally introduces some killer scripts that can be used in the real world to make Windows administration way quicker.

 

 

IIS7 PowerShell Provider Tech Preview 2

 

The second CTP of the IIS7 Powershell provider has released bumping up the total number of cmdlets to 55!

After you download and install the provider you can start exploring it with the pre-built PowerShell console file:

Click on the Start Menu -> "All Programs" -> "IIS 7.0 Extensions" -> "IIS PowerShell Management Console".

The prompt of the new PowerShell command window is set to "IIS:\" - the root of the IIS Provider namespace.

image

 

In the example below I'm loading the provider snap-in (manually) into my current PowerShell session.

# find the IIS snap-in name
PS > Get-PSSnapin -Registered

Name        : IIsProviderSnapIn
PSVersion   : 1.0
Description : IIS Administration Provider

 

# load the snap-in


PS > Add-PSSnapin IIsProviderSnapIn

 

# get a list off all cmdlets name in the provider
PS > Get-Command -PSSnapin IIsProviderSnapIn | select name

Name
----
Add-WebConfiguration
Add-WebConfigurationProperty
Backup-WebConfiguration
Begin-CommitDelay
Clear-FrebData
Clear-WebConfiguration
ConvertTo-WebApplication
Disable-Freb
Disable-WebModule
Enable-Freb
Enable-WebModule
End-CommitDelay
Get-AppDomain
Get-AppPoolState
Get-ConfigurationBackup
Get-URL
Get-WebConfiguration
Get-WebConfigurationProperty
Get-WebHandler
Get-WebItemState
Get-WebModule
Get-WebRequest
Get-WebSiteState
New-AppPool
New-FtpSite
New-ManagedWebModule
New-VirtualDirectory
New-WebApplication
New-WebBinding
New-WebHandler
New-WebModule
New-WebSite
Remove-AppPool
Remove-ConfigurationBackup
Remove-VirtualDirectory
Remove-WebApplication
Remove-WebBinding
Remove-WebConfigurationProperty
Remove-WebHandler
Remove-WebModule
Remove-WebSite
Restart-AppPool
Restart-WebItem
Restore-WebConfiguration
Set-WebBinding
Set-WebConfiguration
Set-WebConfigurationProperty
Set-WebHandler
Set-WebModule
Start-AppPool
Start-WebItem
Start-WebSite
Stop-AppPool
Stop-WebItem
Stop-WebSite

 

To navigate the namespace simply set your location to the root of the IIS Provider namespace, now you can navigate using the get-childitem (dir for short):  

PS > cd iis: 
PS > dir 

Name
----
Sites
AppPools
SslBindings 

 

One thing I've noticed is the lack of command completion when navigating the provider hierarchy. For example, cd si<tab> or dir iis:\si<tab> doesn't resolve to the sites container. Only after manually executing 'dir iis:\sites' the list of sites became available for upcoming dir requests.

To get a list of web sites we list the sites container which returns a site collection:

PS > dir iis:\sites
Name ID State Physical Path Bindings ---- -- ----- ------------- -------- Default Web Site 1 Started %SystemDrive%\inetpub\wwwroot http *:80: test1 2 Started http *:80:test1 test2 3 Started http *:80:test2




 

The same applies for the AppPool container. It can be quiet long to type this each time you want to get all (or even one) web sites or app pools. I excepted to find corresponding cmdlets such as Get-WebSite or Get-AppPool but there are no.

PS > get-command -noun appp* -pss iis* | select name

CommandType     Name
-----------     ----
Cmdlet          Get-AppPoolState
Cmdlet          New-AppPool
Cmdlet          Remove-AppPool
Cmdlet          Restart-AppPool
Cmdlet          Start-AppPool
Cmdlet          Stop-AppPool


PS > get-command -noun webs* -pss iis* | select name

CommandType     Name
-----------     ----
Cmdlet          Get-WebSiteState
Cmdlet          New-WebSite
Cmdlet          Remove-WebSite
Cmdlet          Start-WebSite
Cmdlet          Stop-WebSite

 

You can overcome the absence of this cmdlets by defining your own functions to get this objects:

PS > function Get-WebSite($name='*') {Get-Item IIS:\Sites\$name}
PS > function Get-AppPool($name='*') {Get-Item IIS:\AppPools\$name} # get all websites PS 153> Get-WebSite Name ID State Physical Path Bindings ---- -- ----- ------------- -------- Default Web Site 1 Started %SystemDrive%\inetpub\wwwroot http *:80: test1 2 Started http *:80:test1 test2 3 Started http *:80:test2


# get a website name using wildcards
PS > Get-WebSite def* Name ID State Physical Path Bindings ---- -- ----- ------------- -------- Default Web Site 1 Started %SystemDrive%\inetpub\wwwroot http *:80:

 

OK, time to build a new site. There is a new cmdlet just for that:

PS > New-Website -name NewWebsite Name ID State Physical Path Bindings ---- -- ----- ------------- -------- NewWebsite 4 http *:80:NewWebsite New-WebSite : The object identifier does not represent a valid object.
Exception from HRESULT: 0x800710D8) At line:1 char:12 + New-Website <<<< -name NewWebsite


  

# the site was created but with an error. I'm not sure what is the
# cause so I logged a comment on that IIS.net forums # I found that if you assign the command to a # variable then the error is gone # e.g. $newSite = New-WebSite -name NewWebSite # despite the error the new site is up and running

PS > Get-WebSite new* Name ID State Physical Path Bindings ---- -- ----- ------------- -------- NewWebsite 4 Started http *:80:NewWebsite

 

Now lets try to remove it: 

PS > Get-WebSite NewWebsite | Remove-WebSite
PS > Get-WebSite

Name             ID State   Physical Path                 Bindings
----             -- -----   -------------                 --------
Default Web Site 1  Started %SystemDrive%\inetpub\wwwroot http *:80:
test1            2  Started                               http *:80:test1
test2            3  Started                               http *:80:test2

 
The site was removed successfully but WITHOUT any warning or confirmation. This is were another PowerShell built-in feature comes to rescue, the -confirm parameter.
When a cmdlet action significantly affects the system, such as by deleting data or by using a significant amount of system resources, Windows PowerShell can automatically prompt you for confirmation before performing the action:
 
PS 197> Get-WebSite new* | Remove-WebSite -Confirm

Confirm
Are you sure you want to perform this action?
Performing operation "Remove-WebSite" on Target "NewWebsite".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  
[?] Help (default is "Y"):

 

You can get more information on PowerShell's common parameters by typing:

PS > help about_CommonParameters

 

That's it for now :)

2008 Microsoft MVP Award

 mvp
I got an email from Microsoft regarding the 2008 Microsoft MVP Award. I was chosen for the award in the Admin Frameworks PowerShell category.

What a day! What can I say. It feels like walking on the moon. It is an honor to be among the great guys. I would like to thank the people who supported me along the way and to Microsoft for the award.

PowerShell ROCKS!