Powershell code question "if" statement

Dear experts

Please read the script code below, I would like to improve it with some more function in it. First I would like to make it runable for double click on the file and after indata with $Path and $Pattern, it show up the result in it and then it will ask if you wanna search again yes or no, if yes then rerun the script from Param if no breaks.  I tried to write a if statement below well, knowing that it will not  work like that, would be great for a solution.

Thanks alot.

Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Pattern
)

function Runscript ($Path, $Pattern) {
Get-ChildItem -Path $Path -Recurse -File |
	Select-String -Pattern $Pattern |
	Group-Object Path |
	ForEach-Object {
		$LWT = (Get-Item -Path $_.Name).LastWriteTime
		$_.Group | Select-Object Path, LineNumber, @{n='LastWriteTime'; e={$LWT}}
	}
}

Param([Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
[String]$rerun
)

if ($rerun = y){
Runscript
}
else {
break }

Open in new window

LVL 1
WeTiAsked:
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.

WeTiAuthor Commented:
Well I tried again, with do and while statement, but not working...

do
{
Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Pattern
)



function Runscript ($Path, $Pattern) {
Get-ChildItem -Path $Path -Recurse -File |
	Select-String -Pattern $Pattern |
	Group-Object Path |
	ForEach-Object {
		$LWT = (Get-Item -Path $_.Name).LastWriteTime
		$_.Group | Select-Object Path, LineNumber, @{n='LastWriteTime'; e={$LWT}}
	}
}

$response = read-host "Repeat?"
}

while ($response -eq "Y")

Open in new window

0
sirbountyCommented:
You have nothing calling your function...try this

Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Pattern
)
$response = 'Y'

do {
  Get-ChildItem -Path $Path -Recurse -File | Select-String -Pattern $Pattern |
	Group-Object Path | ForEach-Object {
		$LWT = (Get-Item -Path $_.Name).LastWriteTime
		$_.Group | Select-Object Path, LineNumber, @{n='LastWriteTime'; e={$LWT}}
	}
$response = read-host "Repeat?"
} while ($response -eq "Y")

Open in new window

0
WeTiAuthor Commented:
error?
File C:\search_file_string2.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
    + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnauthorizedAccess
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

sirbountyCommented:
execution policy needs to be changed...
Run this from an elevated powershell session:
Set-ExecutionPolicy RemoteSigned
0
WeTiAuthor Commented:
Now what you did was: Repeat the script from Get-ChildItem, this will show 2 results everytime I write Y, what I want is to rerun the whole script that you put in new $Path and new $Pattern, and the $respond seems show up directly after the input $Pattern well I want it to show up after the result.
0
sirbountyCommented:
Ah, I understand...

function Runscript {
  Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Pattern
  )

  Get-ChildItem -Path $Path -Recurse -File | Select-String -Pattern $Pattern |
	Group-Object Path | ForEach-Object {
		$LWT = (Get-Item -Path $_.Name).LastWriteTime
		$_.Group | Select-Object Path, LineNumber, @{n='LastWriteTime'; e={$LWT}}
	}

}

do { 
  Runscript 
  $response = read-host "Repeat?"
} until ($response -ne 'y')

Open in new window

0
WeTiAuthor Commented:
Almost there i want: first it ask you of $Path, then  $Pattern, show result then ask repeat? 'Y' rerun the whole script, now the script ask:
input $Path, input $pattern, input $respond, then it shows result...
0
sirbountyCommented:
Hmm - I don't see that behavior on my end - what version of Powershell?
You might try this revision.

function Runscript {
  Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Pattern
  )

  Get-ChildItem -Path $Path -Recurse -File | Select-String -Pattern $Pattern |
	Group-Object Path | ForEach-Object {
		$LWT = (Get-Item -Path $_.Name).LastWriteTime
		$_.Group | Select-Object Path, LineNumber, @{n='LastWriteTime'; e={$LWT}}
	}

}

do { 
  Runscript 
} until ((read-host "Repeat?") -ne 'Y')

Open in new window

