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

erlythornton
erlythornton used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
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 / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
Expert of the Year 2017

Commented:
Do any commands work?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

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

Commented:
have you successfully connected to vCenter?
Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
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 http://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 / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
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 / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
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
Fellow 2018
Expert of the Year 2017
Commented:
Okay, have a look at the following

 
#Quick and Dirty script

Param (
  
    [string] $vimHost,
    [string] $vimUser,
    [string] $vimPass
 )

#Loads VMware's PowerCLI PowerShell Cmdlets if necessary
[void](Get-PSSnapin VMWare.VimAutomation.Core -ErrorVariable getVmwareSnapinErr 2> $null)
if ($getVmwareSnapinErr.Count -gt 0) {    Add-PSSnapin VMware.VimAutomation.Core }

#Establish a connection to the VI Server based on input parameters
Connect-VIServer $vimHost -User $vimUser -Password $vimPass

#Enter Commandlets for use e.g. Get-Inventory
Get-Inventory

Open in new window



execute in a batch file or on command line

powershell -command "& 'C:\scriptname.ps1' -vimHost vcenterserver -vimuser vcenter-user -vimpass password"

you could store the variables in the script, but personally I don't like passwords in files.

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 / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
Expert of the Year 2017

Commented:
no problems, glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial