整合PowerCLI与PowerShell

早先我有写一篇文章关于如何手工整合PowerCLI和PowerShell。最近重装了系统,不得不再做一次。以前我习惯用PowerGUI写脚本,但不知道怎么搞的PowerGUI总是丢失菜单界面,这困扰了我很久一直没办法完全解决,所以这次我在想能不能把PowerCLI和PowerShell整合了一劳永逸,毕竟内置的好用一些啊。

其实我需要的很简单,按下TAB键可以自动补全PowerCLI命令,人年纪大了实在记不住那么多命令行。搜了一下,找到一篇不错的文章 how to add PowerCLI to PowerShell ISE。但是我希望以后重装系统后能够快速整合,不用鼠标一个一个点。以下是我用来整合PowerCLI和PowerShell的命令行,希望对大家有用。:-)

第一步,以下命令必须返回“false”,否则你需要备份PowerShell的profile文件。

Test-Path $profile

第二步,创建PowerShell的profile文件并且检验是否创建成功,返回”true”表示成功。

New-Item -Path $profile -type file -force | Out-Null
Test-Path $profile

第三步,添加PowerCLI到PowerShell中。

Add-Content -Path $profile -value "# Load Windows PowerShell cmdlets for managing vSphere `r`nAdd-PsSnapin VMware.VimAutomation.Core -ea SilentlyContinue"

注意:

  1. 我只在Windows 10上测试过。Windows 7或者早前版本可能不一样。
  2. 整合后需要重启机器,否则运行PowerShell时会出现如下错误。
    A provider is not available for the following required component service: CisCoreService