WeTi
asked on
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.
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 }
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")
ASKER
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: (:) [], ParentContainsErrorRecordE xception
+ FullyQualifiedErrorId : UnauthorizedAccess
File C:\search_file_string2.ps1
https:/go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordE
+ FullyQualifiedErrorId : UnauthorizedAccess
execution policy needs to be changed...
Run this from an elevated powershell session:
Set-ExecutionPolicy RemoteSigned
Run this from an elevated powershell session:
Set-ExecutionPolicy RemoteSigned
ASKER
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.
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')
ASKER
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...
input $Path, input $pattern, input $respond, then it shows result...
Hmm - I don't see that behavior on my end - what version of Powershell?
You might try this revision.
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')
ASKER
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
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
Hmm - odd. Same version, but mine doesn't behave that way.
Could you provide a screen shot?
Possibly try this logic.
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
}
ASKER
See pic, Repeat shows directly after Pattern....
dump.PNG
dump.PNG
Assuming that no results are displayed, that is to be expected.
You can dump the results if you'd like:
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
}
ASKER
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.
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?
Or are you wanting if nothing is shown, it automatically just cancels out?
ASKER
ASKER
ASKER
Dump1 shows blank after the Pattern then Repeats then result display here... dump2 is No results found. Between Pattern and Repeat.
ASKER
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.
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')
ASKER
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
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
Can you give an example of what type of output you want?
Better still - how are you launching this script?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now its work fine, thx only the LineNumber is now blank?
ASKER
Thanks for the kindly help, now its working well. Linenumber shows up when I add Select-Object Path, Line, LWT! Great support!
Happy to have helped - thanks for the grade!
ASKER
Open in new window