PowerShell newbie grappling with trapping errors:
First, if I use this line to copy file items from a source (a mounted Windows Server 2019 ISO file) to a destination that happens to be a USB stick formatted to FAT32 (so the stick will work on a BIOS/UEFI system):
Copy-Item -Path ($ISODriveLetter +":\*") -Destination ($Volume.DriveLetter + ":\") –Recurse
When the Copy-Item process tries to copy an install.wim file that’s almost 5GB in size, it’ll throw an error to the console and continue copying the rest of the items in the mounted ISO to the stick because the _default_ error action is Continue.
Anyway, I’m pretty certain that this is an example of a non-terminating error.
Now I’ve learned that if I use Try {…} Catch {…} statement groups on a non-terminating error, the Catch part will never run.
However , if I place an –ErrorAction Stop at the end of my Try statement like this to turn the error into a _terminating_ error:
Try
{
Copy-Item -Path ($ISODriveLetter +":\*") -Destination ($Volume.DriveLetter + ":\") –Recurse –ErrorAction Stop
}
The Copy-Item process will _stop_ when it gets to the big install.wim file and the rest of the files on my source won’t get copied (if I understand how –ErrorAction Stop works)
What I want my script to do is to _continue_ with the Copy-Item process, log the error and after Copy-Item completes, check the error log if and one exists, run this bit of code to split the wim:
dism /Split-Image /ImageFile:q:\sources\install.wim /SWMFile:f:\sources\install.swm /FileSize:4096
Would the following be a good way to go about doing this?
Place the statement $Error.clear() just before the Copy-Item line
Then use Copy-Item -Path ($ISODriveLetter +":\*") -Destination ($Volume.DriveLetter + ":\") –Recurse -ErrorAction SilentlyContinue
Then have this test to do the split:
If ( $Error.count –ne 0 ) {
dism /Split-Image /ImageFile:q:\sources\install.wim /SWMFile:f:\sources\install.swm /FileSize:4096
}
Gordon
--
You received this message because you are subscribed to the Google Groups "ntpowershell" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntpowershell...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ntpowershell/SJ0PR07MB76799792DA9B9E21387921C0D3BB9%40SJ0PR07MB7679.namprd07.prod.outlook.com.
I have to use FAT32 to be able to use the stick on a UEFI server.
So it’s technically not a problem (and the DISM /Split-Image gets the install.wim split into 2 files and written to the stick).
From: ntpowe...@googlegroups.com [mailto:ntpowe...@googlegroups.com]
On Behalf Of Henry Awad
Sent: Wednesday, January 27, 2021 10:35 AM
To: NTPowershell Mailing List <ntpowe...@googlegroups.com>
Subject: Re: [ntpowershell] Understanding non-terminating errors in Copy-Item
[EXTERNAL]
To view this discussion on the web visit https://groups.google.com/d/msgid/ntpowershell/CAGaCHK7Bk%2BA6vaaCD1BH3gwS4_eQvWDFtb%2B88HKEoxnTn%3DYY6w%40mail.gmail.com.
I’d keep the try/catch.
Before the try{}, do an $error.Clear(), and set $errorVar = $null.
On the Copy-Item, add an argument for “-ErrorVariable errorVar”. Then post-process the contents of $errorVar (i.e., “foreach( $err in $errorVar ) { … }”).
It’ll have the source and target as well as the actual exception/error.
--