PowerShell 3
Windows 2012
Major Minor Build Revision
3 0 -1 -1
I have a few PowerShell scripts that were working for the last few years.
Now they can't seem to run successfully via a Scheduled Task.
If I manually run the PowerShell script outside of Task Scheduler it works.
The script is just creating a new folder on a UNC path.
It is getting an Access Denied error when trying to 'Test-Path'.
Seems like this would be a permissions problems, however, it works using the same login and just double-clicking the script.
The Scheduled Task is set to use the same credentials I am logged on to the server with when I manually run the script.
I have created a new Basic Task in the Scheduler and it still doesn't work.
I have stripped down the code to a basic test-path and create a folder, and still not working.
I create a batch file to read and create a folder in the directory, set it to run via Scheduled Task and that DOES work.
Error Output:
C:\Scripts>c:
C:\Scripts>cd\scripts
C:\Scripts>powershell.exe c:\scripts\makefolder.ps1
Creating New Folders...
Test-Path Result with SilentlyContinue... Does the folder exist already?
False
The folder is: \\intranet.mycompany.com\dept\finance\Shared Documents\Sales Commissions\MyTest
test-path : Access is denied
At C:\scripts\makefolder.ps1:23 char:6
+ IF (test-path -path $today_folder)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied:
(\\intranet.myco...missions\My
Test:String) [Test-Path], UnauthorizedAccessException
+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.Powe
rShell.Commands.TestPathCommand
New-Item : Access is denied
At C:\scripts\makefolder.ps1:28 char:11
+ {New-Item -ItemType Directory -Path $today_folder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (\\intranet.myco...missions\My
Test:String) [New-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.Powe
rShell.Commands.NewItemCommand
The Batch file that the Scheduled Task executes. This just runs the PowerShell Script. I have also removed this Batch file and had the Scheduled Task run the PowerShell directly with the same results:
c:
cd\scripts
powershell.exe c:\scripts\makefolder.ps1
Here is the PowerShell Script:
Write-Host 'Creating New Folders...
' -fore black -back yellow
$today_folder = "\\intranet.mycompany.com\dept\finance\Shared Documents\Sales Commissions\MyTest"
Write-Host 'Test-Path Result with SilentlyContinue... Does the folder exist already?
' -fore black -back yellow
test-path -path $today_folder -ErrorAction SilentlyContinue
write-host 'The folder is: ' $today_folder
write-host '
'
IF (test-path -path $today_folder)
#Folder Already Exist
{ Write-Host $today_folder ":Already Exist, moving on..." -fore black -back green }
ELSE
#Create the Folder
{New-Item -ItemType Directory -Path $today_folder
Write-Host $today_folder ":Created" -fore black -back yellow}
#To see the console window
sleep 5
#Read-Host -Prompt "Press Enter to exit"
If I perform a similar function just using a Batch file it works:
@echo off
echo Hello this a test batch file
pause
net use L: "\\intranet.mycompany.com\dept\finance\Shared Documents\Sales Commissions"
dir L:
pause
mkdir L:\M2019
pause
net use L: /delete
echo all done
pause