powershell 代码中无法识别Get-WmiObject

u2nhd7ah  于 2022-12-13  发布在  Shell
关注(0)|答案(3)|浏览(500)

我在将Get-WmiObject添加到用于远程推送软件的某些代码时遇到错误。我希望代码在继续之前检查当前安装的应用程序,但当我运行它时,遇到错误:
获取WmiObject:术语“Get-WmiObject”未被识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。At C:\code\getwmiobj.ps1:1 char:1 + Get-WmiObject-Class“win32_Product”-ComputerName“$computer”|......+ ~~~~~~~~~~~~~~ +类别信息:找不到对象:(Get-WmiObject:字符串)[],命令未找到异常+完全限定的错误ID:未找到命令异常
下面是我的代码:

$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
    }
}
jhdbpxl9

jhdbpxl91#

你有一个不同的语言包安装?它显然抱怨.所以这意味着,如果你复制+粘贴了别人的博客/代码,写在不同的地区,他们可能有一个特殊的字符.
修正:重新编写命令。粘贴到记事本,看看会出现什么。打开记事本中的脚本,你会看到什么是错误的。

k3fezbri

k3fezbri2#

最初的asker显然有一个额外字符的问题,但如果有人来这里实际上有问题的Get-WmiObject,他们可能会注意到这个命令是过时的,所以我修复了这个问题,使用PS 5.1而不是7.3(在某个时候,我安装了两者)。
失败:

(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

后续:

(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

当然,我可能应该更新我的脚本,GH线程建议Get-CimInstance可能是一个有效的替换,尽管我现在不知道参数是否相同。

9q78igpj

9q78igpj3#

我可以通过将代码复制到新的.ps1文件中并运行来修复此错误。没有进行其他更改。:P

相关问题