I use this function to input a string, search through every script module in my $pshome, and then offer up one or more finds for addition as an ISE script tab. Each function I write is a separate file with a name matching the function.
However, it fails if only one matching file is found. Works fine if I select two or more files.
If someone could just tell me what I am doing wrong.
#requires -Version 2
function Get-FunctionFileName
{
Get-FunctionFileName -Name Get-NIC
.EXAMPLE
PS C:\ref> Get-FunctionFileName -Name .\Get-NIC.ps1
This works ok as well. The function removes '.\' and '.ps1'
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[String]
$Name = 'TBAppointment'
# $Name = 'functionfilename'
# $Name = 'Get-Diskspace'
)
$Start = $PWD
$ArrayofPaths = @( $env:PSModulePath -split ';')
$Collection = @()
if ( $fn ) {Remove-Variable -Name fn -Scope Global -ea Continue}
$fn =@()
$Name = $Name -replace '\.\\', ''
$Name = $Name -replace '\.ps1', ''
foreach ($dir in $ArrayofPaths)
{
Write-Verbose -Message $dir
$Collection += Get-ChildItem -Path $dir -Include "*$Name*" -Recurse | Where-Object -FilterScript {$_.PSIsContainer -eq $false}
} #end foreach
if (-not( $Collection))
{
Write-Warning -Message 'No Results were found!'
break
}
[array]$Results = $Collection | Select-Object -Property BaseName, LastWriteTime, Length, Fullname,
@{ Name = 'Module'; Expression = {Split-Path -Path $_.Directory -Leaf} }
$Results | Format-Table -AutoSize -Property @{Name = 'Index';Expression = {[array]::IndexOf($Results, $_)}}, BaseName, LastWriteTime, Length, Fullname
[array]$Choice = Read-Host -Prompt 'Please Choose by Index Number(s)'
$Choice = $Choice -split {$_ -eq ' ' -or $_ -eq ','} |
Where-Object -FilterScript {$_} |
ForEach-Object -Process {$_.trim()}
Foreach ($Ch in $Choice)
{
$fn = $fn+ $Results[$Ch].fullname
}
if ( -Not($fn)) {Write-Warning '$FN was not set'}
foreach ($file in $fn ) {$file ; psEdit -filenames $file }
}