Solved

Powershell script that runs at first boot only

Posted on 2016-11-03
7
78 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
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 70

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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
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 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 70

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

687 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