We help IT Professionals succeed at work.

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

erlythornton
erlythornton asked
on
5,017 Views
Last Modified: 2012-05-11
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.
Comment
Watch Question

Andrew Hancock (VMware vExpert PRO / EE Fellow)VMware and Virtualization Consultant
CERTIFIED EXPERT
Fellow
Expert of the Year 2017

Commented:
"* 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/

Author

Commented:
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?
Andrew Hancock (VMware vExpert PRO / EE Fellow)VMware and Virtualization Consultant
CERTIFIED EXPERT
Fellow
Expert of the Year 2017

Commented:
Do any commands work?
Andrew Hancock (VMware vExpert PRO / EE Fellow)VMware and Virtualization Consultant
CERTIFIED EXPERT
Fellow
Expert of the Year 2017

Commented:
have you successfully connected to vCenter?
Andrew Hancock (VMware vExpert PRO / EE Fellow)VMware and Virtualization Consultant
CERTIFIED EXPERT
Fellow
Expert of the Year 2017

Commented:
what is vim.psc1? can you upload here.

Author

Commented:
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/Programming/Languages/Scripting/Powershell/Q_26662748.html

It is also part of the command that is run that starts the VMware VSphere PowerCLI Command Line window after it is installed.  
Andrew Hancock (VMware vExpert PRO / EE Fellow)VMware and Virtualization Consultant
CERTIFIED EXPERT
Fellow
Expert of the Year 2017

Commented:
is that the script which Bestway posted?

Author

Commented:
That vim.psc1 script comes with the VMWare PowerCLI installaton
Andrew Hancock (VMware vExpert PRO / EE Fellow)VMware and Virtualization Consultant
CERTIFIED EXPERT
Fellow
Expert of the Year 2017

Commented:
I'm using Initialize-VIToolkitEnvironment.ps1

Author

Commented:
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.
VMware and Virtualization Consultant
CERTIFIED EXPERT
Fellow
Expert of the Year 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
That is a freaking great solution and I can use it with my application!  Just what I needed.  Thanks So Much!
Andrew Hancock (VMware vExpert PRO / EE Fellow)VMware and Virtualization Consultant
CERTIFIED EXPERT
Fellow
Expert of the Year 2017

Commented:
no problems, glad to help.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.