Connect to vCenter Server within PowerCLI script

In my previous article I wrote about a scheduled PowerCLI script. If you use a PowerCLI the one of the first command is to log in to a vCenter Server. During the log in you need to authenticate. If you need to run your script fully automated, use the Windows Task Scheduler, you will have some problem. Here is the solution.

A normal PowerCLI looks like the following. An authentication window appears for your credentials.

A fully automated script will not run if you use the above used command. You should create a new user, who has an appropriate right in the vCenter Server. I create a script, which create a daily/monthly inventory, so a read right was enough for me. I created an svc_inventory user. Here is the login part of my script.

</pre>
<pre class="brush: powershell;">$VIConnection = Connect-VIServer testvCenter  –user “svc_inventory”
 –Pass “Asdf1234” –Protocol Https

if (-not $VIConnection.IsConnected) {

Write-Host "Unable to connect to vCenter, please ensure you have altered the
 vCenter server address correctly”

Write-Host " to specify a username and password edit the connection string"

break

}</pre>
<div>

That’s all!

Advertisements