VM 上的 Powershell 脚本作为计划任务

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

我编写了一个简单的脚本,用于将文件名中包含特定子字符串的文件从一个文件夹移动到另一个文件夹。

$Source = Get-ChildItem -Path "C:\source" -Filter *.xlsm                        
$Target = "C:\target"                                           

$substring = 'copy'                                     

foreach ($file in $Source) {                                    
    if($file.Name -match $substring){                           
        Move-Item -Path $file.FullName -Destination $Target -Force      
    }
        }

我想在虚拟机上自动化它。当我手动运行它并在登录虚拟机时通过任务计划程序运行时,它工作正常,但是当我在任务计划程序(脚本属性)中切换到“无论登录还是注销都运行”时,它将无法工作。我使用以下参数运行它:

-noprofile -executionpolicy unrestricted -noninteractive -file "path to my script"

有什么想法吗?

powershell virtual-machine scheduled-tasks windows-task-scheduler
1个回答
0
投票

选择“无论用户是否登录都运行”选项时,计划任务将在不同的会话上运行。

这意味着它无权访问映射的网络驱动器!。

因此,您需要在脚本中映射驱动器或使用完全限定名称(即\服务器名称\共享名称)

更多详情这里以及

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