powershell - PowerShell 测试路径 UNC 访问仅通过计划任务被拒绝
问题描述
PowerShell 3
视窗 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
解决方案
我通过添加 NET USE 并在 PowerShell 脚本中指定凭据来使其工作。
感谢@AdminOfThings 至少让我有不同的想法。
我不知道为什么这才开始发生。我也不明白为什么只有任务计划程序的权限有问题。我使用了与我登录时相同的登录凭据,与创建任务相同的登录凭据,以及存储在每个任务中的相同登录凭据。
$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
推荐阅读
- python - 如何通过 tkinter 绑定传递更多参数
- java - 'UserDetailsService' 类型的 bean 不止一个。Beans:inMemoryUserDetailsManager
- c++ - 如何在不违反 C++ 核心准则的情况下将整数转换为 void*?
- python - 有没有办法在表格底部插入数据?
- javascript - 在 Ajax 中循环 JavaScript
- c++ - 为什么 fflush(stdin) 对输出没有影响?
- c# - ElasticSearch 数据的分组、计算和排序
- recursion - Sympy 中的递归类斐波那契函数
- c++ - 如何使用 while 循环找到 GCD?模运算符如何工作?
- r - 有没有办法自动将文件名设置为列名?