【发布时间】:2019-04-10 17:50:24
【问题描述】:
PowerShell 3
Windows 2012
主要次要版本修订
3 0 -1 -1
我有一些过去几年一直在工作的 PowerShell 脚本。
现在它们似乎无法通过计划任务成功运行。
如果我在任务计划程序之外手动运行 PowerShell 脚本,它可以工作。
脚本只是在 UNC 路径上创建一个新文件夹。
尝试“测试路径”时出现访问被拒绝错误。
这似乎是一个权限问题,但是,它使用相同的登录名并只需双击脚本即可。
计划任务设置为使用我在手动运行脚本时登录服务器时使用的相同凭据。
我在调度器中新建了一个基本任务,还是不行。
我已将代码剥离为基本的测试路径并创建了一个文件夹,但仍然无法正常工作。
我创建了一个批处理文件来读取并在目录中创建一个文件夹,将其设置为通过计划任务运行并且确实可以工作。
错误输出:
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
计划任务执行的批处理文件。这只是运行 PowerShell 脚本。我还删除了这个批处理文件,并让计划任务直接运行 PowerShell,结果相同:
c:
cd\scripts
powershell.exe c:\scripts\makefolder.ps1
这是 PowerShell 脚本:
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"
如果我只使用批处理文件执行类似的功能,它就可以工作:
@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
【问题讨论】:
-
您是否以用户配置为在任务计划程序中运行作业成功运行脚本?
-
是的。我以该用户身份登录。如果我执行批处理或 Powershell 文件,它工作正常。此登录名也是计划任务的作者。
-
如果将
Test-Path逻辑替换为[System.IO.Directory]::Exists($today_folder)会得到更好的结果吗? -
谢谢,嗯,您建议的“[System.IO.Directory]::Exists($today_folder)”没有错误,但现在错误转移到尝试创建文件夹:: New-Item :访问被拒绝在 C:\scripts\makefolder.ps1:28 char:11 + {New-Item -ItemType Directory -Path $today_folder +
-
那我猜你可以用
[System.IO.Directory]::CreateDirectory($today_folder)替换New-Item逻辑。
标签: powershell task access-denied taskscheduler unc