powershellerror-handling

Trap error and display warning instead - powershell


I have the following error in my powershell code and I want to replace it with a warning. Here is the error and my function.

Updated Function

Function Find-ReplaceAll {
Param(
[string] $path,
[string] $stringFind,
[string] $stringReplace,
[parameter( Mandatory = $false )]
[string[]] $exclude = @("*.ps1","*.psm1","*.psd1","*.cmd","*.vbs")
)
    try{
        $files = gci -File -Rec -Path $path -Force -Exclude $exclude  -ErrorAction Stop
            foreach ($file in $files){
            $count += $file.count
                write-host "Checking file: $($file.fullname)"
                (Get-Content -path ($file.FullName) -ReadCount 100) |  
                    ForEach-Object { 
                        $_ -replace $stringFind, $stringReplace} |
                            Set-Content -path $file.FullName -force
            }
    } catch {
    [System.UnauthorizedAccessException] | out-null
        write-warning -Message error[0].TargetObject
    catch {
        $error[0]}
    
    }
}


Find-ReplaceAll -files *.txt -stringFind 'testAgain' -stringReplace 'testOld'

ERROR

Find-ReplaceAll : A parameter cannot be found that matches parameter name 'files'.
At line:1 char:17
+ Find-ReplaceAll -files *.txt -stringFind 'testAgain' -stringReplace ' ...
+                 ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Find-ReplaceAll], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Find-ReplaceAll

Solution

  • FIRST QUESTION

    if you want a list, then you would use @(<comma separated strings>)

    [string[]]$exclude = @("*.ps1","*.psm1","*.psd1","*.cmd","*.vbs")
    

    and then replace the string in the gci command with the variable

    $files = gci -File -Rec -Path $path -Force -Exclude $exclude
    

    SECOND QUESTION

    to get a path you can use split-path

    $path = split-path -path $file.FullName
    

    or you can use psparentpath

    $path = $file.psparentpath
    

    you can put either of these in your warning. but only if it was set before the error

    write-warning -Message "Can't access location. Access is denied for path $path"
    

    or

    write-warning -Message "Can't access location. Access is denied for path $($file.psparentpath)"
    

    although generally an access denied error should include the path as parameter TargetObject

    this is why specified exception handling is important but you could use

    write-warning -Message "Can't access location. Access is denied for path $(error[0].targetobject)"
    

    to be honest, it would probably just be easier to print $error[0] and then explain the process is stopping.

    but you can do this

    catch [System.UnauthorizedAccessException] {
        write-warning -Message "Can't access location. Access is denied for path $(error[0].targetobject)"
        write-warning -Message "Stopping further actions"
    }
    catch {
        $error[0]
    }
    

    Hope this helps