Opening the PowerCLI shortcut just to run a script gets a bit old when you basically live in it. I have scripts that I can open to do simple tasks like changing network adapters or adding a HDD, usually you would have to open the PowerCLI window and then cd to the directory where your scripts are and then execute them. Obviously that gets old, there is a better way! We can import the PowerCLI snapin and then automatically connect to your vCenter server. Heres how:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$vcserver = 'SERVER NAME' try { Add-PSSnapin VMware.VimAutomation.Core $host.ui.rawui.WindowTitle="PowerShell [PowerCLI Module Loaded]" Write-Host "PowerCLI imported Succesfully" -ForegroundColor green -backgroundcolor black Write-Host "Connecting to vCenter, please wait.." -ForegroundColor yellow -backgroundcolor black #Connect to vCenter Connect-ViServer $vcserver } catch [Exception]{ Write-Host "Ouch it broke!" -ForegroundColor red -backgroundcolor black $_.Exception.Message $_.Exception.ItemName } |
In this example you would have to add your vCenter server var to every script. That can get old as well, especially if you have a ton of scripts. A great way to fix this is to add a new environment variable to your computer (ie, vcserver = SERVER NAME), once that is done you can access that variable from any PS script by using $env:vcserver. Hope that helps!