2

I read everything I could about Powershell and scheduled task. Tried every bits of what could appears to be a solution.

Unfortunately I just can't make PS script apparently run correctly once called from the Windows tasks scheduler. I obviously don't have the knowledge to handle this alone.

The script is working fine if manually launched from a server (Win 2012) running a VirtualBox. No A.D just here to run Virtual Box, eventually used as a public share (one unique user: Administrateur)

  • PS script stops a Virtual Box VM, running on this same server, robocopy the full folder to an usb external drive E: (almost 90min), then restart the VM, then copy again the copy made to the external HD to another machine server 2012 over the network (Z:) .

Some clues: I just can't understand that some tests don't return the same. For instance :

$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"

is a trace making me see some value evolution. when script is manually run, the test-Path is always true (normal) $destination_srv2_root is = "z:\VMbackup", Z: is the result of previous net use to the second server. But become false when run from the scheduler ..!?

And the out-file to a same folder txt log file is correctly writen BUT the same out-file to the external HD (e:...) does not work at all (file not created). It's like nothing exists outside the VirtualBox folder where the PS script exists and is launched. But no console, no error, nothing..

Tried to use every style of arguments last try :

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

Thank for any help.

enter image description here enter image description here

EDIT: here's a part of the code..

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 

2 Answers 2

1

Mapped drives aren't likely to work. Mapped drives are per-user, and more granular than that even; they aren't shared between the privileged and unprivileged tokens of an administrative user.

You should be using a UNC path in your script. For easy drop-in replacement, create a PSDrive of Z in your script that maps to the UNC path.

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

Of course, the user running the scheduled task must have permission to access that UNC path.

Sign up to request clarification or add additional context in comments.

4 Comments

ok.Thank you this could be one of the issue. But what about the external drive E: that is a real physical device. ?
@user1011212 nothing comes to mind for that. Show more of the code. At least the line that writes to E:, but remember more context is better; lines that you think are unrelated to the problem may not be.
here's the full code. in the next reply My first one... cna't we add a file here ?
@user1011212 edit the code into your question, do not post it in comments
0

here's a part of the code..

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.