0

I'm trying to run below code in an automated scheduled task. Whether I run this task manually or automated it is not working. When the option 'Run only when user is logged in' is set I at least see a PowerShell window opening, and I do see the jobs getting started. However, when the PS window closes the jobs are not visible (not completed, failed, nothing).

The logging shows the script runs till the import-csv command. I have put the CSV in the C: map, and I run the automated task as the logged in user and on highest privilege.

Why doesn't it get past import-csv? When I run this script in i.e Powershell ISE it works like a charm.

Running program

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Arguments: –NoProfile -ExecutionPolicy Unrestricted -File "C:\Users\usr\Desktop\Scripts\script.ps1"

Start-in: C:\Users\usr\Desktop\Scripts

Write-Host "Starting script"

$maxItems = 8
$iplist = import-csv "C:\Create.csv.txt"
Write-Host "Opened $($iplist[0])"
For ($i=0; $i -le $maxItems; $i++) {
    Write-Host $iplist[$i].DisplayName
    Start-Job -ScriptBlock {
        Param($displayName)
        try{
                Start-Transcript
                Write-Host "Found and started a job for $($displayName)"
            Stop-Transcript
        }
        Catch{
            
            Write-Host "Something went wrong "
            Stop-Transcript
        }
    } -ArgumentList $iplist[$i].DisplayName
}

UPDATE:

The PS window closed before it got to do anything. The answer in this page send me in the right direction. The full fix I used to get this working: Task Scheduling and Powershell's Start-Job

Kraishan
  • 443
  • 5
  • 14
  • 38
  • have you confirmed that the CSV is REALLY named `Create.csv.txt`? have you confirmed that the file is actually a CSV file? have you tried importing that file as a CSV and checking to see if it works _in a regular session_? – Lee_Dailey Jul 18 '20 at 19:40
  • In a regular sessions this works, the file is indeed called create.csv.txt which contains 1 column and multiple rows – Kraishan Jul 18 '20 at 19:47
  • [1] showing the csv to import would help a lot in pinpointing the problem. [2] You use `Import-Csv`, but treat the result as plain string array. [3] Are you sure the file actually has at least 9 items? – Theo Jul 18 '20 at 19:50
  • @KnijnOps - thank you for the added info! [*grin*] please, add the1st three or four lines of the file to your Question - and wrap it in code formatting so that it can be easily read. – Lee_Dailey Jul 18 '20 at 20:00

1 Answers1

1

First, to prevent the powershell window from closing, run add the following line to the bottom of the script:

Read-Host 'Press Any Key to exit'

Second, if you run into issues with params, try explicitly naming the param with a flag:

$iplist = Import-csv -LiteralPath "C:\Create.csv.txt"

Third, make sure that you explicitly declare the delimiter being used if different than a comma.

Solomon Bush
  • 165
  • 9
  • Your first remark send me in the right direction. The PS window (altough running in the background) probably closed before it did anything. This post has the answer that got it working for me: https://stackoverflow.com/questions/41977362/task-scheduling-and-powershells-start-job – Kraishan Jul 20 '20 at 18:58
  • Awesome! I'm glad I could help – Solomon Bush Jul 20 '20 at 20:59