Search code examples
c#.netprocess.startwmicsecurestring

Process.start() WMIC.exe password issue


I am trying to run WMIC as Admin via my program, using the following code works, but only if WMIC.exe is allready run as an administrator, otherwise it will just return an empty HTML. I can't seem to find a related issue on stackoverflow or elsewhere... Does anyone see the issue here?

My method for converting the securestring:

SecureString secureString = new SecureString();

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

secureString.MakeReadOnly();
return secureString;

Startcode:

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");

Solution

  • Found the problem, this was due to a Win7 bug of WMIC. To fix this, you can copy all the *.xsl files of C:\Windows\System32\wbem\en-US to the application startup path (or elsewhere) and link to it like this:

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

    Possible dupe for wmic error (invalid XSL format) in windows7