Solved

Powershell script that runs at first boot only

Posted on 2016-11-03
7
30 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Too many email signature changes to deal with?

Are you constantly being asked to update your organization's email signatures? Do they take up too much of your time? Wouldn't you love to be able to manage all signatures from one central location, easily design them and deploy them quickly to users. Well, you can!

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

932 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

8 Experts available now in Live!

Get 1:1 Help Now