【问题标题】:C# -- Windows Server 2012 installing Features and Roles using PowershellC# -- Windows Server 2012 使用 Powershell 安装功能和角色
【发布时间】:2014-04-02 16:37:17
【问题描述】:

我编写了一个使用 Powershell API 安装 Windows 角色和功能的应用程序。它在 Windows 2008 R2 中运行良好,但在 Windows 2012 中没有任何反应;程序继续运行,好像一切正​​常,但没有安装任何东西。

我尝试将程序制作为 .NET v4.5(它是 .NET v2.0),但这并没有帮助。我一直在谷歌这件事上,我找不到有效的解决方案。事实上,大多数人说要使用适用于 Windows 2008 R2 的那种实现。这是我的代码:

        public bool runPowerShell(string command, string args)
    {
        mLogger myLogger = mLogger.instance;  //How I log stuff in my application.
        bool done = false; //default Return value.
        const string path = @"C:\\XMPLogs\\Roles and Features"; //Where Powershell output will go.

        //Make sure Powershell log directory is there.
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);

        //Start a new Powershell instance.
        PowerShell powershell = PowerShell.Create();
        System.Collections.ObjectModel.Collection<PSObject> output = new System.Collections.ObjectModel.Collection<PSObject>();
        StringBuilder strBuilder = new StringBuilder(); //Used to examine results (for testing)
        powershell.AddScript(@"Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted");
        powershell.AddScript(@"Import-Module ServerManager");
        //powershell.Invoke();

        powershell.AddScript(command + " " + args);
        try
        {

            output = powershell.Invoke();

            // Construct a StringBuilder to examine the output of Invoke()
            foreach (PSObject obj in output)
                strBuilder.AppendLine(obj.ToString());

            // Show the StringBuilder to see results (always empty!)
            MessageBox.Show(strBuilder.ToString());
            done = true;
        }
        catch (Exception ex)
        {
            string test = ex.ToString();
            MessageBox.Show(test);
            myLogger.output("ERRO", "PowerShell command " + command
                + "failed to run with arguments \"" + args + "\".  Message:  " + ex.ToString());
            done = false;
        }

        powershell.Dispose();
        return done;
    }

我会这样调用方法:

runPowerShell("add-windowsfeature", "-name FS-FileServer -logpath \"c:\\XMPLogs\\Roles and Features\\File Services.log\"");

“输出”对象中没有任何数据,日志文件也没有。所以,我不知道发生了什么。我确实知道,如果我在方法调用中获取两个参数并手动将它们输入到 Powershell 提示符中,安装就会完美运行。

谁能看到我在 Windows Server 2012 上的这个实现有什么问题?

谢谢。

【问题讨论】:

    标签: c# powershell windows-server-2012


    【解决方案1】:

    如果没有更多信息,很难知道这里的真正问题是什么。也许您没有以管理员身份运行应用程序,因此不允许添加 Windows 功能?

    但是,PowerShell 在终止错误(会阻止执行并引发异常,这会使您的代码进入 catch 语句)和非终止错误(仅写入错误流并且不会进入您的捕获语句)。

    如果您运行Get-Help Write-ErrorGet-Help about_ThrowGet-Help about_Try_Catch_Finally,您可以阅读更多相关信息。

    我猜你的 powershell 命令会导致一个非终止错误。要确定是否发生了非终止错误,您可以检查powershell.HadErrors 属性并获取错误消息,您可以阅读powershell.Streams.Error 属性。

    这可能会帮助您找出发生的错误,并希望能帮助您解决问题。

    【讨论】:

    • 确实有帮助,谢谢。我现在知道我的错误了。但是,我想知道我应该看什么来纠正它们。基本上,我试图运行的模块如未找到。但是,就像我在原始帖子中所说的那样,我可以打开 Powershell 命令提示符并手动运行这些命令而没有任何问题。有关示例,请参阅我的下一条评论。
    • [INFO]: [04/02/2014 13:56:54 -05:00] - 安装 .NET v3.5.1 功能 [错误]:[04/02/2014 13:56:54 -05:00] - add-windowsfeature -name NET-Framework-Core -logpath "c:\XMPLogs\Roles and Features\.NET v3.5.1.log " 由于在任何模块目录中都找不到有效的模块文件,因此未加载指定的模块 'ServerManager'。 [错误]:[04/02/2014 13:56:54 -05:00] - add-windowsfeature -name NET-Framework-Core -logpath "c:\XMPLogs\Roles and Features\.NET v3.5.1.log "
    • 术语“add-windowsfeature”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,如果包含路径,请验证路径是否正确并重试。
    • 我知道存在 ServerManager 和 add-windowsfeature。我可以使用它们。 Powershell API 在我的 C# 代码中找不到什么?我正在以提升的权限运行应用程序,因此它不应该是访问权限问题。
    • 找到了答案。我只需要将代码编译为 x64。有关此链接发生的更多信息:Powershell commands from C# 'the term is not recognizes as cmdlet'
    猜你喜欢
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2014-06-19
    • 1970-01-01
    相关资源
    最近更新 更多