Solved

Powershell script that runs at first boot only

Posted on 2016-11-03
7
64 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 69

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 7

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 7

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 84

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 84

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 69

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 7

Author Closing Comment

by:K B
ID: 41873300
Perfect!!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

733 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