Skip to content
This repository was archived by the owner on Dec 3, 2025. It is now read-only.
Closed
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
8 changes: 8 additions & 0 deletions Microsoft.DotNet.Interactive.Jupyter/Protocol/LanguageInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public FSharpLanguageInfo(string version = "4.5") : base("C#", version, "text/x-
}
}

public class PowerShellLanguageInfo : LanguageInfo
{
public PowerShellLanguageInfo(string version = "7.0") : base("PowerShell", version, "text/x-powershell", ".ps1", pygmentsLexer: "powershell")
{

}
}

public class VBnetLanguageInfo : LanguageInfo
{
public VBnetLanguageInfo(string version = "15.0") : base("C#", version, "text/x-vbnet", ".vb", pygmentsLexer: "vbnet")
Expand Down
1 change: 1 addition & 0 deletions Microsoft.DotNet.Interactive.Jupyter/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ private LanguageInfo GetLanguageInfo(string kernelName) =>
{
"csharp" => new CSharpLanguageInfo(),
"fsharp" => new FSharpLanguageInfo(),
"powershell" => new PowerShellLanguageInfo(),
_ => null
};
}
Expand Down
19 changes: 19 additions & 0 deletions Microsoft.DotNet.Interactive.PowerShell/Commands/CommandUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Management.Automation;
using Microsoft.PowerShell.Commands;

namespace Microsoft.DotNet.Interactive.PowerShell.Commands
{
internal class CommandUtils
{
internal readonly static CmdletInfo OutStringCmdletInfo = new CmdletInfo("Out-String", typeof(OutStringCommand));
internal readonly static CmdletInfo WriteInformationCmdletInfo = new CmdletInfo("Write-Information", typeof(WriteInformationCommand));

internal readonly static object BoxedTrue = (object)true;
internal readonly static object BoxedFalse = (object)false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.PowerShell.Commands;

namespace Microsoft.DotNet.Interactive.PowerShell.Commands
{
using System.Management.Automation;

/// <summary>
/// Writes the formatted output of the pipeline object to the information stream before passing the object down to the pipeline.
/// </summary>
/// <remarks>
/// This Cmdlet behaves like 'Tee-Object'.
/// An input pipeline object is first pushed through a steppable pipeline that consists of 'Out-String | Write-Information -Tags "__PipelineObject__"',
/// and then it's written out back to the pipeline without change. In this approach, we can intercept and trace the pipeline objects in a streaming way
/// and keep the objects in pipeline at the same time.
/// </remarks>
[Cmdlet(VerbsDiagnostic.Trace, "PipelineObject")]
public sealed class TracePipelineObjectCommand : PSCmdlet
{
/// <summary>
/// The object from pipeline.
/// </summary>
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
public object InputObject { get; set; }

private static PowerShell s_pwsh;
Comment thread
TylerLeonhardt marked this conversation as resolved.
Resolved
private SteppablePipeline _stepPipeline;

static TracePipelineObjectCommand()
{
s_pwsh = PowerShell.Create();
s_pwsh.AddCommand(CommandUtils.OutStringCmdletInfo).AddParameter("Stream", CommandUtils.BoxedTrue)
.AddCommand(CommandUtils.WriteInformationCmdletInfo).AddParameter("Tags", "__PipelineObject__");
}

/// <summary>
/// BeginProcessing override.
/// </summary>
protected override void BeginProcessing()
{
_stepPipeline = s_pwsh.GetSteppablePipeline();
_stepPipeline.Begin(this);
}

/// <summary>
/// ProcessRecord override.
/// </summary>
protected override void ProcessRecord()
{
_stepPipeline.Process(InputObject);
WriteObject(InputObject);
}

/// <summary>
/// EndProcessing override.
/// </summary>
protected override void EndProcessing()
{
_stepPipeline.End();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<NoWarn>$(NoWarn);2003;CS8002;NU1608;</NoWarn> <!-- AssemblyInformationalVersionAttribute contains a non-standard value -->
<Deterministic Condition="'$(NCrunch)' == '1'">false</Deterministic>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>;NU5104;2003;8002</NoWarn>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.DotNet.Interactive.Formatting\Microsoft.DotNet.Interactive.Formatting.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive\Microsoft.DotNet.Interactive.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.0-rc.1" />
</ItemGroup>
<ItemGroup>
<Content Include="Modules\**" PackageCopyToOutput="true">
<Link>Modules\%(RecursiveDir)\%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#
# Module manifest for module 'Microsoft.DotNet.Interactive.PowerShell'
#
# Generated by: tyleonha
#
# Generated on: 1/23/2020
#

@{

# Script module or binary module file associated with this manifest.
# RootModule = ''

# Version number of this module.
ModuleVersion = '0.0.1'

# Supported PSEditions
# CompatiblePSEditions = @()

# ID used to uniquely identify this module
GUID = 'c3ca4cca-fd49-4b1e-a79e-b12afb026068'

# Author of this module
Author = 'Microsoft Corporation'

# Company or vendor of this module
CompanyName = 'Microsoft Corporation'

# Copyright statement for this module
Copyright = '(c) Microsoft Corporation. All rights reserved.'

# Description of the functionality provided by this module
# Description = ''

# Minimum version of the PowerShell engine required by this module
# PowerShellVersion = ''

# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# CLRVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @('Microsoft.DotNet.Interactive.PowerShell.dll')

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @()

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @('Trace-PipelineObject')

# Variables to export from this module
VariablesToExport = '*'

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()

# A URL to the license for this module.
# LicenseUri = ''

# A URL to the main website for this project.
# ProjectUri = ''

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false

# External dependent modules of this module
# ExternalModuleDependencies = @()

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}
67 changes: 67 additions & 0 deletions Microsoft.DotNet.Interactive.PowerShell/PowerShellExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections;
using System.Collections.ObjectModel;

namespace Microsoft.DotNet.Interactive.PowerShell
{
using System.Management.Automation;
Comment thread
jonsequitur marked this conversation as resolved.
Resolved

internal static class PowerShellExtensions
{
public static void InvokeAndClearCommands(this PowerShell pwsh)
{
try
{
pwsh.Invoke();
}
finally
{
pwsh.Streams.ClearStreams();
pwsh.Commands.Clear();
}
}

public static void InvokeAndClearCommands(this PowerShell pwsh, IEnumerable input)
{
try
{
pwsh.Invoke(input);
}
finally
{
pwsh.Streams.ClearStreams();
pwsh.Commands.Clear();
}
}

public static Collection<T> InvokeAndClearCommands<T>(this PowerShell pwsh)
{
try
{
var result = pwsh.Invoke<T>();
return result;
}
finally
{
pwsh.Streams.ClearStreams();
pwsh.Commands.Clear();
}
}

public static Collection<T> InvokeAndClearCommands<T>(this PowerShell pwsh, IEnumerable input)
{
try
{
var result = pwsh.Invoke<T>(input);
return result;
}
finally
{
pwsh.Streams.ClearStreams();
pwsh.Commands.Clear();
}
}
}
}
Loading