I have following Powershell script file sumdigits.ps1 which outputs number to a log file every 10 seconds. The scripts work properly when executed manually. Execution Policy is set to RemoteSigned.
$logfile = "C:\Users/username/logger.log"
Function Log {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[string]
$Message
)
$Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss.fff")
$Line = "$Stamp $Message"
Add-Content $logfile -Value $Line
}
$i=0
while($true)
{
$i++
Log “We have counted up to $i”
Start-Sleep -s 10
}
I have added it to Startup folder and its showing in task manager as follows. So it seems its getting executed at startup.
However, when I see log file, nothing is getting written to that. Is the script not actually running or it got some error? How to find the error in latter case?
The issue was that ps1 file was getting opened in notepad instead of getting executed. To resolve this, I created batch file to launch powershell script.