Well you don't give us much to work with here. You need to figure out what can go wrong in your code and handle those using ex. try/catch blocks or traps.
Ex. your filestream
constructor may throw an exception if you don't have access to create/overwrite the destination-file. This is an UnauthorizedAccessException
exception, as defined at MSDN - FileStream Class So to handle that, you can use this:
try {
$stream = New-Object System.IO.FileStream($destinationfolder + $newFileName + $extension), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
} catch [UnauthorizedAccessException] {
#Handle your exception, ex. log it. Exception is stored in variable $_ and includes properties like $_.message
} catch {
#Catch every possible terminating exception except 'UnauthorizedAccessException'
}
To inspect the properties of an exception, use
(new-object UnauthorizedAccessException) | gm
or
(new-object Exception) | gm
(90% if not all of the properties are inherited from a this general expcetion anyways)
Search here at SO or google to learn more about try/catch and traps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…