ErrorAction SilentlyContinue

Tagged: 

This topic contains 9 replies, has 3 voices, and was last updated by Profile photo of Tony Antony Tony Antony 1 week, 1 day ago.

  • Author
    Posts
  • #58929
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Thank you, how would I do that?

  • #58938
    Profile photo of Tony Antony
    Tony Antony
    Participant

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

  • #58944
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
      Profile photo of Tony Antony
      Tony Antony
      Participant

      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
    Profile photo of Alex Aymonier
    Alex Aymonier
    Participant

    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
    Profile photo of Alex Aymonier
    Alex Aymonier
    Participant

    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
    Profile photo of Tony Antony
    Tony Antony
    Participant

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

  • #58999
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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

You must be logged in to reply to this topic.