none
C#调用PowerShell的问题 RRS feed

  • 问题

  • 这是一个VisualSVN 创建用户的命令,使用PowerShell的操作是:
    1.输入命令行 $password = Read-Host -AsSecureString
    2.输入 密码
    3.输入命令行 Set-SvnLocalUser -Name jack -Password $password

    上面这段操作,用C#应该如何实现?

    2020年10月14日 6:59

全部回复

  • Hi peify,

    你可以尝试下面的代码从c#调用Powershell.

                PowerShell psinstance = PowerShell.Create();
                psinstance.AddScript("$password = Read - Host - AsSecureString");
                Console.WriteLine("Please input passoword");
                string password = ConsolePlus.ReadPassword();
                psinstance.AddScript(password);
                psinstance.AddScript("Set-SvnLocalUser -Name jack -Password $password");
                psinstance.Invoke();
                Console.WriteLine();
                Console.WriteLine("success");

    示例:

    其中你需要安装的nugetpackage有:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年10月15日 6:51
    版主