header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Error Checking
Last Post 30 Aug 2010 06:45 PM by Marco Shaw (MVP). 1 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
surge3333User is Offline
New Member
New Member
Posts:57
Avatar

--
30 Aug 2010 08:09 AM

    I have a simple script to display the members of a Local Group on a server, and I’m having problems trying to add some Error Checking to it. I want to display a message and end the script if the specified group doesn’t exist.

      

    $Server = "xx.x.xxx.xx"

    $Group = "fred"

    $g = [ADSI]("WinNT://$server/$group,group")

                   

    Running the above results in this error -

     

    format-default : The following exception occurred while retrieving member "PSComputerName": "The group name could not b

    e found.

    "

        + CategoryInfo          : NotSpecified: (:) [format-default], ExtendedTypeSystemException

        + FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand

     

    If I could capture this text from the message, that would be great - "The group name could not be found.” But at this point just identify there is an error would be good.

     

    The –ErrorAction parameter doesn’t work on this $g = [ADSI]("WinNT://$server/$group,group")  Statement.

     

    I’ve tried looking at the $Error[0].Exception.Message but can’t seem to get what I need. I’ve tried using Try/Catch as follow to at least detect an error, but with no luck, as it doesn't seem to Catch the error -

     

    try

              {

                 $g = [ADSI]("WinNT://$server/$group,group")

              }

              catch

              {

                  Write-Warning "Group not found"

              }

     

     

    Any thoughts?

     

    Thanks

    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1642
    Avatar

    --
    30 Aug 2010 06:45 PM
    That's a good one... It doesn't seem to return a terminating error so a try won't work, and it isn't a cmdlet so you can't use -ErrorAction, for example.
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer