通过一系列Web服务器进行远程部署以部署代码

时间:2019-04-01 20:54:38

标签: powershell iis remote-server iis-10

我希望通过一些Web服务器和导入模块WebAdministration弹出窗口,以更改某些回收属性。

问题是,似乎foreach不喜欢用于远程会话。我猜这是因为单个代码块只能在1个远程会话中执行?是否可以通过多个会话弹出,还是必须手动执行?

只需寻找有关如何编写代码以部署到我所有服务器上所有apppool的输入。

我已经测试了通配符appPool目录,这似乎可行。

IIS10 Powershell 5.1.14393.2189

$servers = @("MyEnvironment-web01","MyEnvironment-web02","MyEnvironment-web03")
foreach ($server in $servers) {
    enter-Pssession -ComputerName $server
    Write-Host $server
    Read-Host -Prompt "Press Enter to continue" #This was added because I thought maybe it just needed time to connect? Doesn't need to be in here.
    import-module WebAdministration
        Get-ItemProperty -Path IIS:\AppPools\*
        exit-PSSession
        }

很多: import-module:未加载指定的模块“ WebAdministration”,因为在任何模块目录中均未找到有效的模块文件。 在第1行:char:1

这很奇怪,因为如果我手动运行for-each循环,那很好用。除了大约20%的时间没有....

我敢打赌我会为这一切错。

1 个答案:

答案 0 :(得分:0)

$Cred = Get-Credential -UserName <domain\user> -Message 'Enter Password'
$servers = @("myserver1"."myserver2","etc")
foreach ($server in $servers) {
    Invoke-Command -ComputerName $server -Credential $cred {
    import-module WebAdministration
    Get-ItemProperty -Path IIS:\AppPools\*
    }
}
相关问题