VMware Cloud Community
icehckyplyr22
Enthusiast
Enthusiast
Jump to solution

Running PowerCLI Connect VIServer & Get-VM as a single command

I am trying to create a script which allows me to output the results of the cmdlet "get-vm | get-snapshot | format-list" to a file


I am using a piece of software which allows us to remotely execute scripts either via the Command Prompt or PowerShell


How could I create a single line command which would run the command Connect-VIServer vCenterServerName  & then the get-vm | get-snapshot | format-list??


I am adding variables via our software for the username/password & vmware server IP. These variables have to be part of the command, for example:


Connect-VIServer -Server @VMwareHostIP@ -Protocol https -User @Username@ -Password @Password@


Is there a way I could call the PowerCLI from the Windows command prompt?

0 Kudos
1 Solution

Accepted Solutions
kunaludapi
Expert
Expert
Jump to solution

You can try below from command prompt

powershell "add-pssnapin vmware.vimautomation.core"; "Connect-VIServer -Server @VMwareHostIP@ -Protocol https -User @Username@ -Password @Password@"; "get-vm | get-snapshot | format-list"


separate each commad with ";" and best practice is Quote unquote each command.

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".

View solution in original post

0 Kudos
4 Replies
kunaludapi
Expert
Expert
Jump to solution

You can try below from command prompt

powershell "add-pssnapin vmware.vimautomation.core"; "Connect-VIServer -Server @VMwareHostIP@ -Protocol https -User @Username@ -Password @Password@"; "get-vm | get-snapshot | format-list"


separate each commad with ";" and best practice is Quote unquote each command.

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
icehckyplyr22
Enthusiast
Enthusiast
Jump to solution

That doesnt seem to output anything 😕

0 Kudos
icehckyplyr22
Enthusiast
Enthusiast
Jump to solution

If I dump that to a txt file

WARNING: There were one or more problems with the server certificate:

* The X509 chain could not be built up to the root certificate.

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

Certificate: [Subject]

  OID.1.2.840.113549.1.9.2="1262530446,564d7761726520496e632e",

CN=localhost.localdomain, E=ssl-certificates@vmware.com, OU=VMware ESX Server

Default Certificate, O="VMware, Inc", L=Palo Alto, S=California, C=US

[Issuer]

  O=VMware Installer

[Serial Number]

  454472306979416A

[Not Before]

  1/3/2010 6:54:06 AM

[Not After]

  7/4/2021 7:54:06 AM

[Thumbprint]

  059369C0E7AB3709CA6D3917015CBCCDA1946ACB

The server certificate is not valid.

   

WARNING: THE DEFAULT BEHAVIOR UPON INVALID SERVER CERTIFICATE WILL CHANGE IN A

FUTURE RELEASE. To ensure scripts are not affected by the change, use

Set-PowerCLIConfiguration to set a value for the InvalidCertificateAction

option.

      

Name                           Port  User                         

----                           ----  ----                         

192.168.0.10                   443   root                         

out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.For

mat.FormatStartData" is not valid or not in the correct sequence. This is likel

y caused by a user-specified "format-list" command which is conflicting with th

e default formatting.

    + CategoryInfo          : InvalidData: (:) [out-lineoutput], InvalidOperat

   ionException

    + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.P

   owerShell.Commands.OutLineOutputCommand

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Drop the Format-List at the end


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference