I would like to… enter selection then run the selection (Tool) with parameters
The “tools” are generally run against remote PC’s; all of the functions in the psm1 file use invoke-command…
to date i have the script below which enumerates the “tools” (Functions) in a .psm1 file dynamically.
$Function = get-module Toolkit -ListAvailable | % {$_.ExportedCommands.Values}
$menu = @{}
for ($i=1;$i -le $Function.count; $i++)
{ Write-Host “$i. $($Function[$i-1].name),$($Function[$i-1].status)”
$menu.Add($i,($Function[$i-1].name))}
[int]$ans = Read-Host ‘Enter selection’
$selection = $menu.Item($ans) ;
$selection
$selection would run the selected tool.
Suggestions as to how I can achieve this would be greatly appreciated, bearing in mind my skill level is basic
Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and also makes testing easier as we can copy and paste your code without faffing about replacing curly quote marks to get things working.
One way to do this would be use Invoke-Expression which converts a string to executable code.
$Function = Get-Module Toolkit -ListAvailable | ForEach-Object { $_.ExportedCommands.Values }
$menu = @{}
for ($i = 1; $i -le $Function.count; $i++) {
Write-Host "$i. $($Function[$i-1].name),$($Function[$i-1].status)"
$menu.Add($i, ($Function[$i - 1].name))
}
[int]$ans = Read-Host 'Enter selection'
Invoke-Expression $menu[$ans]
$commands = (Get-Module Storage -ListAvailable).ExportedCommands.Values.Name
$menu = $commands |
Select @{Name='Index';Expression={$commands.IndexOF($_) + 1}},
@{Name='Command';Expression={$_}}
Output:
Index Command
----- -------
1 Add-InitiatorIdToMaskingSet
2 Add-PartitionAccessPath
3 Add-PhysicalDisk
4 Add-StorageFaultDomain
5 Add-TargetPortToMaskingSet
6 Add-VirtualDiskToMaskingSet
7 Block-FileShareAccess
8 Clear-Disk
9 Clear-FileStorageTier
10 Clear-StorageDiagnosticInfo
11 Connect-VirtualDisk
12 Debug-FileShare ...
2 Likes
Apologies for not formatting the code. I knew there was a preferred way, but couldn’t find /couldn’t remember it.
thank you for your suggestion, i will give it a try
HI Rob-simmers, thank you for your reply, I will give it a go
@matt-bloomfield, that worked a treat. you’ve made my day.
cheers Peter
You’re welcome, happy to help.
Did you manage to incorporate @rob-simmers suggestion? That’s a neat way to generate the menu.
@matt-bloomfield & @rob-simmers no i haven’t tried that one yet. was excited not having to go toa txt file containing all the functions in my toolkit module each time i needed to run one. I will have a try though
thanks again everyone for your suggestions
Peter