0
WeTiAuthor Commented:
Same result, it first ask: Path then ask Pattern then ask Repeat.
What I want is: It first ask: Path then ask Pattern, show result, then ask Repeat...
PS version below.
ame             : Windows PowerShell ISE Host
Version          : 5.1.16299.98
0
sirbountyCommented:
Hmm - odd.  Same version, but mine doesn't behave that way.
Could you provide a screen shot?

Possibly try this logic.

function Runscript {
  Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Pattern
  )

  Get-ChildItem -Path $Path -Recurse -File | Select-String -Pattern $Pattern |
	Group-Object Path | ForEach-Object {
		$LWT = (Get-Item -Path $_.Name).LastWriteTime
		$_.Group | Select-Object Path, LineNumber, @{n='LastWriteTime'; e={$LWT}}
	}

}

Runscript 

while ((read-host "Repeat?") -eq 'Y') {
    Runscript
}

Open in new window

0
WeTiAuthor Commented:
See pic, Repeat shows directly after Pattern....
dump.PNG
0
sirbountyCommented:
Assuming that no results are displayed, that is to be expected.

You can dump the results if you'd like:

function Runscript {
  Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Pattern
  )

$results =  Get-ChildItem -Path $Path -Recurse -File | Select-String -Pattern $Pattern |
	Group-Object Path | ForEach-Object {
		$LWT = (Get-Item -Path $_.Name).LastWriteTime
		$_.Group | Select-Object Path, LineNumber, @{n='LastWriteTime'; e={$LWT}}
	}
if ($results -ne $null) {$results} else {write-host "No results found"}
}

Runscript 

while ((read-host "Repeat?") -eq 'Y') {
    Runscript
}

Open in new window

0
WeTiAuthor Commented:
Well that code only shows No result found when I put in something I that didn't exist, then it asks repeat? if I put in something that gives result its showing same problem, that first Path input, then pattern input, then space, then Repeat? As in picture.
0
sirbountyCommented:
Your picture showed no results and then repeat?  Which is what is to be expected, I think.
Or are you wanting if nothing is shown, it automatically just cancels out?
0
WeTiAuthor Commented:
0
WeTiAuthor Commented:
0
WeTiAuthor Commented:
Dump1 shows blank after the Pattern then Repeats then result display here... dump2 is No results found. Between Pattern and Repeat.
0
WeTiAuthor Commented:
After for a while error searching I find out that this: Select-Object Path, LineNumber this was jumping down the result to Repeat, but I have no idea how to fix this... Still trying.
0
sirbountyCommented:
I think the grouping was tripping it up - try it this way...

function Runscript {
  Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Pattern
  )

    foreach ($result in (Get-ChildItem -Path $Path -Recurse -File | Select-String -Pattern $Pattern)) {
        $obj = New-Object -TypeName PSObject
        $obj | Add-Member -MemberType NoteProperty -Name LWT -Value ((Get-Item -Path $result.path).LastWriteTime)
        $obj | Add-Member -MemberType NoteProperty -Name Path -Value $result.path
        $obj | Add-Member -MemberType NoteProperty -Name Line -Value $result.linenumber
        Write-Output $obj
    }
}

do {
    $output = Runscript | Group-Object Path
    $output
    $response = read-host 'Repeat?'
} until ($response -ne 'Y') 

Open in new window

0
WeTiAuthor Commented:
That way result become like in the picture... I dont need to know Count column, I only need Name with path and LineNumber.
What you did was removing the Select-Object. It seems that Select-Object is the reason why it jump to do statement, if I need to show Object only like Path and Line, is the Select-Object the only way to do? If so then there has to be a way to solve this.
dump.PNG
0
sirbountyCommented:
Can you give an example of what type of output you want?
0
sirbountyCommented:
Better still - how are you launching this script?
0
sirbountyCommented:
Think I've been able to finally reproduce this.
Try this slight adjustment to your do loop...

do {
    Runscript | out-host
    $response = read-host 'Repeat?'
} until ($response -ne 'Y')
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
WeTiAuthor Commented:
Now its work fine, thx only the LineNumber is now blank?
0
WeTiAuthor Commented:
Thanks for the kindly help, now its working well. Linenumber shows up when I add Select-Object Path, Line, LWT! Great support!
0
sirbountyCommented:
Happy to have helped - thanks for the grade!
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.