使用Net在Linux环境下调用PowerShell
前言
最近,在使用PowerShell做一些事情,然后需要放到AKS运行。
正文
一开始,使用的System.Management.Automation调用PowerShell,后来,发现有些功能在AKS下面执行不了,后面才想到使用Process的方式,如下:
public void ExecutePowerShell(string script) { try { ProcessStartInfo startInfo = new ProcessStartInfo { FileName = "pwsh", Arguments = $"-Command {script}", RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true }; System.Diagnostics.Process process = new System.Diagnostics.Process { StartInfo = startInfo }; process.Start(); string output = process.StandardOutput.ReadToEnd(); string error = process.StandardError.ReadToEnd(); process.WaitForExit(); Console.WriteLine("Output:"); Console.WriteLine(output); if (!string.IsNullOrEmpty(error)) { Console.WriteLine("Error:"); Console.WriteLine(error); } int exitCode = process.ExitCode; Console.WriteLine($"Exit Code: {exitCode}"); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); Console.WriteLine(ex.StackTrace.ToString()); } }
这里就是新建一个Process,使用pwsh调用PowerShell,只需要传入对应的脚本就行了。
如果命令非常多,可以试试StringBuilder来新建PowerShell脚本。
博文推荐: |
SharePoint 2013 WebPart 管理工具分享[开源] |
基于SharePoint 2013的论坛解决方案[开源] |
SharePoint 2013 学习基础系列入门教程 |
SharePoint 2013 图文开发系列之门教程 |
SharePoint Designer 学习系列入门教程 |
特:如果有SharePoint项目,欢迎邮件联系我,Email:linyu_s@163.com |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
2018-11-26 SharePoint Online 自定义Modern UI表单