Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PSFramework/PSFramework.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
'Get-PSFLicense',
'Get-PSFLoggingProvider',
'Get-PSFMessageLevelModifier',
'Get-PSFPipeline',
'Get-PSFResultCache',
'Get-PSFRunspace',
'Get-PSFTaskEngineCache',
Expand Down
Binary file modified PSFramework/bin/PSFramework.dll
Binary file not shown.
Binary file modified PSFramework/bin/PSFramework.pdb
Binary file not shown.
122 changes: 122 additions & 0 deletions PSFramework/bin/PSFramework.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions PSFramework/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGELOG
## 0.10.28.???
- New: Command ConvertTo-PSFHashtable converts objects into hashtables
- New: Command Get-PSFPipeline grants access to the current pipeline and all its works.
- New: Logging Provider for gelf / graylog

## 0.10.28.144 : 2018-10-28
- Upd: Module Architecture update
Expand Down
133 changes: 133 additions & 0 deletions PSFramework/functions/meta/Get-PSFPipeline.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
function Get-PSFPipeline
{
<#
.SYNOPSIS
Generates meta-information for the pipeline from the calling command.

.DESCRIPTION
Generates meta-information for the pipeline from the calling command.

.EXAMPLE
PS C:\> Get-Pipeline

Generates meta-information for the pipeline from the calling command.
#>
[OutputType([PSFramework.Meta.Pipeline])]
[CmdletBinding()]
param (

)

begin
{
function Get-PrivateProperty
{
[CmdletBinding()]
param (
$Object,

[string]
$Name,

[ValidateSet('Any', 'Field', 'Property')]
[string]
$Type = 'Any'
)

if ($null -eq $Object) { return }

$typeObject = $Object.GetType()
[System.Reflection.BindingFlags]$flags = "NonPublic, Instance"
switch ($Type)
{
'Field'
{
$field = $typeObject.GetField($Name, $flags)
$field.GetValue($Object)
}
'Property'
{
$property = $typeObject.GetProperty($Name, $flags)
$property.GetValue($Object)
}
'Any'
{
$field = $typeObject.GetField($Name, $flags)
if ($field) { return $field.GetValue($Object) }
$property = $typeObject.GetProperty($Name, $flags)
$property.GetValue($Object)
}
}
}
}
process
{
$callerCmdlet = (Get-PSCallStack)[1].GetFrameVariables()["PSCmdlet"].Value

$commandRuntime = Get-PrivateProperty -Object $callerCmdlet -Name _commandRuntime -Type Field
$pipelineProcessor = Get-PrivateProperty -Object $commandRuntime -Name PipelineProcessor -Type Property
$localPipeline = Get-PrivateProperty -Object $pipelineProcessor -Name LocalPipeline -Type Property

$pipeline = New-Object PSFramework.Meta.Pipeline -Property @{
InstanceId = $localPipeline.InstanceId
StartTime = Get-PrivateProperty -Object $localPipeline -Name _pipelineStartTime -Type Field
Text = Get-PrivateProperty -Object $localPipeline -Name HistoryString -Type Property
PipelineItem = $localPipeline
}

if ($pipeline.Text)
{
$tokens = $null
$errorItems = $null
$ast = [System.Management.Automation.Language.Parser]::ParseInput($pipeline.Text, [ref]$tokens, [ref]$errorItems)
$pipeline.Ast = $ast

$baseItem = $ast.EndBlock.Statements[0]
if ($baseItem -is [System.Management.Automation.Language.AssignmentStatementAst])
{
$pipeline.OutputAssigned = $true
$pipeline.OutputAssignedTo = $baseItem.Left
$baseItem = $baseItem.Right.PipelineElements
}
else { $baseItem = $baseItem.PipelineElements }

if ($baseItem[0] -is [System.Management.Automation.Language.CommandExpressionAst])
{
if ($baseItem[0].Expression -is [System.Management.Automation.Language.VariableExpressionAst])
{
$pipeline.InputFromVariable = $true
$pipeline.InputVariable = $baseItem[0].Expression.VariablePath.UserPath
}
else { $pipeline.InputDirect = $true }
if ($baseItem[0].Expression -is [System.Management.Automation.Language.ConstantExpressionAst])
{
$pipeline.InputValue = $baseItem[0].Expression.Value
}
elseif ($baseItem[0].Expression -is [System.Management.Automation.Language.ArrayLiteralAst])
{
$pipeline.InputValue = @()
foreach ($element in $baseItem[0].Expression.Elements)
{
if ($element -is [System.Management.Automation.Language.ConstantExpressionAst])
{
$pipeline.InputValue += $element.Value
}
else { $pipeline.InputValue += $element }
}
}
else { $pipeline.InputValue = $baseItem[0].Expression }
}
}

$commands = Get-PrivateProperty -Object $pipelineProcessor -Name Commands -Type Property
$index = 0
foreach ($command in $commands)
{
$commandItem = Get-PrivateProperty -Object $command -Name Command
$pipeline.Commands.Add((New-Object PSFramework.Meta.PipelineCommand($pipeline.InstanceId, $index, (Get-PrivateProperty -Object $command -Name CommandInfo), $commandItem.MyInvocation, $commandItem)))
$index++
}

$pipeline
}
}
75 changes: 75 additions & 0 deletions library/PSFramework/Meta/Pipeline.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation.Language;
using System.Text;
using System.Threading.Tasks;

namespace PSFramework.Meta
{
/// <summary>
/// Object representing a pipeline at runtime
/// </summary>
public class Pipeline
{
/// <summary>
/// The unique ID of the pipeline
/// </summary>
public int InstanceId;

/// <summary>
/// When the pipeline was created
/// </summary>
public DateTime StartTime;

/// <summary>
/// The commands that make up the pipeline
/// </summary>
public List<PipelineCommand> Commands = new List<PipelineCommand>();

/// <summary>
/// The full text of the pipeline
/// </summary>
public string Text;

/// <summary>
/// The Ast of the pipeline
/// </summary>
public Ast Ast;

/// <summary>
/// Whether the output is getting assigned to something
/// </summary>
public bool OutputAssigned;

/// <summary>
/// What the output gets assigned to
/// </summary>
public string OutputAssignedTo;

/// <summary>
/// Does the pipeline receive input from a variable?
/// </summary>
public bool InputFromVariable;

/// <summary>
/// What variable does the pipeline receive input from
/// </summary>
public string InputVariable;

/// <summary>
/// Does the pipeline receive a constant as input value directly?
/// </summary>
public bool InputDirect;

/// <summary>
/// What is the value it receives?
/// </summary>
public object InputValue;

/// <summary>
/// The actual PowerShell internal pipeline object
/// </summary>
public object PipelineItem;
}
}
Loading