Help with Test-Path error handling

I am trying test create a couple of functions that will return the status of both the directory and file does or does not exist and if there is access to both.

First function shown below is testing if the folder and file exist and if the directory does not it is throwing an exception error that I need to catch nicely. If the directory does exist and the file does not the error message is never report which of course means the exception is not thrown which I currently don't have a try catch in place for it.

Help getting this script to execute catching the error and returning the correct pass or fail message would be appreciated.


Function TestExist()
{
    if (!(Test-Path $TestDir -pathType "Container"))
	    {
            $errMsg = "*** Error: File Directory: " + $TestDir + " does not exist. "
		    Write-Error $errMsg
	    }
    else
        {
            if (!(Test-Path $TestFileFQDN))
	        {
                $errMsg = "*** Error: File: " + $TestFile + " does not exist. "
		        Write-Error $errMsg
	        }
        }
}


Clear-Host

$TestDir = "c:\temp"
$TestFile = "testfile.txt"


TestExist

Open in new window

sekoonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
In your code you didnt define $TestFileFQDN ...

Try..

$TestDir = "c:\temp"
$TestFile = "testfile.txt"
$TestFileFQDN = "$TestDir\$TestFile"

TestExist

Open in new window

0
SubsunCommented:
To display the success messages, try...

Function TestExist()
{if (!(Test-Path $TestDir -pathType "Container"))
	{
		$errMsg = "*** Error: File Directory: " + $TestDir + " does not exist. "
		Write-Error $errMsg
	}
  else
	{
	Echo "*** Success: File Directory: $TestDir exist."
		If (!(Test-Path $TestFileFQDN))
		{
			$errMsg = "*** Error: File: " + $TestFile + " does not exist. "
			Write-Error $errMsg
		}
		else
		{
			Echo "*** Success: File: $TestFile exist."
		}
	}
}

Clear-Host

$TestDir = "c:\temp"
$TestFile = "testfile.txt"
$TestFileFQDN = "$TestDir\$TestFile"

TestExist

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sekoonAuthor Commented:
Works like a charm until I hand it an invalid directory or file than it throws me a WriteException error. How can I trap the exception error so that what is return is directory or file does not exist with possibly the Exception Error
0
Challenges in Government Cyber Security

Has cyber security been a challenge in your government organization? Are you looking to improve your government's network security? Learn more about how to improve your government organization's security by viewing our on-demand webinar!

SubsunCommented:
If there is an invalid directory then the script will generate error.. That's how it's written.. is that what you want? If not please explain..
0
SubsunCommented:
If you are just trying to display the error message then use Write-Host or Write-Output instead of Write-Error
0
sekoonAuthor Commented:
Changing Write-Error to Write-Host completed your other changes to my script. Thanks very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.