PowerShell测试路径UNC访问仅通过计划任务拒绝

问题描述 投票:0回答:1

PowerShell 3

Windows 2012

主要次要构建修订


3 0 -1 -1

我有一些PowerShell脚本在过去几年中工作。

现在他们似乎无法通过计划任务成功运行。

如果我在任务计划程序之外手动运行PowerShell脚本,它可以正常工作。

该脚本只是在UNC路径上创建一个新文件夹。

尝试'Test-Path'时会出现Access Denied错误。

这似乎是一个权限问题,但是,它使用相同的登录,只需双击脚本。

计划任务设置为使用我手动运行脚本时登录到服务器的相同凭据。

我在调度程序中创建了一个新的基本任务,它仍然无法正常工作。

我已将代码剥离到基本的测试路径并创建一个文件夹,但仍然无法正常工作。

我创建一个批处理文件来读取并在目录中创建一个文件夹,将其设置为通过Scheduled Task运行并且DOES正常工作。

错误输出:

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脚本。我还删除了这个Batch文件,让Scheduled Task直接运行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

预定任务:enter image description here

enter image description here

powershell task access-denied taskscheduler unc
1个回答
0
投票

我通过添加NET USE并在PowerShell脚本中指定凭据来实现它。

感谢@AdminOfThings至少让我以不同的方式思考。

我不知道为什么这才刚刚开始发生。我也不明白为什么只有Task Scheduler的权限有问题。我使用了与我登录时相同的登录凭据,创建任务的相同,以及存储在每个任务中的相同。

$user = 'corp\crazyuser'
$passwd = 'myPassword'

$today_folder = "\\intranet.mycompany.com\dept\finance\Shared Documents\Sales Commissions"


$subfolder = "TestFolder"


$complete_folder = $today_folder + '\' + $subfolder

#open path
NET USE $today_folder /user:$user $passwd 


    IF (test-path -path $complete_folder) 
        #Folder Already Exist
            { Write-Host $complete_folder ":Already Exist, moving on..." -fore black -back green }
        ELSE
        #Create the Folder 
            {New-Item -ItemType Directory -Path $complete_folder
                    Write-Host $complete_folder ":Created" -fore black -back yellow}

write-host '
Closing NET UNC path
'
NET USE /DELETE  $today_folder

© www.soinside.com 2019 - 2024. All rights reserved.