代码之家  ›  专栏  ›  技术社区  ›  user1471980

无法运行VCenter powershell脚本

  •  0
  • user1471980  · 技术社区  · 11 年前

    我正在尝试连接到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很陌生,我真的很感谢你的帮助?

    2 回复  |  直到 11 年前
        1
  •  1
  •   user189198 user189198    11 年前

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

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

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

        2
  •  0
  •   StackzOfZtuff    4 年前

    安装vmware“PowerCli”模块

    在我的笔记本电脑上(winver.exe=> Windows 10 Enterprise版本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