ErrorAction SilentlyContinue

Welcome Forums General PowerShell Q&A ErrorAction SilentlyContinue

This topic contains 9 replies, has 3 voices, and was last updated by

 
Participant
2 years, 1 month ago.

  • Author
    Posts
  • #58929

    Participant
    Points: 64
    Rank: Member

    Hello,

    I have the script below that checks if a folder exists, and if it doesn't it displays the message in Write-host below. The problem is that, it is displaying it, but it also displays the message in bold. I don't want it to show the message in bold. Would a try / catch work better here?

    if ((Test-Path $HomeDirectory) -eq $false)
                    {
                        Write-host "ATTENTION: "$Full_Name " home folder path was not found" -ErrorAction SilentlyContinue
                    }

    NEW-ITEM : The network path was not found
    At \\hqfs1\users\tantony\PowerShell\HRSecurityForms\HRForm.ps1:112 char:13
    + NEW-ITEM –path $HomeDirectory -type directory -force | Ou ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : WriteError: (\\server-8011\users\Person8:String) [New-Item], IOException
    + FullyQualifiedErrorId : CreateIntermediateDirectoriesIOError,Microsoft.PowerShell.Commands.NewItemCommand

    I have my script that creates new AD user almost done (thanks to everyone that answered my previous post), just trying to finish it up.

    Thank you,

    Tony

  • #58930

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    So... you've attached -SilentlyContinue to Write-Host, meaning if Write-Host fails, it will not display an error message. You have not in any way prevented Test-Path from throwing an error.

  • #58932

    Participant
    Points: 64
    Rank: Member

    Thank you, how would I do that?

  • #58938

    Participant
    Points: 64
    Rank: Member

    I tried the -ErrorAction in the if statement, and obviously that doesn't work either.

  • #58944

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    Well, no, it wouldn't; If is not a command.

    What's the goal here? Have Test-Path not show an error? Then you add -SilentlyContinue to Test-Path. But your'e not going to know it failed, if that's the case, so you get no chance to print an alternate message. If you need to CATCH the error and do something about it, you're going to use -ErrorAction Stop, and a Try/Catch block. See, "The Big Book of PowerShell Error Handling" on our eBooks menu.

    • #58950

      Participant
      Points: 64
      Rank: Member

      The goal is, if the folder doesn't exist, to display the "Write-host" message only. Not also the one generated by powershell.

      I'll see try/catch block.

      Thanks,

  • #58945

    Participant
    Points: 98
    Helping Hand
    Rank: Member

    Test-path will give you a true or False. I suspect that the error is coming from your variable $Full_Name

    $HomeDirectory = "C:\temp1234"
    
    if ((Test-Path $HomeDirectory) -eq $false)
                    {
                        Write-host "ATTENTION: $HomeDirectory home folder path was not found"
                    }
  • #58948

    Participant
    Points: 98
    Helping Hand
    Rank: Member

    Also the error in bold is nothing to do with the Code you posted.

    NEW-ITEM : The network path was not found
    At \\hqfs1\users\tantony\PowerShell\HRSecurityForms\HRForm.ps1:112 char:13
    + NEW-ITEM –path $HomeDirectory -type directory -force

    The Error is being created by the cmdlet New-Item

  • #58953

    Participant
    Points: 64
    Rank: Member

    Thanks Alex, I'll check the New-Item error as well

  • #58999

    Participant
    Points: 64
    Rank: Member

    To update....

    I have this working now. Some of our servers are named "server-" branch number, and some of them are named after the city. Since I don't know if they're named after the city or the branch number, I have a script that looks up the branch number, and if it doesn't work, it looks up by the city.

    Thanks everyone.

    Tony

The topic ‘ErrorAction SilentlyContinue’ is closed to new replies.