Solved

Powershell script that runs at first boot only

Posted on 2016-11-03
7
24 Views
Last Modified: 2016-11-03
I would like a Powershell script to run at initial boot only please. - as an administrator.
Once, it runs successfully, I would like some sort of flag incremented so that it will not run again.

This is not Domain Joined, so GPOs are not in the picture.

Thank you!
0
Comment
Question by:K B
  • 3
  • 2
  • 2
7 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 41872507
Most people create a flag file somewhere, or create a registry entry, to show the script has been run. The script then only needs to check for existence to terminate execution. That part is not difficult at all.

How do you think you can get the script running on a boot in the first place?
0
 
LVL 5

Author Comment

by:K B
ID: 41872513
thank you for your reply Qlemo!

That makes sense.  So how would I fire off the script at boot (in your opinion).
0
 
LVL 5

Author Comment

by:K B
ID: 41872518
I am realizing that I would like this to run before anyone logs in.  Is that possible?
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 83

Expert Comment

by:oBdA
ID: 41872541
That can be done with a scheduled task. Pick "Create a Basic Task" from the menu, set it to start "When the computer starts".
As Program/Script, use "Powershell.exe", as arguments (obviously adjusting the path inside the single quotes)
-ExecutionPolicy Bypass -Command "& 'C:\Wherever\script.ps1'"

Open in new window

In the "Finish" dialog, check "Open the Properties dialog ...", click "Finish", then set the user and password (you can use SYSTEM as well, no password), change the radio button to "Run whether user is logged on or not" (if not using SYSTEM), and check the box "Run with highest privileges".
If someone's fast with the logon, he might make it before it runs, but unless he's using the same account you used for the task, he won't see anything, so the success depends on what it is you want to achieve with the script.
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41872620
This creates a registry key and sets a REG_DWORD "RunCount" and a REG_SZ value "RunLog", so you can check whether it actually did something.
$Key = 'HKLM:\Software\Acme'
If ($RunCount = (Get-ItemProperty -Path $Key -ErrorAction SilentlyContinue).RunCount) {
	$Log = "Script started at $(Get-Date); left without changes."
} Else {
	$Log = "Script started for the first time at $(Get-Date)."
	New-Item -ItemType Leaf -Path $Key | Out-Null
	$RunCount = 0
}
Set-ItemProperty -Path $Key -Name 'RunCount' -Value ($RunCount + 1)
Set-ItemProperty -Path $Key -Name 'RunLog' -Value $Log
Write-Host -Object $Log
If ($RunCount -ne 0) {Exit 0}
Write-Host -Object 'Starting the magic ...'

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 41872787
Instead of the reg key we could remove or disable the scheduled task after execution, which I would prefer.
0
 
LVL 5

Author Closing Comment

by:K B
ID: 41873300
Perfect!!
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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Companies keep a much closer eye on costs today, so changing to new Technology – Microsoft Office 365 is the smartest move to take.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

705 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

22 Experts available now in Live!

Get 1:1 Help Now