Link to home
Start Free TrialLog in
Avatar of erlythornton
erlythornton

asked on

Construct a single windows command line to perform a VMware PowerCLI function or a script where I can pass parameters

Hello,

I need to execute a windows command line the invokes VMware PowerCLI to connect to my server and then perform a few more commands.  As a test I have tried to send out the following just to see if I could get an inventory to display and it gives me the following error.

C:\Documents and Settings\Administrator>C:\WINDOWS\system32\windowspowershell\v1
.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\
vim.psc1" -noe -c "Connect-VIServer -Server esxserver.company.com -Protocol https -User
 myusername -Password mypassword | Get-Inventory"
WARNING: There were one or more problems with the server certificate:

* A certification chain processed correctly, but terminated in a root
certificate which isn't trusted by the trust provider.

* The X509 chain is not valid due to an invalid time value, such as a value
that indicates an expired certificate.

* The certificate's CN name does not match the passed value.

Get-Inventory : The input object cannot be bound to any parameters for the comm
and either because the command does not take pipeline input or the input and it
s properties do not match any of the parameters that take pipeline input.
At line:1 char:116
+ Connect-VIServer -Server esxserver.company.com -Protocol https -User myusername -Pass
word mypassword | Get-Inventory <<<<

Alternatively I could try to do the connect in a batch file but then I would need to pass parameters to it from the command line also.  For example I could put the Connect-VIServer in the ps1 file but I need to pass the server hostname, user and password then in one of the later commands I need to pass some more variables.

Please help me with any suggestions as to what I am doing wrong and the command line only without having to use a ps1 file is preferable.
Avatar of Andrew Hancock (VMware vExpert PRO / EE Fellow/British Beekeeper)
Andrew Hancock (VMware vExpert PRO / EE Fellow/British Beekeeper)
Flag of United Kingdom of Great Britain and Northern Ireland image

"* A certification chain processed correctly, but terminated in a root
certificate which isn't trusted by the trust provider.

* The X509 chain is not valid due to an invalid time value, such as a value
that indicates an expired certificate.

* The certificate's CN name does not match the passed value."

the above is normal, because you've not replaced the SSL certificate on your VMware vCenter Server or ESX server. It should not affect the operation.

I would recommend using the following,

http://communities.vmware.com/community/vmtn/server/vsphere/automationtools/powercli

One of the simplest PowerCLI examples, and something which is actually extremely useful on its own, is the Get-VM cmdlet. Which lists VMs.

PowerCLI can be a bit overwhelming to use,  But it can be enhanced with a nice toolset and a library of preconfigured scripts to jump-start your mass changes, which is possible with PowerGUI

http://powergui.org/index.jspa

and

VMware Community Pack

http://www.virtu-al.net/featured-scripts/vmware-powerpack/

Avatar of erlythornton
erlythornton

ASKER

Thanks,

Changing to command to Get-VM * instead of Get-Inventory produces the same.  It complains about the pipe command not being something that can be associated with the command as if you cannot run a connect and another command afterwards at all.  The links you sent point to the top web sites for a lot of information.  Thanks but the reason I come to experts exchange is so I can get more specific examples.  What about the command above needs to be changed so I can perform a connection and then an operation or two using that connection?
have you successfully connected to vCenter?
Well the Connect-VIServer appears to work or at least it does not error out.  If I place a wrong username or password, it will error.  With the correct information there it does not give an error message leaving me with the assumption that it is succeeding.  No commands that I enter after that Connect-VIServer command work using the pipe as a way of incrementing to the next command.  It gives the error as mentioned above.

vim.psc1 appears to be the script that exposes the VMware PowerCLI interface scripts.  Look at question https://www.experts-exchange.com/questions/26662748/How-to-run-a-Power-Shell-script-using-PowerCLI.html

It is also part of the command that is run that starts the VMware VSphere PowerCLI Command Line window after it is installed.  
That vim.psc1 script comes with the VMWare PowerCLI installaton
I'm using Initialize-VIToolkitEnvironment.ps1
Replacing with that command causes this error:  

Windows PowerShell console file "C:\Program Files\VMware\Infrastructure\vSphere
PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1" extension is not psc1. Win
dows PowerShell console file extension must be psc1.

Let me explain my application reason more.  I have to be able to invoke this from a windows command prompt without user interaction.  I am working with an application that is like a scheduler that will execute a script using rules automatically.  That is why I must supply the command in a single line.  What is missing here is syntax on how to perform this.
ASKER CERTIFIED SOLUTION
Avatar of Andrew Hancock (VMware vExpert PRO / EE Fellow/British Beekeeper)
Andrew Hancock (VMware vExpert PRO / EE Fellow/British Beekeeper)
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is a freaking great solution and I can use it with my application!  Just what I needed.  Thanks So Much!