【问题标题】:Powershell script in C# for joining windows domainC# 中用于加入 Windows 域的 Powershell 脚本
【发布时间】:2019-09-18 18:34:07
【问题描述】:

我正在使用下面这个函数来创建powershell脚本

public static void joinDomain()
{
    string path = @"C:\Windows\Temp\Test.ps1";
    if(!File.Exists(path))
    {
        using (StreamWriter sw = File.CreateText(path))
        {
            sw.WriteLine("add-computer –domainname ad.contoso.com -Credential AD\adminuser -restart –force");
        }
    }
}

成功创建脚本后,我使用以下代码运行该脚本

Classes.Functions.joinDomain();

string strCmdText = @"C:\Windows\Temp\Test.ps1";

System.Diagnostics.Process.Start("C:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe ", strCmdText);

如果我从 Powershell ISE 运行脚本,它会提示输入密码,以便脚本运行。

即使调用它也可以,但我只是得到了蓝色的 powershell 命令行,然后它解散了它不会要求输入密码,我不知道为什么。

任何想法将不胜感激?

【问题讨论】:

  • 我建议您改为关注 this 从 C# 调用 PS。
  • @Mark 不,我尝试过,但它不适用于我的情况。所以我写了函数(它没有完成,但现在只是为了测试目的)来创建powershell脚本而不是运行它们。其他脚本适用于我的解决方案,但 powershell 中的此功能应在 powershell shell 中提示输入密码,但它只是不这样做。它只是分散剂。

标签: c# windows powershell dns


【解决方案1】:

找到解决方案:

这是由于 Windows 对未签名脚本的限制政策造成的。

解决办法:

https://github.com/eapowertools/ReactivateUsers/wiki/Changing-Execution-Signing-Policy-in-Powershell

【讨论】:

    猜你喜欢
    • 2012-09-04
    • 2018-11-26
    • 2015-08-14
    • 2021-11-30
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    相关资源
    最近更新 更多