Solved

Run Powershell script on startup before user log on

Posted on 2011-09-09
2
1,827 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
larry urban 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Redirected folders in a windows domain can be quite useful for a number of reasons, one of them being that with redirected application data, you can give users more seamless experience when logging into different workstations.  For example, if a use…
OfficeMate Freezes on login or does not load after login credentials are input.
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
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…
Suggested Courses

627 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