无法运行VCenter powershell脚本

时间:2014-08-20 14:15:26

标签: powershell vmware

我正在尝试连接到VCenter来提取一些性能数据。当我从powershell窗口执行脚本时,出现错误:

这是我的剧本:

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很新,我非常感谢你对此有任何帮助吗?

1 个答案:

答案 0 :(得分:1)

假设您已经安装了PowerCLI管理单元....

您需要使用Add-PSSnapIn为PowerShell添加VMware vCenter管理单元。最后我检查过,它仍然使用旧的管理单元模型而不是较新的模块结构。

您应该有一个“开始菜单”快捷方式来启动VMware PowerShell控制台,该控制台应自动为您添加管理单元。