2017-09-08 96 views
0

將Get-WmiObject添加到我用於遠程推送軟件的某些代碼時,會出現錯誤。我想要的代碼,以檢查哪些應用程序繼續之前當前安裝的,但是當我運行它,我得到一個錯誤:Get-WmiObject無法在代碼中識別

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

這裏是我的代碼:

$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 
    } 
} 
+4

這顯然是一個編碼問題。你的腳本是用'utf8'編碼的嗎? – TheIncorrigible1

+0

我不確定這是什麼意思。我剛剛讓我的同事運行它,它在他的Windows 10機器上工作,所以我的機器上顯然有錯... – aroden20

回答

1

你有不同的語言包安裝?它明顯抱怨Â。那麼,這意味着,如果你複製+粘貼某人在不同地區寫過的博客/代碼,他們可能會有一個特殊字符。

修復:手動重寫命令。將其粘貼到記事本中,看看會發生什麼。在記事本中打開腳本,你會看到什麼是錯的。

+0

我從另一個只運行get-wmiobject的簡單腳本複製它,以查看遠程安裝了哪些軟件機器,但你的修復確實奏效。感謝您的建議!非常感激! – aroden20

-1

我能夠通過將代碼複製到一個新的.ps1文件並運行來解決此錯誤。沒有其他變化。 :P