SOLVED

PowerShell doens't run correctly from scheduled task

Copper Contributor

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'
5 Replies
best response confirmed by Tim McGrath (Copper Contributor)
Solution
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?
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
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?
thank you for the responses. I was actually able to get it to work correctly by removing spaces in the file name and changing Actions:
Program/script: Powerschell.exe
Add Arguments: ./namenospaces.ps1
Start in: e:\pathtofile (e is local data drive)
Script runs, Log file that was was recommended by Harm Veenstra is created, data in the csv file is updated.!
Nice to hear that it worked!
1 best response

Accepted Solutions
best response confirmed by Tim McGrath (Copper Contributor)
Solution
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?

View solution in original post