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
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.