PowerShell / PowerShell/PowerShell
Incorrect output when calling PowerShell's Invoke function in C#
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 55.5k
- Forks
- 8.5k
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 88
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
I used Nuget to download and use the official Microsoft.PowerShell.SDK (7.1.0) and System.Management.Automation (7.1.0).
The purpose is to convert a piece of obfuscated code to origianl string. However, this code will truncate part of the execution results.
But when I use PowerShell7 and PowerShell5 to call the invoke method in powershell environment$PowerShell = [powershell]::Create();[void]$PowerShell.AddScript(......);$PowerShell.Invoke(), I can get the correct results. I suspect there are bugs in the officially provided SDK and Autoumation packages for C#.
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
namespace ClassLibrary2
{
public class Class1
{
static void Main(string[] args)
{
PowerShell psInstance = PowerShell.Create();
string script = "(NeW-oBjeCt MAnaGEment.AUtOmATiOn.pScReDeNtiAL ' ', ( '76492d1116743f0423413b16050a5345MgB8AHIAdgA3AFYATgBoAFMAUwBzAEMATgBRAFEAUgB2AGcASwA2AE4AagBlAEEAPQA9AHwANwBjADAAMgBkADcAYwAyADQANgA5ADUANwBlAGMANwBiAGEANQAwADMANwAzAGMAOQBlADkANwBjAGQAMQBlAGUAZAA3AGIAMQBjADQAYwBhAGMAMQAzADYANgBjADQAYwBjADkAOABmAGUAMABhADgANQBmADAANwBkADQAYQBjAGMAZgBjAGIAMAAyAGUANgBkADQAZAAyAGEAMgBiADkAYwA4ADAAZQA4AGUAYwAwAGMAYwBiAGUAZQA3ADgAZABmADYAYgAwADUAMAA0ADIAYgBjAGYANwA1ADcANwA5ADgAYgAwADAAZgA1ADUAMQAyADMANgBlAGYAOABjADkANwA0ADgANgAzADkANAA5ADQAOQBkAGMAMgAwADcANQA1AGMAMQAwADUANgA2ADEANgBlADYANQA2AGMAMQA='| CoNvertTO-SEcuREStrInG -KEY (216..185))).getNEtWorkCrEdEntIAL().PAsSWord";
psInstance.AddScript(script);
Collection<PSObject> psOutput;
psOutput = psInstance.Invoke();
}
}
}
Expected behavior
hello123456789hi123456789go123456789
Actual behavior
hello123456789hi123456789go12345
Error details
No response
Environment data
Microsoft Windows 10.0.19045
C#
.Net Core 7.0
PowerShell5
Name Value
---- -----
PSVersion 5.1.19041.4291
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.4291
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PowerShell7
Name Value
---- -----
PSVersion 7.5.0-preview.2
PSEdition Core
GitCommitId 7.5.0-preview.2
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the C# example using Microsoft.PowerShell.SDK, System.Management.Automation, PowerShell.Create(), AddScript(), and Invoke(), and reproduce the truncated output on the listed .NET and Windows environment. Trace the Invoke result path and compare it with PowerShell 5 and 7; done means the C# SDK returns the complete expected string and has coverage for this reproduction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, powershell
- Domain
- api, cli
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100