Powershell,使用 msdeploy 启动或停止 iis 站点

Powershell, start or stop iis site using msdeploy

我正在尝试将我的批处理脚本迁移到 powershell。

我已经尝试编写脚本并运行它来自 powershell ise。

$sites = @("abc","xyz","pqr")
foreach ($site in $sites)
{
    msdeploy -verb:sync -verbose -source:runcommand -dest:runcommand="$env:windir\system32\inetsrv\appcmd stop site /site.name":$site
}

当我 运行 命令 (msdeploy) 时,它 运行 在命令提示符下是完美的。

我收到以下错误(附后):

Error capture

如果有人可以帮助我,我将不胜感激。提前致谢。

您需要使用 Invoke-Expression,这样您就可以将字符串作为命令执行。将您的命令存储为字符串并将其作为参数传递给 Invoke-Expression

$sites = @("abc","xyz","pqr")
$commandPrefix = 'msdeploy -verb:sync -verbose -source:runcommand -dest:runcommand="$env:windir\system32\inetsrv\appcmd stop site /site.name"'
foreach ($site in $sites)
{
    $command = $commandPrefix ":" $site
    Invoke-Expression $command
} 

经过这么多的努力,我想通了,并解决了这个问题。

foreach($site in $sites){
msdeploy -verb:sync -verbose -source:runcommand -dest:runcommand=`"$env:windir\system32\inetsrv\appcmd stop site /site.name`":"$site",computername="$serverName",username="$user",password="$passCode"
}

这只是一个转义字符,帮助我 运行 脚本 ("`")。

我希望它能帮助到一些人而不拉扯他们的头发。

您也可以尝试使用 recycleApp 提供程序:

msdeploy -verb:sync -source:recycleApp -dest:recycleApp="MySite",recycleMode="StopAppPool"
//deploy and sync content
msdeploy -verb:sync -source:recycleApp -dest:recycleApp="MySite",recycleMode="StartAppPool"