Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Try / Catch - can not catch Error. why?

$
0
0

Hi All! I have the following script which scan a folder for xml and/or xls files and then move them to a destination folder. It works fine! I tried to test the error handling in the script and wrote wrong folders (the folder booo and aaaa does not exist on the system). when i run the script it is in the "running state" for ever. No error is catched or displays. Any idea what is wrong? Thanks for the help Archive-Files -Source C:\booo -Destination C:\aaaaa -ErrorAction Stop

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
functionArchive-Files{[CmdletBinding()][OutputType([int])]Param([Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]$Source,[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]$Destination)try{$ErrorActionPreference="Stop"$Files=Get-ChildItem-Path$Source-Recurse-ErrorActionStop|where{$_.Extension-match'xml'-or$_.Extension-match'xls*'}if($Files){$Files|foreach{$target=$Destination+$_.FullName.Substring($Source.length);New-Item-ItemTypefile-Path$target-Force-Verbose;Copy-Item$_.FullName-Destination$target-Force-Verbose}$Files|Remove-Item-Force-Verbose}else{Write-Warningx"No files to move"}}Catch{Write-warning"Error: $_."}}Archive-Files-SourceC:\booo-DestinationC:\aaaaa-ErrorActionStop

Viewing all articles
Browse latest Browse all 6937

Trending Articles