I have written a backup script in powershell. I authorised it with Unblock-File, so it can be run without having to interact.
I used gpedit to add it to the powershell scripts to be run on shutdown.
While the script works well wenn executed directly it just fails to run on shutdown.
I disabled fast boot and hibernation as described in the second answer to this question but nothing changes.
How can I find out
- whether my script fails
- or the script is not called at all
- and wtf happens during shutdown
Addendum: more information
- The script opens a network connection to a NAS and defines a network drive
- it then copies local data to a newly created folder on that network drive using date and time as name for the folder
- upon completion of the copy process, the network drive is disconnected.
- I considered "at shutdown" as a good analogon for "some work done". I think that when shutting down the computer wilfully, some progress is very likely to have happened.
- I don't know if it is the best choice to do it at shutdown, but it is the best choice I know. Perhaps it could be done on every change of a file automagically?
- I used gpedit.msc to install the script and I went to Computerkonfiguration->Windows-Settings->Scripts->Shutdown. There I added the script in the dedicated "PowerShell-Scripts"-Tab. Ichanged the sequence settings so that the PowerShell-scripts are executed first.
- The script has a local path, C:\Backupsoftware\Smart_Backup_Create_folders_by_Date_MK.ps1
- The Data which shall be copied is on the Desktop and in the documents folder of a certain user account.
More Details I followed flolilolilo's suggestions and added a line at the beginning of my script, which basically does some printf-debug (or is it Write-Output-Debug?). The place where I found the text output file was
C:\Windows\System32\GroupPolicy\Machine\Scripts\Shutdown
So the script is apparently called but does nothing other than putting out the debug information. Can I redirect errors and warnings into a text file?
Ah, and how do I mount the NAS?
New-PSDrive -Name "backup" -PSProvider Filesystem -Root "\\169.254.100.100\share"
Third Addendum
Here comes the current code.
#+-------------------------------------------------------------------+
#| = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |
#|{>/-------------------------------------------------------------\<}|
#|: | Authors: Aman Dhally; ariser | :|
#| :| Email: amandhally@gmail.com
#|: | Purpose: Smart Backup and create folder by Date
#| :|
#|: | Date: 29 November 2011 - 2017
#|: |
#| :| /^(o.o)^\ Version: n.a. |: |
#|{>\-------------------------------------------------------------/<}|
#| = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = : = |
#+-------------------------------------------------------------------+
#System Variable for backup Procedure
# $date = Get-Date -Format d.MMMM.yyyy
$date = Get-Date -Format yyyy.MM.dd.HH.mm.ss
Write-Output "This script was called at %(Get-Date)" | Out-File .\test.txt
New-PSDrive -Name "backup" -PSProvider Filesystem -Root "\\169.254.100.100\Krautlight_aktiv"
# $source = "D:\Tally\Data\"
$destination = "backup:\$date"
# $path = test-Path $destination
$homeprefix= "C:\Users\MyUser\"
$directories = @{ }
$directories.add($homeprefix+"Documents\eagle", "EDA\eagle")
$directories.add($homeprefix+"Documents\KL\Eaglelibraries", "EDA\eigeneLibraries")
$directories.add($homeprefix+"Desktop\Flansch", "CAD\Flansch")
$directories.add($homeprefix+"Desktop\BoCubeDateien", "CAD\Bopla")
try{
mkdir $destination
; write-outpot "mkdir passsed" | out-file .\test.txt -append;
}
catch
{ write-output "mkdir failed" | out-file .\test.txt -append }
ForEach($source in $directories.KEYS.GetEnumerator())
{
cd backup:\
$destpath=$destination+'\'+$directories.Get_Item($source)
Copy-Item -Path $source -Destination $destpath -Recurse
cd c:\
}
Remove-PSDrive "Backup"
My current investigations show a working output of debug messages to the test.txt file. And the mkdir fails.