在非英语机器上运行Powershell C#

时间:2018-02-13 19:51:27

标签: c# powershell

我通过C#代码生成一个powershell脚本。然后我保存脚本并通过C#调用命令提示符来运行创建的脚本。这在英语机器上工作正常,但是当我尝试在德语机器上运行代码时,它无法读取英语脚本代码。

我尝试在代码中添加一个文化检测,但它似乎仍然没有飞行

            //Create MyScript
            sb.AppendLine("function MyCustomFunction ([string]$input) {");
            //...powershell function created I am using

            //Make a function to detect current machine language
            sb.AppendLine("function Using-Culture (");
            sb.AppendLine("[System.Globalization.CultureInfo]$culture,");
            sb.AppendLine("[ScriptBlock]$script)");
            sb.AppendLine("{");
            sb.AppendLine("    $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture");
            sb.AppendLine("    trap");
            sb.AppendLine("    {");
            sb.AppendLine("    [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture");
            sb.AppendLine("    }");
            sb.AppendLine("    [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture");
            sb.AppendLine("    $ExecutionContext.InvokeCommand.InvokeScript($script)");
            sb.AppendLine("    [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture");
            sb.AppendLine("}");

            //Call my function after detecting the language
            sb.AppendLine("Using-Culture " + langCode + " {");    
            sb.AppendLine("MyCustomFunction  " + someInput);                
            sb.AppendLine("}");


            //....save the stringbuilder to a file

            //Run powershell via command prompt
            command.Run(@"powershell.exe",
                    @"-ExecutionPolicy Bypass " + @"& '" + The_Script_I_Created +"'",
                    out output,
                    out errors); 

0 个答案:

没有答案