tags:

views:

1031

answers:

2

I have a PowerShell script that configures web site and web application settings in IIS. So I use the cmdlets in the WebAdministration snap in. But this script needs to run on Windows 2008, Windows 7 and Windows 2008 R2. And I need it to run with minimal fuss from the user.

The problem is that Windows 7 and Windows 2008 R2 use IIS 7.5 which comes with the WebAdministration installed as a module. On Windows 2008 we have installed the IIS 7 PowerShell provider which installs WebAdministration as a snap in.

So including import-module WebAdministration in the script blows up on IIS 7 but works fine on IIS 7.5 and including add-pssnapin WebAdministration blows up on IIS 7.5 but works fine on IIS 7.

So our workaround is to make the administrators load WebAdministration manually with the appropriate command for the environment before running the script. But this isn't optimal as it is easy to forget which command works in which environment. We could create two different scripts, but that creates a maintenance problem for development.

Has anyone solved this problem? Does anyone know how to check the environment and then call the appropriate cmdlet from within the PS script?

--- ANSWER (for my situation) ----

The solution is a combination of code and preconfigured console. The IIS 7 PoSH Provider includes a desktop shortcut that launches a PoSH console with WebAdministration loaded. That combined with the following function make my script run like a charm on all three systems.

Function Load-WebAdmin {
  $webAdminModule = get-module -ListAvailable | ? { $_.Name -eq "webadministration" }
  If ($webAdminModule -ne $null) {
    import-module WebAdministration
  }
}
A: 

Is it possible to catch the error from one or the other, and do the opposite. Dont have my shell handy but something like:

$succeeded = import-module WebAdministration
if (($succeeded -ne $null) -and ($succeeded.GetType() -eq [System.Exception]) {
  #Could not import, trying to snapin
  add-pssnapin WebAdministration
}

Actually thinking about this a bit more...

$hasSnapin = get-pssnapin | Select { $_.Name.toLower().Trim() = "webadministration" }
if ($hasSnapin -ne $null) {
  add-pssnapin WebAdministration
} else {
  import-module WebAdministration
}

On the first one, I know the error type check will probably need to be modified. As far as the work going on, this can actually be done in C# by looking in the registry for loaded snapins, or the IIS version installed on the machine and then use the appropriate method.

GrayWizardx
The only problem with this is that while **import-module** can be run multiple times without raising an error even though the module is already loaded, **add-pssnapin** will raise an error if the snapin is already loaded.Thanks for the inspiration for a workable solution.
Mark Arnott
That could be fixed with a second call to ps-snapin to see if it is already loaded in the current session
GrayWizardx
A: 

If you want to create "pre-configured" PowerShell sessions, look into PowerShell console files e.g.:

man Export-Console -full

You can create one for use on Win7 and Server 2008 R2 and another for use on Server08.

Keith Hill
I didn't think console files supported modules. You can however use powershell with the -file parameter to create a custom environment that supports loading modules.
Chad Miller
Oops. Yes you're right. They don't support modules - too bad.
Keith Hill
Creating a shortcut to launch powershell with the webadministration module/snap in already loaded might be a solution if I can't figure out how to do it cleanly in the script.
Mark Arnott