Raw Script

remove-item \\server\c$\scripts\temp\temp.txt
$Where = read-host "Would you like to search in
1. Script name
2. Script content
"

$What = read-host "What are you searching for?"

$scripts = Get-ChildItem "\\server\c$\Scripts" -Recurse -include *.ps1 | ForEach-Object -Process {$_.FullName}


#Perform actions based on whether you are searching for the name or a line of code in a script

#    Initialize the hash table
       $hash = $null
       $count = 1             #<-- Count is used to create the number (key) in the hash table below
       $hash = @{}
       $hash.add(1,"Still haven't found what I'm looking for")
IF($where -eq "1")
    {Foreach($s in $scripts)
        {$Name = Get-childitem $s;
        IF($name.name -match $what){$count = $count + 1; $hash.add($count, $name.name)}
        }
    }

ELSEIF($where -eq "2")
    {Foreach($s in $scripts)
        {$content = Get-Content $s
        IF($content -match $what){$count = $count + 1;$Name = Get-childitem $s;$hash.add($count, $name.name)}
        }
    }
           
           
$len = $hash.count              #<-- Since the hash will be variable length, count the hash entries
$menuChoice = 0
While($menuChoice -lt 1 -or $menuChoice -gt $len)  # <-- Ask the user to select an entry until they make a valid selection
    {
     $menuChoice = Read-Host "Please enter the # of the search item (1-$len):
         
         
                                     $($hash.GetEnumerator() | sort key | out-string)"
         Switch($menuChoice)
                {
                   1       {
                           # use $menuchoice = 0 is you want to make them choose again
                           }
                   default {
                           $selected = $hash.Item([int]$menuChoice);
                           $menuchoice = $len
                           }
                         
                 }
               
         
                                       
Write-Host "You want to look at "$selected
$selectedDetails = get-childitem \\server\c$\scripts\* -recurse -include $selected
$selectedPath = ($selectedDetails).pspath
$d = $selectedPath.trimstart("Microsoft.PowerShell.Core\FileSystem::")
$d = "\\" + $d
Write-Host "The path is " $d

copy-item $d \\server\c$\scripts\temp\temp.txt
notepad \\server\c$\scripts\temp.txt

 }

No comments:

Post a Comment