You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本通过任务计划程序运行时Get-StoredCredential失效,无法访问WebDAV共享的问题求助

PowerShell脚本通过任务计划程序运行时Get-StoredCredential失效,无法访问WebDAV共享的问题求助

我正在写一个PowerShell脚本,用来定期备份远程文件夹的内容——这个远程文件夹被映射成了Windows的WebDAV共享盘G:\。

查了相关资料后了解到:

  • 映射驱动器不能直接通过脚本访问
  • 应该改用UNC路径来访问

但通过UNC路径访问远程文件夹需要提供凭据,我不想把密码明文放在脚本里,所以打算通过凭据管理器来获取。我已经安装了CredentialManager模块,手动从VS Code里运行脚本时,获取凭据并挂载远程文件夹的操作都能正常完成,但把脚本放到任务计划程序里运行就失败了。

我的环境和配置情况:

  • PowerShell版本是7.3.6
  • 任务计划程序使用的是和手动运行相同的非管理员账户,whoami输出一致
  • 该账户已经拥有“登录为批处理作业”的权限
  • 任务没有勾选“使用最高权限运行”
  • 试过“不管用户是否登录都要运行”和“只在用户登录时运行”两种选项,结果都一样

故障表现:任务计划运行脚本时,无法找到G:\驱动器,而且Get-StoredCredential获取凭据失败。

我的脚本(access.ps1)

# MAIN ########################################################################

$debuglog = $PSScriptRoot + "\" + "debuglog.txt"

#$debuglog = $null

$PSDefaultParameterValues = @{'Out-File:Encoding' = 'utf8'}

# Info:
echo "$(get-date -format 'yyMMdd-HHmmss') ##################################################" >> $debuglog
echo "Which domain/account?: <$(whoami)>"   >> $debuglog
echo "Available drives:"     $(get-psdrive) >> $debuglog

# OK, when drive does not show up when script is run from task scheduler, then let's map it:
$credential = Get-StoredCredential -Target "webdav.mc.gmx.net"
echo "Credential: _$credential_" >> $debuglog

if ($credential) {
    echo "Credentials found." >> $debuglog
    $username = $credential.UserName
    $password = $credential.GetNetworkCredential().Password
    $driveLetter = "Y"
    $networkPath = "\\webdav.mc.gmx.net@SSL\DavWWWRoot\myData\"
    remove-psdrive       $driveLetter
    New-PSDrive    -Name $driveLetter -PSProvider FileSystem -Root $networkPath -Credential $credential
}
else {
    echo "Accessing credentials failed." >> $debuglog
}

echo "psdrives:" $(get-psdrive) >> $debuglog
echo "Dir:     " $(dir y:\) >> $debuglog
remove-psdrive       $driveLetter
echo "Exit."                >> $debuglog
exit

脚本运行结果对比

  • 手动运行(VS Code内):一切正常,能找到凭据、挂载驱动器并访问内容
  • 任务计划运行:无法找到G:\驱动器,日志显示“Accessing credentials failed.”

任务计划的XML配置

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2023-07-31T23:41:50.6178473</Date>
<Author>DELFIN7\Martin</Author>
<URI>\Access2</URI>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<StartBoundary>2023-07-31T23:40:09</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByMonth>
<DaysOfMonth>
<Day>13</Day>
</DaysOfMonth>
<Months>
<January />
<February />
<March />
<April />
<May />
<June />
<July />
<August />
<September />
<October />
<November />
<December />
</Months>
</ScheduleByMonth>
</CalendarTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-21-48XXXXX20-12XXXXXX13-21XXXXXX82-1001</UserId>
<LogonType>Password</LogonType>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>Parallel</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
<RestartOnFailure>
<Interval>PT2H</Interval>
<Count>120</Count>
</RestartOnFailure>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
<Arguments>"D:\Datenbanken\Datensicherung\myRoboBak\Executables\access.ps1"</Arguments>
<WorkingDirectory>D:\Datenbanken\Datensicherung\myRoboBak\Executables\</WorkingDirectory>
</Exec>
</Actions>
</Task>

备注:内容来源于stack exchange,提问作者Traveler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 15:22:59