1

I'm trying to get MessageBox dialog called from a class using the following code

Add-Type -Assembly PresentationFramework

[System.Windows.MessageBox]::Show('MessageBox is out of the class')

 Class TestClass {
    TestClass(){
        [System.Windows.MessageBox]::Show('MessageBox is from the class')
    }
 }

$null = [TestClass]::new()

There are two dialogs shown when I use Powershell ISE however I got an error when I've run ps-script through cmd

>powershell.exe -file ClassTest.ps1
At D:\ClassTest.ps1:7 char:10
+         [System.Windows.MessageBox]::Show('MessageBox is from the cla ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.MessageBox].
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TypeNotFound

How can I get a MessageBox dialog correctly?

P.S. Win10, Powershell 5.1.19041.1320

chainick
  • 11
  • 2
  • 1
    I truly believe this is a duplicate question and theres already an answer for it but, I just can't find it. Basically, since PowerShell parses the entirety of the script before executing, any unresolvable type name inside your class will be read as a parse error. You can wrap the entirety of it in a *here string*, and call on it using `Invoke-Expression` like so: `Invoke-Expression @'..'@` and it should execute. Will flag your question as duplicate once I find that post. Just want to get ya in the right direction in the meantime. – Abraham Zinala Jan 01 '22 at 03:35
  • 2
    AHA! [Found it](https://stackoverflow.com/questions/34625440/using-net-objects-within-a-powershell-v5-class)! :) – Abraham Zinala Jan 01 '22 at 03:41

0 Answers0