0

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.

Ariser
  • 414
  • 1
  • 6
  • 17
  • What does the script do basically? Why would "at shutdown" be best choice specifically? What GP configs are you using with this setup? Where is the script located exactly like a unc path / file share? – Vomit IT - Chunky Mess Style Oct 16 '17 at 05:47
  • to find out if it is called at all: why not simply start it with `Write-Output "This script was called at $(Get-Date)" | Out-File .\test.txt`? – flolilo Oct 16 '17 at 19:30
  • @flolilolilo ok, I'm going to try that. – Ariser Oct 16 '17 at 20:32
  • @flolilolilo: there was an interesting outcome. Too much for a comment. I'll append it to my question. – Ariser Oct 17 '17 at 18:32
  • You've confirmed through your updated research that your script is at least getting called. It'd be helpful to see the rest of your script so we can try and identify problems. If you're not confident that your script is completing in its entirety, add `write-output "end" >> .\test.txt` to the end of your script. Your NAS mount question appears irrelevant to your original question. If you're having problems with the `New-PSDrive` cmdlet, ask a question separate to this one. – root Oct 17 '17 at 20:22
  • 1
    wouldn't some verbose in form of `try`-`catch` (combined with `out-file`-commands) help to identify the problem? something like `try{gci .\; write-output "gci passed!" | out-file test.txt -append;}catch{write-output "gci failed!" | out-file test.txt -append}` – flolilo Oct 17 '17 at 22:04
  • @flolilolilo It turned out, the first mkdir call already fails. – Ariser Oct 26 '17 at 18:39

0 Answers0