将Get-WmiObject添加到用于远程推送软件的某些代码时出现错误。我希望代码在继续之前检查当前安装的应用程序,但是当我运行它时,我收到错误:
Get-WmiObject:术语'Get-WmiObject'不被识别为 cmdlet的名称,函数,脚本文件或可运行的程序。校验 名称的拼写,或者如果包含路径,请验证 路径是正确的,然后再试一次。在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
}
}
答案 0 :(得分:1)
您是否安装了其他语言包?它明显抱怨Â。那么这意味着,如果你复制+粘贴某个人的博客/代码在不同的地区写下它们,他们可能会有一个特殊的角色。
修复:手动重写命令。将其粘贴到记事本中,看看会出现什么。在记事本中打开脚本,您将看到错误。
答案 1 :(得分:-1)
我能够通过将代码复制到新的.ps1文件并运行来修复此错误。没有进行任何其他更改。 :P