I am stuck on a learning project I have set out on, the objective here was meant for me to learn this whole the Begin
, Process
and End
blocks concept
My project consists of a function, that will let the user open a single Firefox window with a specific profile and optionally open one or more web pages via .url
files saved on the C:
drive. This is what I have so far:
Function Ask-Web{
[CmdletBinding()]
Param(
[Parameter(Position = 1)]
[ArgumentCompletions('andy','andrew','halley', "hanna")]
[Array]$FirefoxProfile = 'Andy',
[Parameter(ValueFromPipeline, Position = 2)]
[ArgumentCompletions('DIYhome','DIYreddit','DIYexchange','DIYall', 'SelectWithFZF')]
[Array]$Pages
)
Begin{
$FavouritePages = [ordered]@{
DIYhome = "https://www.diyUK.com"
DIYreddit = "https://www.reddit.com/r/DIY/submit"
DIYexchange = "https://diy.stackexchange.com/questions/ask"
# DIYall Will handle this later
}
[array]$Pages = If($Pages -eq "SelectWithFZF"){Get-ChildItem -Path "C:\temp\Ask Pages\" -Filter *.url*| FZF}Else{$FavouritePages."$Pages"} ;the FZF utility will store all selected items into $Pages variable
}
End{. 'C:\Program Files\Mozilla Firefox\firefox.exe' -p $FirefoxProfile $Pages}
}
The function is supposed to let the user select pages via argument completions, pre provide a web page from the pipeline or make a selection from folder C:\temp\Ask Pages\
using the utility FZF.
In all instances, the user is expected to provide one or more pages/urls:
Ask-Web -FirefoxProfile andy -Pages DIYhome,DIYreddit,DIYexchange ; Open a singe Firefox window, with profile "Andy" and the pages DIYhome,DIYreddit and DIYexchange
Ask-Web -FirefoxProfile hanna -Pages DIYhome ; Open a singe Firefox window, with profile "Hanna" and the page DIYhome
Ask-Web -FirefoxProfile andrew -Pages SelectWithFZF ; Open a singe Firefox window, with profile "Andrew" and all the pages that were selected with FZF
Get-ChildItem .\Diy -filter *.url* | Ask-Web -FirefoxProfile hanna ; Open a singe Firefox window, with profile "Hanna" and with all the pages provided by the pipeline
Where I seem to be stuck is getting PowerShell to give me all the pipeline input at once, rather than one at a time or the last pipeline item, which is what I get with the above function.
If I go with Process{. 'C:\Program Files\Mozilla Firefox\firefox.exe' -p $FirefoxProfile $Pages}
then PowerShell will open a Firefox window for every item there is in $Pages
, yet with End{. 'C:\Program Files\Mozilla Firefox\firefox.exe' -p $FirefoxProfile $Pages}
only the last item in $Pages
gets opened.
I tried:
Function Ask-Web{
[CmdletBinding()]
Param(
[Parameter(Position = 1)]
[ArgumentCompletions('andy','andrew','halley', "hanna")]
[Array]$FirefoxProfile = 'Andy',
[Parameter(ValueFromPipeline, Position = 2)]
[ArgumentCompletions('DIYhome','DIYreddit','DIYexchange','DIYall', 'SelectWithFZF')]
[Array]$Pages
)
Process{[Array]$Pages += @($input)}
End{$Pages}
}
With Ask-Web -FirefoxProfile andy -Pages DIYhome,DIYreddit,DIYexchange
, in the End
block I get :
DIYhome
DIYreddit
DIYexchange
But with 'DIYhome','DIYreddit','DIYexchange'|Ask-Web -FirefoxProfile andy
in the End
block I get :
DIYexchange
DIYexchange
If I forego all the Begin
, Process
and End
blocks with:
Function Ask-Web{
[CmdletBinding()]
Param(
[Parameter(Position = 1)]
[ArgumentCompletions('andy','andrew','halley', "hanna")]
[Array]$FirefoxProfile = 'Andy',
[Parameter(ValueFromPipeline, Position = 2)]
[ArgumentCompletions('DIYhome','DIYreddit','DIYexchange','DIYall', 'SelectWithFZF')]
[Array]$Pages
)
$input
}
I get consistent results, all the elements of array $Pages
are handed at once, regardless of how the user provided them. How can I do the same but with the Begin
, Process
and End
blocks?
Any help would be greatly appreciated!