【问题标题】:.Net core console application Stop-WebAppPool Remote connect via WinRM using Microsoft.PowerShell.SDK.Net 核心控制台应用程序 Stop-WebAppPool 使用 Microsoft.PowerShell.SDK 通过 WinRM 远程连接
【发布时间】:2020-07-17 16:37:28
【问题描述】:

我需要帮助来解决将 .Net Core Web 应用程序 Api 自动部署到服务器的问题。 如您所知,如果您之前不停止AppPool,则不可能覆盖.dll,并且IIS 中没有解决方案。 实际上通过使用PowerShell 我可以执行一个脚本来做我需要的脚本如下所示: PowerShell script

实际上我需要一个控制台应用程序来执行相同的工作,我发现我可以使用Microsoft.PowerShell.SDK 来实现一个解决方案。

public static void RunC()
    {
        string us = "xxxxxxxxxxxxx"; //User
        string pw = "xxxxxxxxxxxxxxxx";//Passwprd
        string sv = "xxx.x.xx.xxx";//Server
        string apppoolname = "xxxxxxxxxxxxx";

        StringBuilder script = new StringBuilder();
        //Creazione script PS 
        script.Append("$password = ConvertTo-SecureString \"" + pw + "\" -AsPlainText -Force" + Environment.NewLine);
        script.Append("$user = \"" + us + "\"" + Environment.NewLine);
        script.Append("$cred = New-Object System.Management.Automation.PSCredential ($user,$password)" + Environment.NewLine);
        script.Append("Enter-PSSession -ComputerName \"" + sv + "\" -Credential $cred" + Environment.NewLine);
        script.Append("Import-Module webadministration");
        script.Append("Stop-WebAppPool \"" + apppoolname + "\"");
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();

        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(script.ToString());

        //pipeline.Commands.Add("Out-String");

        Collection<PSObject> results = pipeline.Invoke();

        StringBuilder sb = new StringBuilder();
        foreach (PSObject pSObject in results)
        {
            sb.AppendLine(pSObject.ToString());

        }
        Console.WriteLine(sb.ToString());


    }

但我在图像中显示错误,接缝模块未加载或类似的东西.. 有人可以以某种方式帮助我吗?

谢谢你:-)

【问题讨论】:

    标签: c# powershell .net-core winrm


    【解决方案1】:

    我没有在图像中看到错误,但如果这是由于未导入 WebAdministration 引起的问题,则应该修复它:

    #Requires -Modules WebAdministration
    

    将它放在脚本的请求位置,如果该模块在会话中不可用,它将尝试导入该模块。微软文档:

    指定脚本所需的 PowerShell 模块。输入模块名称和可选的版本号。

    如果所需模块不在当前会话中,PowerShell 会导入它们。如果无法导入模块,PowerShell 会引发终止错误。

    source

    希望对你有帮助,祝你好运。

    【讨论】:

    • 嗨 Roque,谢谢您的回答。我会试试看 。因为打开远程连接后导入为代码行 --> script.Append("Import-Module webadministration");不要为我工作
    • 我的意思是如果这不起作用我认为这也不会,但如果它无法导入,我不会关闭脚本,如果它使用#Requires 执行,那么你知道不是模块问题。
    猜你喜欢
    • 1970-01-01
    • 2019-09-26
    • 2020-07-31
    • 2021-05-13
    • 2018-05-14
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2021-03-07
    相关资源
    最近更新 更多