Run a script on the *second* boot?

Status
Not open for further replies.

RojBlake

Ars Legatus Legionis
48,129
Subscriptor
I want to programmatically join a newly deployed Windows VM (VMware) to a domain. I have a simple PowerShell script that will do this, but there's a problem: I need the script to run the second time the VM reboots. Why? Well, the deployment process goes like this:

- Initiate deployment from template
- OS starts, gets to the login screen, then reboots, initiating sysprep to change the hostname (and a bunch of other things)
- OS starts again and automatically logs in to the "Administrator" account
- It's at this point, post-hostname change, that I want the script to run

Any way to do that?
 

RojBlake

Ars Legatus Legionis
48,129
Subscriptor
[url=http:/civis/viewtopic.php?p=26283073#p26283073:pkj4164y said:
wallinbl[/url]":pkj4164y]Just put an indicator somewhere of the boot sequence - first time it runs, there's no indicator, so it writes the indicator. The second time, it finds the indicator, does its stuff and writes something to say it's done. All subsequent times see the "done" indicator and do nothing.
This seems like the way to go. Any idea how to get a script to write to itself in PowerShell? (I've only plinked around with it a couple of times. I don't know a whole lot about the language, it must be said!)
 

CanSpice

Ars Tribunus Angusticlavius
7,147
[url=http:/civis/viewtopic.php?p=26287455#p26287455:2x5j202r said:
hux[/url]":2x5j202r]
[url=http:/civis/viewtopic.php?p=26283073#p26283073:2x5j202r said:
wallinbl[/url]":2x5j202r]Just put an indicator somewhere of the boot sequence - first time it runs, there's no indicator, so it writes the indicator. The second time, it finds the indicator, does its stuff and writes something to say it's done. All subsequent times see the "done" indicator and do nothing.
This seems like the way to go. Any idea how to get a script to write to itself in PowerShell? (I've only plinked around with it a couple of times. I don't know a whole lot about the language, it must be said!)
No need to write to itself. Just drop a zero-byte "first-boot-complete" file somewhere on disk when your script has run the first time.
 

Jehos

Ars Legatus Legionis
55,560
[url=http:/civis/viewtopic.php?p=26282503#p26282503:2it34n84 said:
hux[/url]":2it34n84]I want to programmatically join a newly deployed Windows VM (VMware) to a domain. I have a simple PowerShell script that will do this, but there's a problem: I need the script to run the second time the VM reboots. Why? Well, the deployment process goes like this:

- Initiate deployment from template
- OS starts, gets to the login screen, then reboots, initiating sysprep to change the hostname (and a bunch of other things) As part of the "bunch of other things", copy down your PowerShell script to the machine and write to RunOnce that you want it to run.
- OS starts again and automatically logs in to the "Administrator" account
- It's at this point, post-hostname change, that I want the script to run RunOnce will handle running your script

Any way to do that?
Yep, listed above. You could build your own flag, but MS already handled that for you with RunOnce.

The trick is just writing to the RunOnce key after the first reboot.

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
 

sryan2k1

Ars Legatus Legionis
46,882
Subscriptor++
Uh, just have VMWare join the machine to the domain?

xF0s63K.png
 
Status
Not open for further replies.