如何在 C# Powershell 界面中处理写入主机和用户输入

问题描述

问题:

我们的系统管理员为我们创建了一个 powershell 脚本来迁移用户。 不幸的是,我没有脚本的写权限,但我有执行权限。

接口调用的脚本有大量的用户输入和写入主机。例如:
您要创建邮箱吗(是/否)?
现在要提供用户名吗?
用户名是否正确(是/否)?
您现在要提供员工 ID 吗?
员工 ID 是否正确(是/否)?
所有数据是否正确(是/否)?
等等。

这在 powershell 脚本中像这样处理:

private void execute_button_Click(object sender,RoutedEventArgs e)
{
    string username = this.user_textBox.Text;
    string identification = this.emplyeeID_textBox.Text;
    if (username == "mustma1") return;
    if (identification == "") return;
    // create powershell runspace and call account creating script
    RunspaceConfiguration psConfig = RunspaceConfiguration.Create();
    Runspace psRunspace = RunspaceFactory.CreateRunspace(psConfig);
    psRunspace.open();
    RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(psRunspace);
    runSpaceInvoker.Invoke("Set-ExecutionPolicy Bypass -Scope Process");
    // create a pipeline and Feed it the script text (AddScript method) or use the filePath (Add method)
    Pipeline pipeline = psRunspace.CreatePipeline();
    Command command = new Command(".\\CreateSKANMailBox.ps1");
    command.Parameters.Add("SamAccountName",username); // I don't have named paremeters
    command.Parameters.Add("EmployeeID",identification); // I don't have named paremeters
    pipeline.Commands.Add(command);
    pipeline.Invoke();
    psRunspace.Close();
    // reset text
    this.user_textBox.Text = "mustma1";
    this.emplyeeID_textBox.Text = "";
}

脚本很麻烦。它甚至比手动配置所有东西需要更长的时间!但我们被迫与它合作。我拒绝使用它,但后来我失去了工作,这就是我构建界面的原因。这是一个非常复杂的界面#sarkasm :

enter image description here

我将在我的应用程序中处理用户输入错误。这没什么大不了的。此外,脚本本身具有良好的防错功能

我当前调用脚本的代码如下:

net start "<ServiceName>"

程序失败,并显示主机不支持用户输入的声明。有什么方法可以使用 ps 运行空间浏览这样的脚本?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)