IIS7 PowerShell Provider Tech Preview 2

July 4, 2008

no comments

 

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 :)

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>