Get-WmiObject not being recognized in code

4.9k Views Asked by At

Im getting an error when adding the Get-WmiObject to some code i use to push software remotely. I want the code to check what applications are currently installed before continuing, but when i run it, i get an error:

Get-WmiObject : The term 'Get-WmiObjectÂ' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\code\getwmiobj.ps1:1 char:1 + Get-WmiObject -Class "win32_Product" -ComputerName "$computer" | ... + ~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-WmiObjectÂ:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

Here is my code:

$choices = [System.Management.Automation.Host.ChoiceDescription[]] @("&Y","&N")
while ( $true ) 
{
$computer= Read-Host "Computer that needs software installed or uninstalled" 
Get-WmiObject -Class "win32_Product" -ComputerName "$computer" | Out-GridView -Title "list of programs installed"
$installed = read-host "Is the software already installed (Y or N)?"
if ($installed -eq "N") 
    { 

    if (!$computer) 
        { 
        Write-Host "You did not give me a computer name. Please re-run with computer name or IP address" 
        Pause
        Exit
        }

    $ping = (Test-Connection -ComputerName $computer -Count 1 -quiet)
    $build= "c$\build\"
    $psexec= "\\bconac01\ds-support\gs_IT\office\scripts, Fixes and Tools\Remote install tool\Common install\psExec64.exe"
    $deployappexe="\\bconac01\ds-support\gs_IT\office\scripts, Fixes and Tools\Remote install tool\Common install\Deploy-Application.exe"
    $deployappcfg="\\bconac01\ds-support\gs_IT\office\scripts, Fixes and Tools\Remote install tool\Common install\Deploy-Application.exe.config"
    $swType= Get-ChildItem "\\bconac01\ds-prod\apps\DesktopServices" | Out-GridView -Title "Type of install needed" -PassThru | Select-object   
    $SW= Get-ChildItem "\\bconac01\ds-prod\apps\DesktopServices\$swType\" | Out-GridView -Title "What do you want to install" -PassThru | Select-object 
    $swloc= "\\bconac01\ds-prod\apps\DesktopServices\$swType\$SW\package\"
    $ps= Get-ChildItem $swloc | Out-GridView -Title "what installer would you like to use?" -PassThru | Select-Object -expandproperty name
    $uninstall= read-host "Is this an uninstall? (Y or N) (Default is install)"
    if ($uninstall -eq "Y") {$installchoice= "uninstall"} else {$installchoice= "install" }



        if ($ping -eq "true") 
            {

            Copy-item -Path $swloc -Recurse -Destination \\$computer\$build
            Copy-Item -Path $psexec -Destination \\$computer\$build\package
            Copy-item -Path $deployappexe -Force -Destination  \\$computer\$build\package
            Copy-item -Path $deployappcfg -Force -Destination  \\$computer\$build\package
            Set-Location \\$computer\$build\package
            .\psExec64.exe \\$computer "\\$computer\$build\package\Deploy-Application.exe" "$ps" -DeploymentType "$installchoice" -DeployMode "Interactive" 
            Set-Location -Path 'C:\Code'
            Remove-item -Path \\$computer\$build\package -Recurse -Force
            Write-Host " $installchoice of $SW on $computer is complete."
            Pause
            }
        else 
            {
            Write-Host "Unable to ping $computer at this time. Try re-running with the computers IP address" 
            Pause
            }
        }
else 
    { 
    $choice = $Host.UI.PromptForChoice("Repeat the script?","",$choices,0)
    if ( $choice -ne 0 ) 
        {
        break
        }
    }

$choice = $Host.UI.PromptForChoice("Repeat the script?","",$choices,0)
if ( $choice -ne 0 ) 
    {
    break
    }
}
3

There are 3 best solutions below

1
On

Do you have a different language package installed? Its clearly complaining about the Â. So what that means, is if you Copy + Pasted off someone's blog/code that wrote it in a different region, they might have a special character.

Fix: Re-Write the command by hand. Paste it into notepad and see what comes up. Open the script in Notepad and you will see what is wrong.

0
On

I was able to fix this error by copying the code into a new .ps1 file and running. No other changes were made. :P

0
On

The original asker clearly had a problem with an extra character, but in case someone comes here actually having problems with Get-WmiObject, they might note that this command was deprecated, so I fixed this by using PS 5.1 rather than 7.3 (at some point I installed both).

Failing:

(base) PS C:\Users\tom> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.0
PSEdition                      Core
GitCommitId                    7.3.0
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

Succeeding:

(base) PS C:\Users\tom> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1682
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1682
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Of course, I should probably update my scripts instead. That GH thread suggests that Get-CimInstance might be a valid replacement, though I don't know right now if the arguments are the same.