Powershell: A lowly script kiddy with no idea what he's doing (was attempting a "Hello World" in C#

Created on 23 Jul 2020  路  9Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

Was typing this

using System;
class Hello
{
static void Main() {
Console.WriteLine("My, Dick");
}
replace "System;" with command; and it will crash and yield the result

Last 200 Keys: Console.WriteLine("My, Dick");
DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow Enter
UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow DownArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow LeftArrow RightArrow Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Space Backspace Backspace ; Space LeftArrow LeftArrow LeftArrow RightArrow Backspace Backspace Backspace Backspace Backspace Backspace C o m m a n d


Expected behavior

Windows powershell displays text: "My Dick"

Actual behavior

ErRoR

Environment data


Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Management.Automation.Language.Parser.UsingStatementRule(Token usingToken)
at System.Management.Automation.Language.Parser.UsingStatementsRule()
at System.Management.Automation.Language.Parser.ScriptBlockRule(Token lCurly, Boolean isFilter, StatementAst predefinedStatementAst)
at System.Management.Automation.Language.Parser.ParseTask(String fileName, String input, List1 tokenList, Boolean recursed, ParseMode parseMode) at System.Management.Automation.Language.Parser.Parse(String fileName, String input, List1 tokenList, ParseError[]& errors, ParseMode parseMode)
at System.Management.Automation.Language.Parser.ParseInput(String input, String fileName, Token[]& tokens, ParseError[]& errors)


Issue-Bug

Most helpful comment

Write-Host 'Hello World'

Or, if you're especially lazy:

"Hello World"

All 9 comments

Probably shouldn't crash here, but PowerShell isn't expected to parse C# syntax regardless. No idea what you're trying to do, but the only way to run C# code in PowerShell is via Add-Type -TypeDefinition "<code in a string>"

The null reference exception is thrown from parser, which should be fixed. The parser should be able to take any text without crashing.

Only repos in 5.1 for me.

But honestly... that's one of the few things I'd say maybe should be back ported. Parser throwing NRE is scary.

Probably shouldn't crash here, but PowerShell isn't expected to parse C# syntax regardless. No idea what you're trying to do, but the only way to run C# code in PowerShell is via Add-Type -TypeDefinition "<code in a string>"

so how exactly would a "hello world" look in powershell?

Write-Host 'Hello World'

Or, if you're especially lazy:

"Hello World"
Write-Host 'Hello World'

Or, if you're especially lazy:

"Hello World"

whoah whoah. I mean specifically in c# my bad

Oh, right, sorry!

C# in PowerShell will be pretty much 1:1 for standard c# (most of the time), just wrap it in some quotes to make it a string and pass it to Add-Type:

Add-Type -TypeDefinition @"
using System;
public class Hello
{
    public static void World() {
        Console.WriteLine("Hello World!");
    }
}
"@
# Call the created class method from PS.
[Hello]::World()

Note that you'll often need to make the class and at least one of the methods public to be able to access them in PS.

Add-Type : Cannot add type. The type name 'Hello' already exists.
At line:1 char:1

  • Add-Type -TypeDefinition @"
  • ~~~~~~~

    • CategoryInfo : InvalidOperation: (Hello:String) [Add-Type], Exception

    • FullyQualifiedErrorId : TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand

      :(

You can't define the same type name over and over in PowerShell. Change the class name to Hello2 or restart PowerShell.

Was this page helpful?
0 / 5 - 0 ratings