Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Run Powershell script on startup before user log on

Posted on 2011-09-09
2
Medium Priority
?
1,928 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 2000 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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

A safe way to clean winsxs folder from your windows server 2008 R2 editions
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
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 walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

688 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