Search code examples
c#powershellprocess.start

Powershell command works not on c#


I tried to rename a mapped Network Drive with Powershell. Run the commands in Powershell Script worked it. But when i try with a Powershell Process in c# Comes an Error: Argument cant be NULL. Have someone any idea whats going wrong with my Code?

Process p2 = new Process();
p2.StartInfo.FileName = "powershell.exe";
p2.StartInfo.Verb = "runas";
p2.StartInfo.RedirectStandardOutput = true;
p2.StartInfo.RedirectStandardError = true;
p2.StartInfo.UseShellExecute = false;
p2.StartInfo.CreateNoWindow = true;
p2.StartInfo.Arguments = "$sh=New-Object -com Shell.Application" + " sh.NameSpace('" + webDrive + "').Self.Name = '" + newLabel + "'";   
p2.Start();
string errors = p2.StandardError.ReadToEnd();
MessageBox.Show(errors);

Solution

  • May be (you have forgot $ on sh variable and semicolon between 2 instructions) :

    p2.StartInfo.Arguments = "$sh=New-Object -com Shell.Application;" + " $sh.NameSpace('" + webDrive + "').Self.Name = '" + newLabel + "'";