Class error in script started with powershell.exe

Tagged: 

This topic contains 4 replies, has 3 voices, and was last updated by  Milos Ostojic 1 year, 3 months ago.

  • Author
    Posts
  • #39165

    Milos Ostojic
    Participant

    I was playing with classes in some WPF based tool I built for creating included lists for ADMT.
    Long story short, I created Class called ErrorMess with static method to throw messagebox with messageboxbutton and messageboximage.
    When the script is started from ISE or console everything is OK but when I start it from cmd with -file argument to powershell.exe it throws 'unable to find type' for every type used in ErrorMess class.
    While I was using all of them not wrapped in class everything worked fine. Tool was made for workmates who don't use powershell (I pity the fools) so it's kinda essential for it to be able to start with bat file. I could return to pre-class environment but I don't want to if don't have to...

    Any ideas?

    Thanks!

  • #39168

    Alex
    Participant

    It sounds like the PowerShell its calling from the cmd line is not version 5. try doing something like

    powershell.exe -command "$PSVersionTable"
    

    from the command line and see if it returns "PSVersion 5.0......."
    This would determine if its using the latest version of PowerShell.

  • #39175

    Milos Ostojic
    Participant

    $psversion returns 5.0xxxx. it is using the latest one

  • #39176

    Dave Wyatt
    Moderator

    When you're out in powershell.exe, you probably need to add this line to your script:

    Add-Type -AssemblyName System.Windows.Forms

    This isn't necessary in the ISE since it's a WinForms app already.

  • #39180

    Milos Ostojic
    Participant

    Thanks Dave, I'm getting closer. I haven't noticed that script throws the same error in console. But adding windows.forms doesn't help. I'm using WPF instead of forms for messagebox (system.windows.messagebox). So i added this 'Add-Type -assemblyName PresentationFramework' to the begining of the script.
    That doesn't help either, but when I manually type the same command (or paste it) in console before starting script, script works.

    What am I doing wrong now?

You must be logged in to reply to this topic.