changing permissions on a registry key and other bits from within VB
-
sabato 28 aprile 2012 21:28
Hi all
I am trying to write an app to automate several things that I have to do on a new computer once I have deployed an image, hoping someone could give me some pointers for them:-
1) changing computer description - wmi perhaps?
2) changing computer name - again wmi perhaps?
3) changing permissions on an existing registry key to full control
4) changing the "time to display list of operating systems" when booting from 30 secs to 3
5) turn off simple file sharing
6) and setting current user account so that password doesn't expire - at moment I do it from cmd prompt and do "wmic useraccount where "name='name'" set passwordexpires = false
7) change power plan settings so put the computer to sleep is "never" on both battery and mains
thanks
Darren Rose
- Modificato wingers sabato 28 aprile 2012 21:38
Tutte le risposte
-
lunedì 30 aprile 2012 11:08Moderatore
Hi Wingers,
1) changing computer description - wmi perhaps?
This value is in the key System\CurrentControlSet\Services\lanmanserver\parameters. So you can change this key value. If there is no such key, please create one.
2) changing computer name - again wmi perhaps?
This value is in the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName. you can change this one.
3) changing permissions on an existing registry key to full control
Please take a look at this similar thread: http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/ad8d0c0c-e9cd-4730-8538-1d3d8c3f67b2/
And this class should be also helpful: http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.setaccesscontrol.aspx
http://msdn.microsoft.com/en-us/library/system.security.permissions.registrypermission.aspx
4) changing the "time to display list of operating systems" when booting from 30 secs to 3
Please take a look at this similar thread: http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/aac05ae8-2978-46e2-8f7f-00a2b78fb03e/
5) turn off simple file sharing
Please take a look at this similar thread: http://social.msdn.microsoft.com/Forums/en-US/windowssdk/thread/00b2df7c-bb40-4ec5-9107-b26fd676b1a7
6) and setting current user account so that password doesn't expire - at moment I do it from cmd prompt and do "wmic useraccount where "name='name'" set passwordexpires = false
This documentation should give you some ideas: http://gallery.technet.microsoft.com/scriptcenter/10dae29d-5d1d-44d8-9462-4d61f5bb856c
7) change power plan settings so put the computer to sleep is "never" on both battery and mains
This documentation should be what you are looking for: http://msdn.microsoft.com/en-us/library/hxkc1kwd.aspx
I hope this will be helpful.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Contrassegnato come risposta wingers venerdì 4 maggio 2012 16:16
-
lunedì 30 aprile 2012 15:19
Thanks Mike
1) and 2) done
3) will try later
4) read through posted link, but still doesn't really help me achieve it
5) looked at link and checked value in registry - doesn't seem to be relevant, as whether "use sharing wizard" is enabled or disabled - value is always 0 - UPDATE: found correct value under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\SharingWizardOn
6) have worked out how to find if enabled/disabled , but how can I then change a value using wmi:-
Dim lbpasswordexpire_query As String = "SELECT * FROM Win32_UserAccount" Dim lbpasswordexpire_searcher As New ManagementObjectSearcher(lbpasswordexpire_query) For Each info As ManagementObject In lbpasswordexpire_searcher.Get() If info.Properties("Name").Value = "lblocal" Then tblbpasswordexpires.Text = info.Properties( "PasswordExpires").Value.ToString() End If
Next info
7) this only seems to cover getting "powerstate" - can't find anything to do with being able to change power plan settings
Darren Rose
-
martedì 1 maggio 2012 09:12Moderatore
Hi Wingers,
4)
Yes, it is not VB.net code, but it shows you which WMI class is useful for this issue. Any way, please try this code:
Dim p As New Process Dim pi As New ProcessStartInfo pi.FileName = "bcdedit" pi.Arguments = "/timeout 12" pi.RedirectStandardOutput = True pi.RedirectStandardError = True pi.UseShellExecute = False pi.CreateNoWindow = False p.StartInfo = pi p.Start() Console.WriteLine(p.StandardOutput.ReadToEnd) p.WaitForExit()
You need to run it as Administrator.
6) Please try this code, and run it as administrator:
Private Sub ChangePasswordExpires() Dim lbpasswordexpire_query As String = "SELECT * FROM Win32_UserAccount where name = 'TestUser' and localAccount = True" Dim lbpasswordexpire_searcher As New ManagementObjectSearcher(lbpasswordexpire_query) For Each info As ManagementObject In lbpasswordexpire_searcher.Get() Console.WriteLine(info.Properties("PasswordExpires").Value.ToString()) 'info.Properties("PasswordExpires").Value = False info.Scope.Options.EnablePrivileges = True info.SetPropertyValue("PasswordExpires", False) Dim putO As New PutOptions() putO.Type = PutType.UpdateOrCreate info.Put(putO) Next info End Sub
7)
Try this similar command lines with question 4:
c:\windows\system32\powercfg.exe -change -monitor-timeout-ac 0 c:\windows\system32\powercfg.exe -change -monitor-timeout-dc 0 c:\windows\system32\powercfg.exe -change -disk-timeout-ac 0 c:\windows\system32\powercfg.exe -change -disk-timeout-dc 0 c:\windows\system32\powercfg.exe -change -standby-timeout-ac 0 c:\windows\system32\powercfg.exe -change -standby-timeout-dc 0 c:\windows\system32\powercfg.exe -change -hibernate-timeout-ac 0 c:\windows\system32\powercfg.exe -change -hibernate-timeout-dc 0
This is what the changes you have to do when you disabled the sleep option.
I hope this will be helpful.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Contrassegnato come risposta wingers venerdì 4 maggio 2012 16:16
-
martedì 1 maggio 2012 11:374) code doesn't work for me - ran as admin which my app does anyway - and get win32exception - the system cannot find the file specified on the p.start() line
Darren Rose
-
martedì 1 maggio 2012 11:50Moderatore
Hi Wingers,
I have test the code like this:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim p As New Process Dim pi As New ProcessStartInfo pi.FileName = "bcdedit" pi.Arguments = "/timeout 12" pi.RedirectStandardOutput = True pi.RedirectStandardError = True pi.UseShellExecute = False pi.CreateNoWindow = False p.StartInfo = pi p.Start() Console.WriteLine(p.StandardOutput.ReadToEnd) p.WaitForExit() End Sub
And what is yours? And what is the win32Exception message?
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
martedì 1 maggio 2012 12:00
6) works fine - thanks
Darren Rose
-
martedì 1 maggio 2012 12:02
Hi Wingers,
I have test the code like this:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim p As New Process Dim pi As New ProcessStartInfo pi.FileName = "bcdedit" pi.Arguments = "/timeout 12" pi.RedirectStandardOutput = True pi.RedirectStandardError = True pi.UseShellExecute = False pi.CreateNoWindow = False p.StartInfo = pi p.Start() Console.WriteLine(p.StandardOutput.ReadToEnd) p.WaitForExit() End Sub
And what is yours? And what is the win32Exception message?
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
My code is same - but just when button clicked rather than form load
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim p As New Process Dim pi As New ProcessStartInfo pi.FileName = "bcdedit" pi.Arguments = "/timeout 12" pi.RedirectStandardOutput = True pi.RedirectStandardError = True pi.UseShellExecute = False pi.CreateNoWindow = False p.StartInfo = pi p.Start() Console.WriteLine(p.StandardOutput.ReadToEnd) p.WaitForExit() End Subthe error as I said above is win32exception - "the system cannot find the file specified" and it occurs on the p.Start() line
full exception code:-
System.ComponentModel.Win32Exception was unhandled
ErrorCode=-2147467259
HResult=-2147467259
Message=The system cannot find the file specified
NativeErrorCode=2
Source=System
StackTrace:
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at LB_Imaging_App.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\Darren Rose\Documents\Visual Studio 11\Projects\LB Imaging App\LB Imaging App\Form1.vb:line 78
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at LB_Imaging_App.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:Darren Rose
-
martedì 1 maggio 2012 12:33Moderatore
Hi Darren,
Please check this documentation: http://technet.microsoft.com/en-us/library/cc709667(v=ws.10).aspx
BCDEdit is the primary tool for editing the boot configuration of Windows Vista and later versions of Windows. It is included with the Windows Vista distribution in the %WINDIR%\System32 folder.
If your system is Windows Vista and later versions of Windows, you can also check is there such a file named bcdedit in %WINDIR%\System32.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
martedì 1 maggio 2012 12:33
3) changing permissions on an existing registry key to full control
Please take a look at this similar thread: http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/ad8d0c0c-e9cd-4730-8538-1d3d8c3f67b2/
And this class should be also helpful: http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.setaccesscontrol.aspx
http://msdn.microsoft.com/en-us/library/system.security.permissions.registrypermission.aspx
Tried playing about with the code on above links , but stuggling to get anything to do what I want
Basically need to change users to full control (rather than default read) to this key:-
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing
Darren Rose
-
martedì 1 maggio 2012 13:55
Hi Darren,
Please check this documentation: http://technet.microsoft.com/en-us/library/cc709667(v=ws.10).aspx
BCDEdit is the primary tool for editing the boot configuration of Windows Vista and later versions of Windows. It is included with the Windows Vista distribution in the %WINDIR%\System32 folder.
If your system is Windows Vista and later versions of Windows, you can also check is there such a file named bcdedit in %WINDIR%\System32.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
very odd then - as it is Windows 7 and bcdedit.exe is located in correct place - so not sure why it is not working
i get same error if I just use "System.Diagnostics.Process.Start("c:\windows\system32\bcdedit.exe /timeout 12")
UPDATE
It is some sort of path issue - because if I copy bcdedit into the apps bin/debug folder then it works fine - so any ideas anyone as to why despite me telling it to look in c:\windows\system32 it is not?
Darren Rose
-
mercoledì 2 maggio 2012 03:33Moderatore
Hi Darren,
>>It is some sort of path issue - because if I copy bcdedit into the apps bin/debug folder then it works fine - so any ideas anyone as to why despite me telling it to look in c:\windows\system32 it is not?
Please check the environment variables "path", is there a path "%SystemRoot%\system32;"?
You can view or modify the environment variables by selecting System from the Control Panel, selecting Advanced system settings, and clickingEnvironment Variables.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
mercoledì 2 maggio 2012 09:15
Hi Darren,
>>It is some sort of path issue - because if I copy bcdedit into the apps bin/debug folder then it works fine - so any ideas anyone as to why despite me telling it to look in c:\windows\system32 it is not?
Please check the environment variables "path", is there a path "%SystemRoot%\system32;"?
You can view or modify the environment variables by selecting System from the Control Panel, selecting Advanced system settings, and clickingEnvironment Variables.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
yes that path exists in my environment variablesDarren Rose
-
mercoledì 2 maggio 2012 09:18
Hi,
In addition to current answers you could also try to ask in a Windows administration group. AFAIK you should have some deployment and administrative tools allowing to do all that and much more rather than coding it yourself.
Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
-
mercoledì 2 maggio 2012 21:30
Hi Darren,
>>It is some sort of path issue - because if I copy bcdedit into the apps bin/debug folder then it works fine - so any ideas anyone as to why despite me telling it to look in c:\windows\system32 it is not?
Please check the environment variables "path", is there a path "%SystemRoot%\system32;"?
You can view or modify the environment variables by selecting System from the Control Panel, selecting Advanced system settings, and clickingEnvironment Variables.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
found the problem - I had a correct path entry for system32 under system variables - but for some reason there was a blank path entry under user variables - I removed this and now works fine
Thanks Mike
Can someone help further please with my other post above:-
3) changing permissions on an existing registry key to full control
Please take a look at this similar thread: http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/ad8d0c0c-e9cd-4730-8538-1d3d8c3f67b2/
And this class should be also helpful: http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.setaccesscontrol.aspx
http://msdn.microsoft.com/en-us/library/system.security.permissions.registrypermission.aspx
Tried playing about with the code on above links , but stuggling to get anything to do what I want
Basically need to change users to full control (rather than default read) to this key:-
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSLicensing
Darren Rose
Darren Rose
- Modificato wingers mercoledì 2 maggio 2012 21:30
-
mercoledì 2 maggio 2012 21:32
Hi,
In addition to current answers you could also try to ask in a Windows administration group. AFAIK you should have some deployment and administrative tools allowing to do all that and much more rather than coding it yourself.
Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
Hi Patrice
Yes I expect there are plenty of tools that will do that and lots more - but this tool is needed to do these specific things and other functions that I have already coded - most of which are specific to the company rather than generic things that other tools would do - thanks for your answer though
Darren Rose
-
giovedì 3 maggio 2012 06:02Moderatore
Hi Darren,
Please try the first sample in the documentation: http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.registrysecurity.aspx#Y6300
Because your requirement is create a full control permission, so change the code from
RegistryRights.ReadKey
to
RegistryRights.FullControlI hope this will be helpful.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
giovedì 3 maggio 2012 11:14
Thanks - had already read that article and cannot see how it helps - as I need to set permission for all users "users" to full control for that key - all the samples show changing it for current user not the users groupHi Darren,
Please try the first sample in the documentation: http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.registrysecurity.aspx#Y6300
Because your requirement is create a full control permission, so change the code from
RegistryRights.ReadKey
to
RegistryRights.FullControlI hope this will be helpful.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Darren Rose
-
sabato 5 maggio 2012 14:03
2) changing computer name - again wmi perhaps?
This value is in the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName. you can change this one.
Thought this was working, but as soon as I reboot the computer name changes back to the previous name
Any ideas?
Darren Rose

