How to determine valid variable


i have little function provide common ad info using dsquery:

        function getcontact

        {

            # contacts in domain

            $contact = dsquery contact | cleanadstring | sort

 

            # contacts

            if($contacts.count -eq 0)

            {

                write-host "no contacts found"

            }

            else

            {

                write-host "listing contacts in domain: `n"

                $contacts

                write-host ""

            }

        }

when run function, there may no contacts.  when happens $contacts.count property not valid.  i tried using .gettype() call against variable, but, seems when no hits returned, $contacts variable not longer useful.  how else test see if $contacts variable (for $contacts.length) test valid begin with?

one way change this:

if($contacts.count -eq 0)

to

if(-not $contacts)

if $contacts empty (null) test $false.  if returns anything, test $true.


[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Motherboard replacement

Cannot create Full Text Search catalog after upgrading to V12 - Database is not fully started up or it is not in an ONLINE state

Remote Desktop App - Error 0x207 or 0x607