Process.start() WMIC.exe 密码问题

Process.start() WMIC.exe password issue

我正在尝试通过我的程序 运行 WMIC 作为管理员,使用以下代码有效,但前提是 WMIC.exe 已经准备好 运行 作为管理员,否则它只会return 空 HTML。我似乎无法在 Whosebug 或其他地方找到相关问题...有人在这里看到这个问题吗?

我的安全字符串转换方法:

SecureString secureString = new SecureString();

foreach (char ch in str)
{
    secureString.AppendChar(ch);
}

secureString.MakeReadOnly();
return secureString;

开始代码:

string path = @"C:\Temp\";

if (!Directory.Exists(path))
{
    Directory.CreateDirectory(path);
}

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

startInfo.UseShellExecute = false;
startInfo.FileName = "cmd";
startInfo.Domain = "EU";
startInfo.Verb = "runas";

startInfo.UserName = "Username";
startInfo.Password = GetSecureString("Password");

startInfo.Arguments = @"/k wmic.exe /node: " + "\"" +   txt_input_computers.Text + "\" " + "/output:" + path + @"\" + txt_input_computers.Text + ".html " + DDL_WMIC.Text
          + " list full /format:hform";

process.StartInfo = startInfo;
process.Start();

process.WaitForExit();
Process.Start(path + @"\" + txt_input_computers.Text + ".html");

找到问题了,这是WMIC的一个Win7 bug。要解决此问题,您可以将 C:\Windows\System32\wbem\en-US 的所有 *.xsl 文件复制到应用程序启动路径(或其他地方),然后将 link 复制到它,如下所示:

        startInfo.Arguments = @"/k wmic.exe /node: " + "\"" + "computername" + "\" " + "/output:" + @"C:\Temp\outputfile.html " + "wmiattrib" + " list full /format:\"" + Application.StartupPath + "\hform\"";

可能是 wmic error (invalid XSL format) in windows7

的骗局