0
var processStartInfo = new ProcessStartInfo();
        processStartInfo.WorkingDirectory = @"c:\temp";
        processStartInfo.FileName = "cmd.exe";
        processStartInfo.UseShellExecute = false;
        processStartInfo.RedirectStandardInput = true;
        // set additional properties     
        Process proc = Process.Start(processStartInfo);

        //process.StandardInput.WriteLine("c:");
        proc.StandardInput.WriteLine("powershell -ExecutionPolicy Bypass c:\\temp\\autologin.ps1");
        proc.StandardInput.Close();

Powershell

$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"  
Set-ItemProperty $RegPath "AutoAdminLogon" -Value "0" -type String  
Set-ItemProperty $RegPath "DefaultUsername" -Value " " -type String  
Set-ItemProperty $RegPath "DefaultPassword" -Value " " -type String

I've been trying to run a powershell script that my program writes. I can't just run the powershell commands in the program because I will be rebooting the computers and accessing that powershell program again.

Any ideas? Currently it will run the commands but not make the changes to the registry but if i enter the commands myself into CMD it works perfectly. So I'm a little stuck the only thing I need is for C# to run that powershell script for me.

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon reg keys for reference

NEW EDIT: program is executing but putting the registry keys in WOW6432Node

Sanket
  • 19,295
  • 10
  • 71
  • 82
  • What registry keys are you modifying? Do they require admin rights to access them? – mageos May 13 '17 at 23:43
  • proc.StandardInput.WriteLine("reg add " + c + "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon" + c + " /v AutoAdminLogon /t REG_SZ /d 0 /f"); proc.StandardInput.WriteLine("reg add " + c + "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon" + c + " /v DefaultUserName /t REG_SZ /d " + username + " /f"); proc.StandardInput.WriteLine("reg add " + c + "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon" + c + " /v DefaultPassword /t REG_SZ /d " + "yes" + " /f"); proc.StandardInput.WriteLine("Timeout 10"); – Justin Trantham May 13 '17 at 23:48
  • Thats the Batch version of it the powershell version is different however I just changed the way i wrote it to not be running the script but instead running the commands seperatly and i get success however still no changes to the registry. – Justin Trantham May 13 '17 at 23:50
  • Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon reg keys for reference – Justin Trantham May 13 '17 at 23:50
  • Hi, welcome to StackOverflow. You could do your readers a favour by updating your post to include information about the keys you are trying to change. Perhaps even include a copy of your script (or better yet a shorter test script that reproduces your problem and only affects one registry key). Yes they can read these comments for themselves, but why make them work harder than necessary? See [ask] and [mcve] for more helpful hints on how to ask good questions. – Frank Boyne May 13 '17 at 23:56
  • I don't believe it is a firewall issue, unless you are attempting to execute the commands on a remote machine. Have you confirmed that the script is executing? It doesn't look like it is outputing any trace messages. – mageos May 14 '17 at 02:00
  • Yes it is executing actually the issue lies in the fact its running in 64 bit mode and its putting the registry keys in WOW6432Node of the registry – Justin Trantham May 14 '17 at 02:24

3 Answers3

1
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 0 /reg:64 /f
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultUserName /t REG_SZ /d USERNAME /reg:64 /f
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /t REG_SZ /d PASSWORD /reg:64 /f

went with a batch solution after debugging found i could just edit the way i wrote the file. Now it runs perfectly thanks for your help!

0

It sounds like the issue is that your application needs admin rights to access those keys. With Windows 7 and newer and UAC enabled, even if the logged in user has admin rights, the processes will not have admin rights unless they request it.

This article: How to force my C# Winforms program run as administrator on any computer?

Should help you setup your project so that it runs with admin rights.

Community
  • 1
  • 1
mageos
  • 1,216
  • 7
  • 15
  • Sadly I have already made the changes to the app manifest to and the problem isn't CMD not running as admin i Debugged it running as admin and actually wrote my commands into the CMD propt it opened and they ran fine. However my program is on my E: drive and not my main C: i moved it and still same results. – Justin Trantham May 13 '17 at 23:57
0

You should to build your application on X64 platform target and then run with admin rights. refer to this image

Alisher
  • 1
  • 1