另一个Powershell脚本只有在安排的情况下才能解决

问题描述 投票:2回答:2

我阅读了关于Powershell和计划任务的所有内容。尝试了似乎是解决方案的每一点。

不幸的是,一旦从Windows任务调度程序调用,我就无法使PS脚本显然正常运行。我显然没有足够的知识来处理这个问题。

如果从运行VirtualBox的服务器(Win 2012)手动启动,脚本工作正常。没有A.D就在这里运行Virtual Box,最终用作公共共享(一个独特的用户:Administrateur)

  • PS脚本停止在同一服务器上运行的Virtual Box VM,将整个文件夹robocopy到usb外部驱动器E :(差不多90分钟),然后重新启动VM,然后再将复制到外部HD复制到另一台机器服务器2012通过网络(Z :)。

一些线索:我只是无法理解某些测试不会返回相同的。例如 :

$IsDestPath2 = test-Path $destination_srv2_root 
write-host "IsDestPath2: $IsDestPath2"
Write-Output "$full_dateh - IsDestPath2: $IsDestPath2" | out-File "C:\Program Files\Oracle\VirtualBox\TEST_log.txt"

是一种让我看到一些价值进化的痕迹。当手动运行脚本时,test-Path始终为true(正常)$ destination_srv2_root is =“z:\ VMbackup”,Z:是先前对第二个服务器的净使用的结果。但是从调度程序运行时变得错误..!?

并且正确写入相同文件夹txt日志文件的out-file但是相同的out文件到外部HD(e:...)根本不起作用(文件未创建)。它就像存在并启动PS脚本的VirtualBox文件夹之外没有任何内容。但没有控制台,没有错误,没有..

最后尝试使用每种风格的参数:

 %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe  -executionpolicy unrestricted -noexit & .\testPS.ps1

感谢您的帮助。

enter image description here enter image description here

编辑:这是代码的一部分..

date_heure
     Write-Host $txt_heure
     Write-Host $txt_date
     Write-host $full_dateh


#$source = "C:\TestSourceBack"
 $source = "C:\VirtualBox VMs"
 #destination vise le disque externe amovible
 $destination_root = "E:\VMbackup"
 $destination = "E:\VMbackup\$thedate"
 #destination2 vise le serveur de secours
 $destination_srv2_map = "\\192.168.1.94\public"
 # Z: pointe directement sur public !!
 $destination_srv2_root = "z:\VMbackup"
 $destination_srv2 = "$destination_srv2_root\$thedate"
# -------------------------------------------------------------------
Write-Output "======================    DEBUT SAUVEGARDE    ==========================" | out-File "$destination_root\backupVM_log.txt" -Append  
Write-Output "$full_dateh" | out-File "$destination_root\backupVM_log.txt" -Append
Write-Output "========================================================================" | out-File "$destination_root\backupVM_log.txt" -Append 
windows powershell scheduled-tasks
2个回答
1
投票

映射的驱动器不太可能工作。映射驱动器是按用户使用的,甚至比使用者更精细;它们不在管理用户的特权和非特权令牌之间共享。

您应该在脚本中使用UNC路径。为了便于直接替换,请在脚本中创建一个映射到UNC路径的PSDrive。

New-PSDrive -Name Z -PSProvider FileSystem -Root \\root\share

当然,运行计划任务的用户必须具有访问该UNC路径的权限。


0
投票

这是代码的一部分..

date_heure
     Write-Host $txt_heure
     Write-Host $txt_date
     Write-host $full_dateh


#$source = "C:\TestSourceBack"
 $source = "C:\VirtualBox VMs"
 #destination vise le disque externe amovible
 $destination_root = "E:\VMbackup"
 $destination = "E:\VMbackup\$thedate"
 #destination2 vise le serveur de secours
 $destination_srv2_map = "\\192.168.1.94\public"
 # Z: pointe directement sur public !!
 $destination_srv2_root = "z:\VMbackup"
 $destination_srv2 = "$destination_srv2_root\$thedate"
# -------------------------------------------------------------------
Write-Output "======================    DEBUT SAUVEGARDE    ==========================" | out-File "$destination_root\backupVM_log.txt" -Append  
Write-Output "$full_dateh" | out-File "$destination_root\backupVM_log.txt" -Append
Write-Output "========================================================================" | out-File "$destination_root\backupVM_log.txt" -Append 
© www.soinside.com 2019 - 2024. All rights reserved.