Solved

Run Powershell script on startup before user log on

Posted on 2011-09-09
2
1,652 Views
Last Modified: 2012-05-12
I have a PowerShell script which I used to add machine to a domain.  The purpose is to allow me to snap a VM to a clean state, before machine has been added to a domain.

I would like to be able to setup the snapshot of the VM so that when it starts up, it automatically runs the PowerShell script.
I tried populating the following registry key, but this only works after user logs on.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce

I then tried the following registry key, but nothing seems to happen:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServicesOnce

I also tried creating a scheduled task to call the script, and configured the task to run on start up, but nothing happen.

I think with the last two approach, the script is getting called too early, before a required service is loaded.

I know the script works, because it works when I call it on command line, and it works when I use the log on method (RunOnce) registry key.
Here's the PowerShell script:
$domainName="mydomain"
$domainPw="mypassword"
$domainAcc=$domainName + "\MyAdminUserName"

$comp = get-wmiobject Win32_ComputerSystem
$comp.JoinDomainOrWorkGroup($domainName,$domainPw,$domainAcc,$null,3)

$reboot = (gwmi -Class Win32_OperatingSystem)
$reboot.psbase.scope.options.enableprivileges = $true
$reboot.reboot()

Open in new window


Here's the DOS script which is used to call the PowerShell script:
rem schtasks /Delete /TN AddMachineToTestlabDomain
rem Above is only needed when using task schedule method
powershell Set-ExecutionPolicy RemoteSigned
powershell c:\AddMachineToTestlabDomain.ps1

Open in new window



Does anyone know how I can configure Windows 2008 machine to automatically get added to a domain on bootup without requiring user to log on?
Or how to call above script on bootup without requiring user to log on?
0
Comment
Question by:Axter
2 Comments
 
LVL 7

Accepted Solution

by:
Todar earned 500 total points
ID: 36512626
set the VM to auto login as the local admin on the workstation.

create a .reg file with the following in it: (Edit it to have your administrators password....in quotes)

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]

"DefaultUserName"="Administrator"
"Defaultpassword"="<put your password here>"
"AutoAdminLogon"="1"

run this on your VM before you snapshot it.

It will then boot up as the admin, your registry entries will run and run your script. you can then add to your powershell script to take out this auto login info from the registry so there are no traces of the admin password for prying eyes.
0
 
LVL 30

Author Comment

by:Axter
ID: 36513480
Not all the VM's have the same local administrative password, so I wouldn't be able to create a single script to configure the VM's.

I currently have a script to copy the PowerShell and batch file to the VM.
The script also adds the registry key settings.

Then I take a snapshot of the VM.
If I have to use method with local password, it wouldn't work on all of the VM's.

If I don't get a better posted method, I'll still this approach.
0

Featured Post

Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Join & Write a Comment

Scenario:  You do full backups to a internal hard drive in either product (SBS or Server 2008).  All goes well for a very long time.  One day, backups begin to fail with a message that the disk is full.  Your disk contains many, many more backups th…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now