[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Powershell script that runs at first boot only

Posted on 2016-11-03
7
Medium Priority
?
148 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 71

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 8

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 8

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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 85

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 85

Accepted Solution

by:
oBdA earned 2000 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 71

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 8

Author Closing Comment

by:K B
ID: 41873300
Perfect!!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

872 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