WeTi
asked on
Powershell count file improvement.
Dear Export
I want to improve the code below, thanks for the effort of oBdA the kindly help every time.
Now I tried to run this script and it's running fine until I bug testing this with different way, in Path input i simply just type c:\wojpijwg, now what Get-ChildItem do is, it starts to search through everything under the c:\, The reason why i know this is, it returns several errors that some folder under c:\windows\system is not permitted, well of course It's windows 10 security for protection of virus or malware, this way I knew that Get-Childitem is searching all the folders below c:\ or maybe it could be the $Index that it tries to Index everything in c:\?
Anyway I would add a function that when I type in the path, the script will first check if the path exist or not, if the path exist do all the Get-ChildItem if not, jump down to $response.
Thanks
I want to improve the code below, thanks for the effort of oBdA the kindly help every time.
Now I tried to run this script and it's running fine until I bug testing this with different way, in Path input i simply just type c:\wojpijwg, now what Get-ChildItem do is, it starts to search through everything under the c:\, The reason why i know this is, it returns several errors that some folder under c:\windows\system is not permitted, well of course It's windows 10 security for protection of virus or malware, this way I knew that Get-Childitem is searching all the folders below c:\ or maybe it could be the $Index that it tries to Index everything in c:\?
Anyway I would add a function that when I type in the path, the script will first check if the path exist or not, if the path exist do all the Get-ChildItem if not, jump down to $response.
Thanks
Function FindPattern {
Param(
[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
[String]$Path,
[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
[String]$Pattern
)
$Script:Results = $false
$AllItems = Get-ChildItem -Path $Path -Recurse -File | Select-Object -Property Fullname, LastWriteTime
$Index = 0
$AllItems | ForEach-Object {
$item = $_
Write-Progress -Activity "Söker '$($Pattern)'" -Status $item.FullName -PercentComplete ((100 * $Index++) / $AllItems.Count)
Select-String -Path $item.FullName -Pattern $Pattern |
Select-Object -Property Path, LineNumber, @{n='LWT'; e={
$item.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')
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER