Powershell script scheduled task output to log file - powershell

I have Powershell script with some Write-Host but I want to use it as scheduled task.
I added it as scheduled task with this:
$action = New-ScheduledTaskAction -Execute "$pshome\powershell.exe -WindowStyle Hidden" -Argument "-File $FilePath"
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).Date -RepetitionInterval $repeat
Register-ScheduledTask -TaskName $Name -Trigger $trigger -RunLevel Highest -Action $action
Can I make it write to log file? Is it possible to make it write to file at the same location as the Powershell script?

There were 2 problems with the script and the 1st was my main problem:
if it is not signed default behavior is to be sckipped by the Task Scheduler (or at least I didn't saw an error in the Event Viewer).
I had to use Write-Output
Check if the script is signed with Get-AuthenticodeSignature. Here you are going to receive SignatureStatus as string:
$isSigned = Get-AuthenticodeSignature -FilePath $FilePath | select -ExpandProperty Status
If it is not signed, you can enable the execution of it by adding -ExecutionPolicy Bypass like it is described in this spiceworks how-to. Do this for your own scripts, because of security !
For the writing part I used Write-Output and Out-File:
(
...
Write-Output "some text here"
...
) | Out-File -FilePath "C:\somepath\log.log

Related

Scheduled task created in Powershell runs but does not run Powershell script?

I have a PS script that runs just fine when run manually, but I am trying to set it to run daily and can't seem to figure out why it won't run. The scheduled task itself will start, but the script doesn't actually seem to run. Any help would be appreciated.
This is what I used to create the scheduled task:
$Trigger= New-ScheduledTaskTrigger -At 11:00am –Daily
$User= "NT AUTHORITY\SYSTEM"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File G:\Path\ScriptName.ps1"
Register-ScheduledTask -TaskName "TaskyTask" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force
I created a script on my machine # C:\test\ScriptName.ps1
The script is:
Write-Output "HelloWorld" | Out-File C:\test\HelloWorld.txt
So I can verfiy it's success by seeing if HelloWorld.txt has been created in C:\Test
With a tweak to your code to account for the differnce in my script location:
$Trigger= New-ScheduledTaskTrigger -At 11:00am –Daily
$User= "NT AUTHORITY\SYSTEM"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File C:\Test\ScriptName.ps1"
Register-ScheduledTask -TaskName "TaskyTask" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force
The task appears in task scheduler, I run it and HelloWorld.txt is created, confirming it works. This to me suggests your script is executing, but maybe not doing what you think.
If you change your script to the same simple script above, does it run?

Powershell from batch file not working as expected

Let me start with some background. I work in a school district with about 300 laptops using win 10 pro. We are getting ready for our annual standardized testing which uses a lockdown browser. For whatever reason, the company created a new lockdown browser that does not close MSEdge that is running in the background. If you start the lockdown browser as soon as you log in, MSEdge is not an issue.
The provided solution by the software company is to change the registry entries for the test then change it back after the test. That seems like way too much work to me.
My first choice was to use kiosk mode, but it seems it does not work on win 10 pro. The solution I am working on now is to create a user specifically for the testing and use scheduled task at logon with a command to log off at exit. The basic concept seems to work for this situation, as long as the Laptops are plugged in. I can not seem to get the
New-ScheduledTaskSettingsSet –AllowStartIfOnBatteries –DontStopIfGoingOnBatteries
to take effect. I suspect it has to do with the a missing argument, but I can not find any leads with Google searches.
I am using a batch file to call PowerShell and run the .PS1 file
.bat file
:: this Batch file which is run by double clicking in SMACS_IT account. calls powershell to
start the task_kiosk.ps1. Which starts the proccess of creating the scheduled task for the
user STAAR TEST to run TXSecureBrowser.
Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy
Bypass -File C:\1kiosk\task_kiosk_ps1.ps1' -Verb RunAs}"
.PS1 file
$User = "STAAR TEST"
$Description = "A task created to launch the txsecurebrowser using the kiosk_bat.bat file when
the user STAAR TEST logs in"
$taskName = "Launch TXSecureBrowser"
$taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName }
$action = New-ScheduledTaskAction -Execute 'C:\1kiosk\kiosk_bat.bat'
$trigger = New-ScheduledTaskTrigger -AtLogOn -User "STAAR TEST"
$settings = New-ScheduledTaskSettingsSet –AllowStartIfOnBatteries –DontStopIfGoingOnBatteries
if($taskExists) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
}
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskName -Description
$Description -User $User
Set-ScheduledTask -TaskName $taskName -Settings $settings
Using this code I can log in as the test account and the task starts the staar test as long as the laptop is plugged in. The task does not work if laptop is not plugged in. However, If I enter the command manually in PowerShell running as administrator the task will work if the laptop is not plugged in. Which is what leads me to believe that the .bat file needs another argument.
It's been too many years since I worked with Task Scheduler, so I can't help you directly. But, I still have my working script from the time. We needed a webpage updated with a gate counter every couple of minutes and the script below is how I automated the configuring of the scheduled task.
Strip out everything you don't want, insert a few things you do want, and you should pretty much have it.
.CMD file
<# : BATCH Bootstrap for PowerShell
#ECHO OFF
SETLOCAL
PowerShell -executionpolicy remotesigned -Command "Invoke-Command -ScriptBlock ([scriptblock]::Create($([System.IO.File]::ReadAllText('%~f0')))) -ArgumentList ([string]'%*').split()"
ENDLOCAL
GOTO :EOF
#>
# https://blog.netnerds.net/2015/01/create-scheduled-task-or-scheduled-job-to-indefinitely-run-a-powershell-script-every-5-minutes/
# Change these three variables to whatever you want
$jobname = "Gate Count Web Updater"
$script = "C:\Users\Public\GateCountWebUpdater.cmd"
$repeat = (New-TimeSpan -Minutes 2)
# The script below will run as the specified user (you will be prompted for credentials)
# and is set to be elevated to use the highest privileges.
# In addition, the task will run every 5 minutes or however long specified in $repeat.
$action = New-ScheduledTaskAction –Execute $script
$duration = (New-TimeSpan -Days (365 * 20))
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).Date -RepetitionInterval $repeat -RepetitionDuration $duration
$msg = "Enter the username and password that will run the task";
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)
$username = $credential.UserName
$password = $credential.GetNetworkCredential().Password
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
Register-ScheduledTask -TaskName $jobname -Action $action -Trigger $trigger -RunLevel Highest -User $username -Password $password -Settings $settings

