Neptune443

Untitled

Jan 30th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-CommandDependencies {
  2.     [CmdletBinding(DefaultParameterSetName="ScriptDependencies")]
  3.     param(
  4.         [Parameter(ParameterSetName="ScriptDependencies", Mandatory=$true)]
  5.         [String]$ScriptPath,
  6.         [Parameter(ParameterSetName="FunctionDependencies", Mandatory=$true)]
  7.         [String]$FunctionName
  8.     )
  9.     if ($PsCmdlet.ParameterSetName -eq "ScriptDependencies")
  10.     {
  11.         if( -not (Test-Path $ScriptPath)){
  12.             throw "Script file $ScriptPath not found"
  13.         } else {
  14.             $definition = Get-Content -Path $ScriptPath -Delimiter "`0"
  15.         }
  16.     }
  17.     else
  18.     {
  19.         if( -not (Test-Path function:\$FunctionName)){
  20.             throw "Function $FunctionName not found"
  21.         } else {
  22.            
  23. $scriptBlock = Get-Content -Path function:\$FunctionName
  24.             $definition = $scriptBlock.ToString()
  25.         }
  26.     }    
  27.     [System.Management.Automation.PSParser]::Tokenize($definition,[ref]$null) |
  28.         Where-Object -FilterScript {$_.Type -eq "Command"} |
  29.         Select-Object -ExpandProperty Content |
  30.         Sort-Object -Unique |
  31.         Get-Command -ErrorAction SilentlyContinue
  32. }
Advertisement
Add Comment
Please, Sign In to add comment