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.
erlythorntonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization ConsultantCommented:
"* 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/

erlythorntonAuthor 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 ConsultantCommented:
Do any commands work?
 Acronis Global Cyber Summit 2019 in Miami

The Acronis Global Cyber Summit 2019 will be held at the Fontainebleau Miami Beach Resort on October 13–16, 2019, and it promises to be the must-attend event for IT infrastructure managers, CIOs, service providers, value-added resellers, ISVs, and developers.

Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization ConsultantCommented:
have you successfully connected to vCenter?
Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization ConsultantCommented:
what is vim.psc1? can you upload here.
erlythorntonAuthor 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 ConsultantCommented:
is that the script which Bestway posted?
erlythorntonAuthor Commented:
That vim.psc1 script comes with the VMWare PowerCLI installaton
Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization ConsultantCommented:
I'm using Initialize-VIToolkitEnvironment.ps1
erlythorntonAuthor 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.
Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization ConsultantCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
erlythorntonAuthor 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 ConsultantCommented:
no problems, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.