簡體   English   中英

無法運行 VCenter powershell 腳本

[英]Unable to run VCenter powershell script

我正在嘗試連接到 VCenter 以提取一些性能數據。 當我從 powershell window 執行腳本時,出現錯誤:

這是我的腳本:

Connect-VIServer "vcenter.server.com" -User user123 -Password testpassword
$allvms = @()
$allhosts = @()
$hosts = Get-VMHost
$vms = Get-Vm

foreach($vmHost in $hosts){
  $hoststat = "" | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
  $hoststat.HostName = $vmHost.name

  $statcpu = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat cpu.usage.average
  $statmem = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat mem.usage.average

  $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
  $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum

  $hoststat.CPUMax = $cpu.Maximum
  $hoststat.CPUAvg = $cpu.Average
  $hoststat.CPUMin = $cpu.Minimum
  $hoststat.MemMax = $mem.Maximum
  $hoststat.MemAvg = $mem.Average
  $hoststat.MemMin = $mem.Minimum
  $allhosts += $hoststat
}
$allhosts | Select HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\output\Hosts.csv" -noTypeInformation

foreach($vm in $vms){
  $vmstat = "" | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
  $vmstat.VmName = $vm.name

  $statcpu = Get-Stat -Entity ($vm)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat cpu.usage.average
  $statmem = Get-Stat -Entity ($vm)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10-stat mem.usage.average

  $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
  $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum

  $vmstat.CPUMax = $cpu.Maximum
  $vmstat.CPUAvg = $cpu.Average
  $vmstat.CPUMin = $cpu.Minimum
  $vmstat.MemMax = $mem.Maximum
  $vmstat.MemAvg = $mem.Average
  $vmstat.MemMin = $mem.Minimum
  $allvms += $vmstat
}
$allvms | Select VmName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin | Export-Csv "c:\output\VMs.csv" -noTypeInformation

這些是錯誤:

The term 'Connect-VIServer' is not recognized as the name of a cmdlet, function, script file, or operable program. Chec
k the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\vm.ps1:1 char:17
+ Connect-VIServer <<<<  dc1prhsvspvc-01 -User haquem -Password Basketball1
    + CategoryInfo          : ObjectNotFound: (Connect-VIServer:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Get-VMHost' 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:\scripts\vm.ps1:4 char:20
+ $hosts = Get-VMHost <<<<
    + CategoryInfo          : ObjectNotFound: (Get-VMHost:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Get-Vm' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spel
ling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\vm.ps1:5 char:14
+ $vms = Get-Vm <<<<
    + CategoryInfo          : ObjectNotFound: (Get-Vm:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Get-Stat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the sp
elling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\vm.ps1:11 char:22
+   $statcpu = Get-Stat <<<<  -Entity ($vmHost)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat cpu
.usage.average
    + CategoryInfo          : ObjectNotFound: (Get-Stat:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The term 'Get-Stat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the sp
elling of the name, or if a path was included, verify that the path is correct and try again.
At C:\scripts\vm.ps1:12 char:22
+   $statmem = Get-Stat <<<<  -Entity ($vmHost)-start (get-date).AddDays(-1) -Finish (Get-Date)-MaxSamples 10 -stat mem
.usage.average
    + CategoryInfo          : ObjectNotFound: (Get-Stat:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我是 powershell 的新手,我真的很感謝這方面的幫助?

假設您已經安裝了PowerCLI管理單元...。

您需要使用Add-PSSnapIn添加適用於PowerShell的VMware vCenter管理單元。 最后我檢查了一下,它仍然使用舊的管理單元模型而不是新的模塊結構。

您應該具有“開始菜單”快捷方式以啟動VMware PowerShell控制台,該控制台應會自動為您添加該管理單元。

安裝 vmware“PowerCli”模塊

在我的筆記本電腦上 (winver.exe => Windows 10 Enterprise Version 21H2 ) 我在管理員 powershell 中運行了這個:

PS C:\> Install-Module -Name VMware.VimAutomation.Core -verbose -Force -AllowClobber

我用-Force來抑制提示。

我使用-AllowClobber因為沒有它我得到了這個錯誤:

PackageManagement\Install-Package : The following commands are already available on this system:'Get-Cluster,New-Cluster,Remove-Cluster'. This module 'VMware.VimAutomation.Core' may override the existing commands. If you still want to install this module
'VMware.VimAutomation.Core', use -AllowClobber parameter.

延伸閱讀:

將 REQUIRES 語句添加到腳本以獲得更好的錯誤消息

在較新的 PowerShell 版本中,您可以這樣做:

將此行添加到我們腳本的頂部(本例中為“Get-Vms.ps”):

#Requires -Modules VMware.VimAutomation.Core

然后,如果您嘗試在未安裝該模塊的情況下運行腳本,您不會收到一堆奇怪的錯誤消息,而是收到一條簡單明了的錯誤消息:

PS C:\> .\Get-VMs.ps1
.\Get-VMs.ps1 : The script 'Get-VMs.ps1' cannot be run because the following modules that are specified by the "#requires" statements of the script are missing: VMware.VimAutomation.Core.
At line:1 char:1
+ .\Get-VMs.ps1
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (Get-VMs.ps1:String) [], ScriptRequiresException
    + FullyQualifiedErrorId : ScriptRequiresMissingModules

延伸閱讀: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM