The situation: You have a PowerShell 3.0 script that you want to run
every day, or every week. Alternatively, you want to
execute PowerShell 3.0 commands after a particular event occurs.
My advice is don't re-invent the wheel, run your PowerShell script with the
built-in
Windows Task Scheduler.
This is just an example PowerShell script for the Task Scheduler. While the
script is feeble and ripe for modification, at least it produces output in
the form text file containing a list of processes.
Before you ask the Task Scheduler to run this code I would test by
running the command in the PowerShell ISE. In particular I would
like to see that SchedulePr.txt was created and take note of its Date
Modified.
I use the Windows 'Search' dialog box for finding the built-in Windows
Task Scheduler. Once launched seek the Actions pane then Create Task
(Swerve the basic task.)
General: Give the task a name, e.g. GuyProc. Whilst at this tab
check the options.
Trigger: We will fire the PowerShell script at a specified time,
e.g. 12:30 every day.
Actions: This is where we instruct the scheduler what to do when the trigger
fires.
We have reached the crucial stage, the key point is to type just
'PowerShell' in the Program/script dialog box. See screenshot below right.
Mistake: Don't put the name of the script in the Program/script box,
just leave one instruction here 'PowerShell'.
Add arguments: This is where you tell PowerShell what to
through
parameters: -File D:\PShell\ProcessTask.ps1
or better enclosed with double quote speech marks
-File "D:\PShell\Process Task.ps1"
The " Double Speech Marks" are required if the path has spaces.
Incidentally, ' Single speech marks' don't cut it here.
-ExecutionPolicy Bypass will ensure that the
PowerShell scheduled task will run even thought the computer has a
restricted script policy. However, there is a secret, and that is to
ensure this parameter is at the beginning of the arguments thus:
-ExecutionPolicy Bypass -File "D:\PShell\Process Task.ps1"
What's the name of your PowerShell .ps1 file?
(D:\PShell\ProcessTask.ps1)
Where to look for the output file? ($Location =
"C:\SchedulePr.txt")
Name of the Task (GuyProc)
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, SolarWinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
I hope these help you, in particular, you may need the -ExecutionPolicy
bypass parameter.
-ExecutionPolicy Sets the default execution policy for the current
session.
-File Runs the specified script in the local scope, so that the
functions and variables that the script creates are available in the
current session. Enter the script file path and any parameters. File must
be the last parameter in the command, because all characters typed after
the File parameter name are interpreted as the script file path followed
by the script parameters.
-NoLogo Hides the copyright banner at startup.
-NonInteractive Does not present an interactive prompt to the user.
-NoProfile Does not load the Windows PowerShell profile.
-Version Starts the specified version of Windows PowerShell. Enter a
version number with the parameter, such as "-version 1.0".
-WindowStyle Sets the window style to Normal, Minimized, Maximized
or Hidden.
-Command Executes the specified commands (and any parameters) as
though they were typed at the Windows PowerShell command prompt, and then
exits, unless NoExit is specified. The value of Command can be "-", a
string. or a script block.
If the value of Command is "-", the command text is read from standard
input.
If the value of Command is a string, Command must be the last parameter
in the command , because any characters typed after the command are
interpreted as the command arguments.
If you want to run a PowerShell 3.0 script run
every day, or every week then don't re-invent the wheel, call for the
assistance of the
Windows Task Scheduler. Remember that the name of the Script /
Program is PowerShell, the actual script is referenced by the -file
parameter.
If you like this page then please share it with your friends
Please email me if you have a better script examples. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, SolarWinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.