Werbung

Herkunft

Kurzinformation

Gets the commands referred to from within a function or external script

Syntax

<# .Synopsis Gets the commands referred to from within a function or external script .Description Uses the Tokenizer to get the commands referred to from within a function or external script .Example Get-Command New-Button | Get-ReferencedCommand #> param( # The script block to search for command references [Parameter(Mandatory=$true, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$true)] [ScriptBlock] $ScriptBlock ) begin { if (-not ('WPK.GetReferencedCommand' -as [Type])) { Add-Type -IgnoreWarnings @" using System; using System.Collections.Generic; using System.Management.Automation; using System.Collections.ObjectModel; namespace WPK { public class GetReferencedCommand { public static IEnumerable GetReferencedCommands(ScriptBlock scriptBlock, PSCmdlet cmdlet) { Dictionary resolvedCommandCache = new Dictionary(); Queue tokenQueue = new Queue(); Collection errors; foreach (PSToken token in PSParser.Tokenize(new object[] { scriptBlock }, out errors)) { tokenQueue.Enqueue(token); } if (tokenQueue.Count == 0) { yield return null; } while (tokenQueue.Count > 0) { PSToken token = tokenQueue.Dequeue(); if (token.Type == PSTokenType.Command) { CommandInfo cmd = null; cmd = cmdlet.SessionState.InvokeCommand.GetCommand(token.Content, CommandTypes.Alias); if (cmd == null) { cmd = cmdlet.SessionState.InvokeCommand.GetCommand(token.Content, CommandTypes.Function); if (cmd == null) { cmd = cmdlet.SessionState.InvokeCommand.GetCommand(token.Content, CommandTypes.Cmdlet); } } else { while (cmd != null && cmd is AliasInfo) { AliasInfo alias = cmd as AliasInfo; if (!resolvedCommandCache.ContainsKey(alias)) { yield return alias; resolvedCommandCache.Add(alias, true); } cmd = alias.ReferencedCommand; } } if (cmd == null) { continue; } if (cmd is FunctionInfo) { if (! resolvedCommandCache.ContainsKey(cmd)) { FunctionInfo func = cmd as FunctionInfo; yield return cmd; foreach (PSToken t in PSParser.Tokenize(new object[] { func.ScriptBlock }, out errors)) { tokenQueue.Enqueue(t); } resolvedCommandCache.Add(cmd, true); } } else { if (!resolvedCommandCache.ContainsKey(cmd)) { yield return cmd; resolvedCommandCache.Add(cmd, true); } } } } } } } "@ } $commandsEncountered = @{} } process { [WPK.GetReferencedCommand]::GetReferencedCommands($ScriptBlock, $PSCmdlet) }

Beschreibung

Uses the Tokenizer to get the commands referred to from within a function or external script

Links

Liste aller Commandlets Mehr über die Windows PowerShell

Bücher zur PowerShell

Buchcover Windows PowerShell 5.0 - Das Praxishandbuch (1. Auflage 2016/5. Auflage Gesamtreihe) Windows PowerShell 5.0 - Das Praxishandbuch (1. Auflage 2016/5. Auflage Gesamtreihe)
Autoren: Dr. Holger Schwichtenberg
erschienen 2016, 996 Seiten, 49,99 €
ISBN: 3446446435
Bestellung: Amazon.de oder im Buchhandel über ISBN 3446446435

Schulungen/Seminare

Offene Seminare zur PowerShell
In-House-Seminare zur PowerShell