PowerShell:在IIS 7和IIS 7.5上的ps1脚本中加载WebAdministration [英] PowerShell: Load WebAdministration in ps1 script on both IIS 7 and IIS 7.5

查看:167
本文介绍了PowerShell:在IIS 7和IIS 7.5上的ps1脚本中加载WebAdministration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PowerShell脚本,可以在IIS中配置网站和Web应用程序设置。因此,我在WebAdministration管理单元中使用cmdlet。但此脚本需要在Windows 2008,Windows 7和Windows 2008 R2上运行。我需要它来运行用户的最小问题。

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.

问题是Windows 7和Windows 2008 R2使用IIS 7.5作为模块安装WebAdministration 。在Windows 2008上,我们安装了IIS 7 PowerShell提供程序,该提供程序将WebAdministration安装为快照。

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.

因此在脚本中包含导入模块WebAdministration 在IIS 7上爆炸,但在IIS 7.5上工作正常,包括 add-pssnapin WebAdministration 在IIS 7.5上爆炸,但在IIS 7上工作正常。

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.

因此,我们的解决方法是让管理员在运行脚本之前使用适当的环境命令手动加载WebAdministration。但这不是最佳的,因为很容易忘记哪个命令在哪个环境中起作用。我们可以创建两个不同的脚本,但这会产生开发的维护问题。

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.

有没有人解决过这个问题?有谁知道如何检查环境,然后从PS脚本中调用相应的cmdlet?

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(针对我的情况)----

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

该解决方案是代码和预配置控制台的组合。 IIS 7 PoSH Provider包含一个桌面快捷方式,用于启动加载了WebAdministration的PoSH控制台。结合以下功能使我的脚本在所有三个系统上都像魅力一样运行。

The solution is a combination of code and pre-configured 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
  }
}


推荐答案

是否可以从一个或另一个中捕获错误,并执行相反的操作。不要使用我的shell,但是类似于:

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
}

在第一个,我知道可能需要修改错误类型检查。就工作而言,这实际上可以通过在注册表中查找已加载的管理单元在C#中完成,或者安装在机器上的IIS版本,然后使用适当的方法。

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.

这篇关于PowerShell:在IIS 7和IIS 7.5上的ps1脚本中加载WebAdministration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