Powershell scheduled script is not running properly

I have a short Powershell script which backup desktop items to D drive
$now = [datetime]::now.ToString('yyyy-MM-dd')
$trigger=new-scheduledtasktrigger -daily -at("08:00am")
$action = new-scheduledtaskaction 'copy "C:\USERS\ADMIN\DESKTOP" "D:\$now" -recurse'
Register-scheduledtask -Action $action -Trigger $trigger -Taskname "dailybackup" -Description "Daily backup"
The code seems valid and scheduled is registered well in scheduler
Get-scheduledtask "dailybackup"
But the task havent executed once but dont know why
anyone knows the solution?
New-ScheduledTaskAction executes commands in the old CMD shell by default. The command you're running doesn't error out because COPY is an ancient CMD built-in. It doesn't actually have the ability to recurse though, so it's not what you want.
To run powershell commands, the task action should execute PowerShell.exe, then have the powershell commands themselves added as arguments like below.
It looks like you want to use the run time date as the folder name $now, so you'll need to re-calculate the date every time it runs,
I also suggest adding -Force to create the target directory as needed,
I'm using here-string formatting #' '# to make quotes and things simpler to handle:
$Action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument #'
-Command Copy-Item -Recurse -Force -Path 'C:\USERS\ADMIN\DESKTOP' -Destination "D:\$([datetime]::Now.ToString('yyyy-MM-dd'))"
'#

Unable to create 2 machine environment variables and execute another command via a Scheduled Task

I would like to create 2 "machine" environment variables and also execute a command via a single Scheduled Task. The scheduled task should immediately run and delete itself. The scheduled task should be executing as the current logged-in desktop user.
Below are the 3 items I need to execute:
[Environment]::SetEnvironmentVariable('AppA', (Get-StartApps -Name 'PowerShell (Tools1)').AppID, 'Machine')
[Environment]::SetEnvironmentVariable('AppB', (Get-StartApps -Name 'PowerShell (Tools2)').AppID, 'Machine')
Export-StartLayout -path 'C:\temp\Orig.xml'
Note: I do NOT want to depend on an external script; which is why I have such a long line of code for $action
When I run the code below, almost always, none of the commands are executed. Very rarely, a single environment variable get's created. If it's impossible to do this reliably in a single task, I'm hoping someone could show me how to do this reliably in 3 separate tasks. It seems even if I do 3 separate tasks, the results are still inconsistent/unreliable.
Code:
$AppA = (Get-StartApps -Name 'Windows PowerShell ISE (x86)').AppID
$AppB = (Get-StartApps -Name 'Outlook').AppID
$action = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-noprofile -windowstyle hidden -command ( [Environment]::SetEnvironmentVariable('AppA', $AppA, 'Machine') ; [Environment]::SetEnvironmentVariable('AppB', $AppB, 'Machine') ; Export-StartLayout -path 'C:\temp\Orig.xml' )"
$trigger = New-ScheduledTaskTrigger -Once -At (get-date).AddSeconds(4); $trigger.EndBoundary = (get-date).AddSeconds(8).ToString('s')
$principal = New-ScheduledTaskPrincipal -UserId (Get-WmiObject -Class win32_computersystem).UserName -LogonType ServiceAccount -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DeleteExpiredTaskAfter 00:00:12
Register-ScheduledTask -TaskName "2 envvars and Export-StartLayout" -Action $action -Trigger $trigger -Principal $principal -Settings $settings

