简体   繁体   中英

Writing powershell commands directly in C# code

I have the following code:

        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");

Unfortunately, when I run this piece of code, no directories are created. I guess I am misunderstanding how this works, but ideally, I want my final application to be able to run powershell commands and then grab the output directly within my application.

What am I doing wrong to execute a simple powershell command from my code?

You are missing ps.Invoke() command for your powershell command. Just add it after command is build. Here is MSDN article about how to execute powershell commands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM