【发布时间】: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