Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:41:05+00:00 2026-06-04T07:41:05+00:00

Salutations! So, I was just on my daily routine of some powershell programming and

  • 0

Salutations!

So, I was just on my daily routine of some powershell programming and I got to this little fella:

Get-WmiObject : Invalid class 
At line:184 char:19
+ $RECApp = gwmi <<<<  Win32_product | ? {$_.Name.Contains($Application)}
+ CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId: GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

The funny thing is that this error is only shown when the code is executed from a script, but when manually entered into the command line the same code works. I don’t know if this has something to do with the remote machine which I’m executing the script on, but why should it, if it works when entered manually but not when executed from the script. I’ll get you a piece of my code here:

Enter-PSSession -ComputerName $serverName
$App = gwmi Win32_product | ? {$_.Name.Contains($Application)}
$App.Uninstall();
exit

To summarize, why does this code work like a charm when entered manually to the command line, but when executed form a script I get the above error?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:41:06+00:00Added an answer on June 4, 2026 at 7:41 am

    Enter-PSSession is for interactive use only. If you put that line in a script, the subsequent lines do not run in a remote session. If you want to run some script remotely, within a script, you would do something like this instead:

    $session = New-PSSession -ComputerName $serverName
    Invoke-Command -Session $session {
        param($name)
    
        $App = gwmi Win32_product | ? {$_.Name.Contains($name)}
        $App.Uninstall();
    } -arguments $application
    

    There’s a bit of a gotcha there as I have to pass $application as an argument to Invoke-Command because that variable does not exist on the remote session. Stepping back a bit, you could write the above a bit simpler like this:

    $app = gwmi -computer $servername win32_product | ? {
          $_.name.contains($application)
    }
    

    Now, the problem with this is that you are pulling all win32_product objects back from the server and filtering on the local machine. This is a lot of unneccessary network traffic. It would be faster if you could filter on the remote machine, so let’s modify it a bit more:

    $app = gwmi -computer $servername -query `
        "select * from win32_product where name = '$application'"
    $app.Uninstall()
    

    Now only the win32_product you want will be retrieved from the remote machine. I haven’t tested the above gwmi -computer variants, so it’s more illustrative of technique and syntax. You may have to play a bit with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings and salutations! I've put together a nice floating form CSS file for some
I know this is embarrassing easy but I cannot get this to work right
I have a line like this in my view: <div class=contact> <h6>@Model.Salutation</h6><h3>@Model.FirstName @Model.LastName</h3> </div>
Greetings and Salutations, My web application utilizes the EvalMath class found at PHPClasses. Unfortunately,
greetings & salutations, this is my first run with the google maps API, i'm
This may be a toughie so you get bonus points! THIS IS A .TXT
File1: hello - dictionary definitions: hi hello hallo greetings salutations no more hello for
I have pattern like below hi hello hallo greetings salutations no more hello for
Salutations, I am having problems allowing user's(admin) the ability to upload files/images to the
I have a data access class with an Enum called Salutation: public enum Salutation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.