Forum Discussion

Tim McGrath's avatar
Tim McGrath
Copper Contributor
Jan 10, 2024
Solved

PowerShell doens't run correctly from scheduled task

Hello -

We have a PowerShell script that modifies data and then renames the file. When we run this  script manually it works as expected. When we run it as a scheduled task, the data is not modified, but the file is renamed. 

Server Win2019

Scheduled Task - 

Program/Script:  powershell.exe

Arguments: -NonInteractive -ExecutionPolicy Bypass -File "C:\TCPPS\Final.ps1"

Start in location: E:\MFT Server\users\TCP

 

Run whether user is logged on or not (have tried this both ways) 

 

Scheudule task doesn't run correctly even when a user is logged in and we force the task to run.

Script runs perfectly when we manually run it. 

 

Script

_________________________________________________________

 

# Import the CSV fil
$data = Import-csv -Path 'E:\MFT Server\users\TCP\TCP Employee Secondary Payrate.csv'
 
# Create the mapping hashtable
$categoryMapping = @{
    "3F10" = 1
    "3P10" = 2
    "3S10" = 3
    "4F12" = 4
    "5F12" = 5
    "5S12" = 6
    "6F10" = 7
    "6P10" = 8
    "8F10" = 9
    "8P10" = 10
    "ADM" = 11
    "ADM2" = 12
    "ALT" = 13
    "ALTA" = 14
    "ALTH" = 15
    "ALTT" = 16
    "ASG" = 17
    "BAF" = 18
    "BAP" = 19
    "BDF" = 20
    "BDP" = 21
"FDLD" = 22
"C0C5" = 23
    "CA" = 24
    "CAB" = 25
    "CHEF" = 26
    "CLAS" = 27
    "CNFT" = 28
    "CNPT" = 29
    "CO09" = 30
    "CO10" = 31
    "CO68" = 32
    "COC1" = 33
    "COC2" = 34
    "COC3" = 35
    "COC4" = 36
    "COC5" = 37
    "COC6" = 38
    "COC7" = 39
    "COC8" = 40
    "COC9" = 41
    "CSAD" = 42
    "CUSF" = 43
    "CUSP" = 44
    "DIS" = 45
    "HCA" = 47
    "HCU" = 48
    "HMB" = 49
    "HOUR" = 50
    "IATI" = 51
    "ITG" = 52
    "LIFE" = 53
    "MCAF" = 54
    "MCAP" = 55
    "MCBF" = 56
    "MCBP" = 57
    "MCLA" = 58
    "MECH" = 59
    "MSEC" = 60
    "NUR" = 61
    "SBSE" = 62
    "SCBL" = 63
    "SCHR" = 64
    "SUBT" = 65
    "SUMA" = 66
    "SUMC" = 67
    "SUMM" = 68
    "SUMS" = 69
    "SUMT" = 70
    "SUPC" = 71
    "SWIN" = 72
    "TAX" = 73
    "TCHR" = 74
    "TEAL" = 75
    "VDF" = 76
    "VDP" = 77
    "XTR" = 78
}
 
# Iterate over each row
foreach ($row in $data) {
    # Replace the 'Category' field with the corresponding number from the hashtable
    $row."Current Job Class Code" = $categoryMapping[$row."Current Job Class Code"]
    
}
 
# Export the adjusted data to a new CSV file
$data | Export-Csv -Path 'E:\MFT Server\users\TCP\Updated TCP Employee Secondary Payrate.csv' -NoTypeInformation
 
#delete orignal file
remove-item 'E:\MFT Server\users\TCP\TCP Employee Secondary Payrate.csv'
  • Adding a Start-Transcript c:\temp\log.txt at the top and a Stop-Transcript at the bottom doesn't tell you anything afterward in the log.txt file?
  • Adding a Start-Transcript c:\temp\log.txt at the top and a Stop-Transcript at the bottom doesn't tell you anything afterward in the log.txt file?
    • Tim McGrath's avatar
      Tim McGrath
      Copper Contributor
      When I run the scrip manually it creates the log file... when I run it in scheduled task.... does not create the file.... BUT the script changes the csv file name
      • Harm_Veenstra's avatar
        Harm_Veenstra
        MVP
        And what is in the log file? Are you sure that the script that you edited (Adding the Transcript lines) is the one being used?

Resources