Windows计划任务运行PowerShell映射网络驱动器失败求助
问题:PowerShell脚本手动运行正常,任务计划程序执行失败
- 系统环境:Windows 10/11
- 核心现象:PowerShell脚本手动运行可成功创建映射网络驱动器,但通过任务计划程序执行时始终失败
- 已尝试的任务配置:
- 分别使用System账户、当前登录的管理员账户运行任务
- 任务兼容性设置为Windows 7和2008 R2
- 任务操作命令:
PowerShell.exe -executionpolicy Bypass -file .ps1
附相关脚本:
$networkSharePath = "\\iilansweepcl1\defaultpackageShare$\Post-Install\Patches" $localPath = "C:\Windows\Patches\" $publicDesktopPath = [Environment]::GetFolderPath("CommonDesktopDirectory") $global:driveLetter = "f:" If (-not (Test-Path -Path "${localPath}exclutions.txt")) { New-Item -Path "${localPath}exclutions.txt" } $FilesOnShare = @{ } $FilesLocal = @{ } function decode_password() { param ( $enc_pass ) return [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($enc_pass)) } function Connect2PackageShare { $UserName = "******admin" $password = ConvertTo-SecureString "$(decode_password 'aW**********y')" -AsPlainText -Force $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password try { net use $driveLetter $networkSharePath /user:*****admin ilishelpYOU2 /persistent:no | Out-Null #New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root $networkSharePath -Scope Global -ErrorAction stop } catch { Try { net use $driveLetter $networkSharePath /user:*****admin ilishelpYOU2 /persistent:no | Out-Null #New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root $networkSharePath -Credential $credential -Scope Global -ErrorAction Stop } catch { exit } } $Files = Get-ChildItem -Path 'B:\' foreach ($File in $Files) { $LastWriteTime = $File.LastWriteTime $FilesOnShare[$File.Name] = $LastWriteTime } Return $FilesOnShare } # Connect2PackageShare function Get-LocalPostInstall { $result = Get-Item -Path $localPath if ($result) { $LFiles = Get-ChildItem -Path $localPath foreach ($Lfile in $Lfiles) { $LastWriteTime = $Lfile.LastWriteTime $FilesLocal[$Lfile.Name] = $LastWriteTime } } Return $FilesLocal } # Get-LocalPostInstall Connect2PackageShare
解决方案
1. 修复脚本路径引用错误
脚本定义的映射盘符是f:,但后续获取共享文件时硬编码用了B:\,这会直接导致路径不存在错误。将Get-ChildItem -Path 'B:\'修改为Get-ChildItem -Path $driveLetter。
2. 放弃使用System账户运行任务
System是本地系统账户,默认没有网络共享访问权限(即使共享开放权限给它也存在安全风险),改用拥有该网络共享访问权限的域账户或本地管理员账户执行任务。
3. 调整任务计划程序核心配置
- 常规选项卡:勾选「不管用户是否登录都要运行」(无需交互场景),或「只有在用户登录时运行」(调试时需要可见窗口);同时勾选「使用最高权限运行」
- 操作选项卡:确保脚本路径为完整绝对路径,比如
PowerShell.exe -executionpolicy Bypass -file "C:\YourScriptPath\YourScript.ps1",避免任务默认工作目录(C:\Windows\System32)找不到脚本 - 设置选项卡:取消勾选「如果任务运行时间超过X小时就停止」,防止任务被强制终止
4. 优化net use映射逻辑
先检查盘符是否已映射,避免重复执行映射命令;同时统一账户密码的使用方式(不要同时混用Base64解码和硬编码):
# 检查目标盘符是否已存在映射 if (-not (Get-PSDrive -Name $driveLetter.TrimEnd(':') -ErrorAction SilentlyContinue)) { # 使用变量传递账户信息,避免硬编码泄露风险 net use $driveLetter $networkSharePath /user:$UserName $plainPassword /persistent:no | Out-Null }
注意:Base64是编码而非加密,不要用它存储敏感密码,建议使用任务计划程序的内置凭据存储(在任务属性「操作」中点击「更改用户或组」,选择账户后输入密码)。
5. 开启日志排查问题
修改任务操作命令,将执行日志输出到指定文件,方便定位具体错误:
PowerShell.exe -executionpolicy Bypass -file "C:\YourScriptPath\YourScript.ps1" 2>&1 > "C:\YourScriptPath\TaskRunLog.txt"
内容的提问来源于stack exchange,提问作者Amos
相关产品推荐
相关产品推荐

