Solved

Run Powershell script on startup before user log on

Posted on 2011-09-09
2
1,695 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SYSLOG message format 3 47
vmdk greater than 2TB 2 57
Server 2012 R2 TLS 1.2? 2 41
Server 2008 R2 and Windows 10 Admin Templates 7 38
I was asked if I could set up a fax machine so that incoming faxes were delivered to people's Exchange inboxes and so that they could send faxes from their desktops without needing to print the document first.  I knew it was possible but I had no id…
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

777 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