Register scheduled task with New-ScheduledTaskTrigger to trigger on event ID

Register-ScheduledTask with New-ScheduledTaskTrigger on a Windows event ID
Hello Stack-overflow users. Neither MSDN nor Google yields results...
I configured a couple of scheduled tasks via a Powershell script. The scheduled tasks are set to run at certain times.
This all works fine. But I need to configure another scheduled task which run when a certain event ID is logged in the Windows event logger.
I can set this up manually of course but I want it as part of my automated script.
this is the code I have so far for the scheduled tasks, I need to replace the $Trigger= New-ScheduledTaskTrigger -At 4:00am -Daily section:
Copy-Item "\\networkDrive\Backups\scripts\Reset-Sessions.ps1" "c:\scripts\Reset-Sessions.ps1"
$Trigger= New-ScheduledTaskTrigger -At 4:00am -Daily
$User= 'Nt Authority\System'
$Action= New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-executionpolicy bypass -File c:\scripts\Reset-Sessions.ps1"
Register-ScheduledTask -TaskName "Reset-Sessions" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest -Force
I have changed some of the directory and file names for online purposes.
I would appreciate it if somebody could steer me into the right direction or assist with an example.
I would prefer to only change the $Trigger portion and not re-write the whole script but I would understand if it is not possible.
I use Powershell version 5.1.
With this answer as a base and some additional help I was able to construct this script
$taskname="Reset-Sessions"
# delete existing task if it exists
Get-ScheduledTask -TaskName $taskname -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$false
# get target script based on current script root
$scriptPath=[System.IO.Path]::Combine($PSScriptRoot, "Reset-Sessions.ps1")
# create list of triggers, and add logon trigger
$triggers = #()
$triggers += New-ScheduledTaskTrigger -AtLogOn
# create TaskEventTrigger, use your own value in Subscription
$CIMTriggerClass = Get-CimClass -ClassName MSFT_TaskEventTrigger -Namespace Root/Microsoft/Windows/TaskScheduler:MSFT_TaskEventTrigger
$trigger = New-CimInstance -CimClass $CIMTriggerClass -ClientOnly
$trigger.Subscription =
#"
<QueryList><Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational"><Select Path="Microsoft-Windows-NetworkProfile/Operational">*[System[(EventID=4004)]]</Select></Query></QueryList>
"#
$trigger.Enabled = $True
$triggers += $trigger
# create task
$User='Nt Authority\System'
$Action=New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-ExecutionPolicy bypass -File $scriptPath"
Register-ScheduledTask -TaskName $taskname -Trigger $triggers -User $User -Action $Action -RunLevel Highest -Force
The main magic is to use Get-CimClass to get the correct instance, and then populate Subscription from Get-ScheduledTask "Tmp" | Select -ExpandProperty Triggers
Step1:
Go to eventvwr, then create a new scheduled task based on an eventid.
Step2:
Open powershell, then show the scheduled task and see the the way how to was written.
Step3:
Attached and test it in your scrip.
I created a temporary Get-ScheduledTask and run the below line : all what you have to do is to replace the Subscription to meet your requirement.
$A = (Get-ScheduledTask "Tmp" | select -ExpandProperty Triggers)
Here is another method I used in the end to solve the problem:
If you are trying to do this on a Windows Server prior to Server 2012 then you probably don't have the Get-ScheduledTask and New-ScheduledTaskTrigger and the Register-ScheduledTask modules on your Windows system. That means the script in my original question and the accepted answer won't work.
A workaround is to create the desired scheduled task manually and then export it to a xml file. You can edit this file if you like, it is normal plain text xml.
Then have your script and the xml file in the same directory.
Change your script to be compatible with the older version of Powershell to import the xml file.
schtasks /create /tn "Reset-Sessions" /xml "c:\scripts\Reset-Sessions.xml"
Your new scheduled task should now be registered and active.