gpt4 book ai didi

c# - 直接在 C# 代码中编写 powershell 命令

转载 作者:行者123 更新时间:2023-11-30 21:40:48 25 4
gpt4 key购买 nike

我有以下代码:

        PowerShell ps = PowerShell.Create();

// Call the PowerShell.AddCommand(string) method, add
// the Get-Process cmdlet to the pipeline. Do
// not include spaces before or after the cmdlet name
// because that will cause the command to fail.
ps.AddCommand("mkdir");
ps.AddArgument("C:\\Users\\me\\Desktop\\test");

不幸的是,当我运行这段代码时,没有创建任何目录。我想我误解了这是如何工作的,但理想情况下,我希望我的最终应用程序能够运行 powershell 命令,然后直接在我的应用程序中获取输出。

从我的代码执行简单的 powershell 命令我做错了什么?

最佳答案

您的 powershell 命令缺少 ps.Invoke() 命令。只需在构建命令后添加它。这是 MSDN有关如何执行 powershell 命令的文章。

关于c# - 直接在 C# 代码中编写 powershell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44208044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com