作者:黄晓敏3023 | 来源:互联网 | 2023-10-12 17:18
我们需要远程启动/停止IIS网站和应用程序池,因此我们可以远程部署网站。
我有一个Websocket应用程序,可以启动PowerShell脚本来完成这些活动。我构建了Powershell脚本来完成该任务,并且它们在Powershell提示符中可以完美地工作。但是,当我尝试从websocket运行这些脚本时,脚本会运行(脚本中有Write-Outputs),但是站点和池没有任何变化。我也看不到任何失败的信息。我将不胜感激。
下面是代码摘录:
using (PowerShell ps = PowerShell.Create())
{
string scriptCOntent= string.Empty;
string pathToReadScriptFile = string.Empty;
// add a script that creates a new instance of an object from the caller's namespace
if (r.StartStop.ToLower() == "stop")
{
pathToReadScriptFile = Path.Combine(scriptsPath,"StopPoolAndSite.ps1");
}
else
{
pathToReadScriptFile = Path.Combine(scriptsPath,"StartPoolAndSite.ps1");
}
using (StreamReader sr = new StreamReader(pathToReadScriptFile))
{
scriptCOntent= sr.ReadToEnd();
sr.Close();
}
ps.AddScript(scriptContent);
ps.AddParameter("siteName",r.SiteName);
ps.AddParameter("poolName",r.PoolName);
// invoke execution on the pipeline (collecting output)
Collection PSOutput = ps.Invoke();
// loop through each output object item
foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
await SendMessageToAllAsync($"{outputItem.ToString()}");
}
}
}
以下是powershell脚本代码之一:
Param(
[Parameter(Mandatory=$True)]
[string]$siteName,[string]$poolName
)
if (-Not $poolName)
{
$poolName = $siteName
Write-Output "PoolName not supplied. Using $siteName as default. "
}
Import-Module WebAdministration
Write-Output "Preparing to Start AppPool: $poolName"
Write-Output "(OutPut)Preparing to Start AppPool: $poolName"
Start-WebAppPool $poolName
Write-Output "Preparing to Start Site: $siteName"
Start-WebSite $siteName
Get-WebSite $siteName
实际上,我建议不要重新发明轮子,有一个项目可以检查这些:
- https://github.com/microsoft/iis.administration
- https://manage.iis.net/get