Powershell scripting improvement Get-Childitem

Dear expert

I would like to improve code below. Right now if I don't write anything inside $Path and $String it will return a error message:
FindPattern : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

Now I would like to add a if statement inside like this way: if ($Path -Or $String = $null) {$response}
Simply if variable $Path or $String is empty, go to do statement below or go to variable $response. Now I don't know if Im right about the statement or the execution with $response after but i need help with: where I put this if statement at best? And how will I write?

Thanks.

Function FindPattern {
Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$String
)
	$Script:Results = $false
	Get-ChildItem -Path $Path -Recurse -File |
		Select-String -Pattern $String |
		Select-Object -Property Path, LineNumber, @{n='LWT'; e={
			Write-Progress -Activity "Searching for '$($Path)'" -Status $_.Path
			(Get-Item -Path $_.Path).LastWriteTime
			$Script:Results = $true
		}}
	If (-not $Script:Results) {
		Write-Host "Cannot find '$($String)' under '$($Path)'"
	}
	Write-Progress -Activity 'Done' -Status 'Done' -Completed
}

Do {
	FindPattern
	$response = Read-Host 'Search again?'
} Until ($response -ne 'Yes')

Open in new window

LVL 1
WeTiAsked:
Who is Participating?
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.

oBdACommented:
That doesn't work with the auto prompt of mandatory variables, so you'll have to do it with Read-Host:
Function FindPattern {
Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Pattern
)
	$Script:Results = $false
	Get-ChildItem -Path $Path -Recurse -File |
		Select-String -Pattern $Pattern |
		Select-Object -Property Path, LineNumber, @{n='LWT'; e={
			Write-Progress -Activity "Searching for '$($Path)'" -Status $_.Path
			(Get-Item -Path $_.Path).LastWriteTime
			$Script:Results = $true
		}}
	If (-not $Script:Results) {
		Write-Host "Cannot find '$($Pattern)' under '$($Path)'"
	}
	Write-Progress -Activity 'Done' -Status 'Done' -Completed
}

Do {
	$Path = Read-Host 'Path'
	$Pattern = Read-Host 'String'
	If (-not [string]::IsNullOrEmpty($Path) -and -not [string]::IsNullOrEmpty($Pattern)) {
		FindPattern -Path $Path -Pattern $Pattern
	}
	$response = Read-Host 'Search again?'
} Until ($response -ne 'Yes')

Open in new window

And what happened to the version with full Progress support?
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
rastoiWindows DTS expertCommented:
@ Obda -  it is working, but I would add some
else {write-host "can't be empty string"} 

Open in new window


if the intension is to let the cycle running after empty string.
Otherwise it can ends without ugly red message: let mandatory variables construction do they job and simply add between "function{}" and "do{}" lines
$ErrorActionPreference='SilentlyContinue'
trap {write-host "empty string is not allowed for parameters"}

Open in new window

0
WeTiAuthor Commented:
oBdA thanks for the kindly support again, it works as you said, well I got many test version of that code open so I simply picked a wrong one, the write-progress function should be in there.  question about the "::" what does that do?

Rastoi thanks for the effort by adding a text and provide the $ErrorActionPreference='SilentlyContinue'
trap {write-host "empty string is not allowed for parameters"} to me, however I would like the code to loop in cycle when  $response is Yes. So after that $ErrorActionPreference the action of the script seems stopped. But if there is other way for it to continue it would be good too.

Anyway the If ($Path -Or $String is $null) as I tried before it doesn't work that way or what?

Thanks again
0
oBdACommented:
The :: is PowerShell's way of invoking a static method ("IsNullOrEmpty") of a class ("string").

Oh, and "Trap" is deprecated. Try/Catch/Finally should be used to handle terminating errors.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.