VMware Cloud Community
GuilhermeStela
Enthusiast
Enthusiast
Jump to solution

Execute PowerCLi scripts out of PowerCli Console?

Is there a way to execute PowerCLi scripts out of PowerCli Console ?

I want to use batch commands together with PowerCli commands... and i'm stuck on putting keyboard entries on PowerCli console.Smiley Sad

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You don't need the Harddisk parameter, the harddisk is passed through the pipeline from the Get-Harddisk cmdlet.

If the VM has more than 1 harddisk, you use a Where-clause to select the harddisk you want.

Get-HardDisk -VM "MV0857-gastela" |

where {$_.Name -eq "Hard Disk 2"} |

Set-HardDisk -CapacityKB 20000000 -Confirm:$true

You can call PowerCLI cmdlets, but make sure to connect first.

Something like this for example

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile  "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1"  -command "connect-viserver MyVC | out-null; Get-VM"

Did you set the PS execution policy ?

Do a

Get-ExecutionPolicy

To change you could do

Set-ExeecutionPolicy RemoteSigned

You only need to do this once.


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

I assume you mean running scripts from the PowerCLI prompt ?

If yes, then if you are located in the folder where your script is stored, you can just do

PS C:\> .\myscript.ps1

or you can use the full path to the script

PS C:\> D:\Scripts\MyScript.ps1

And yes, you can mix console commands with PS cmdlets in a .ps1 file.


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

0 Kudos
GuilhermeStela
Enthusiast
Enthusiast
Jump to solution

In really i want to expand and after extend MV's disks buit i don't have necessary permissions to access the hosts directly so i just have access to expand it.

I have a batch that receives 1 entry parameter( VM's DNS Name ) and i use it to extend the VM's Disk . This batch was built using psexec and diskpart.

I need to expand the disk of the VM using Powercli cmdlets and after that start the batch script i have created.

I have seen before someone that open cmd prompt, enter the powershell path and run a cmdlet right there. but unfortunately i didn't remember where or who did it..

As well, if i could run a cmdlet out of the PowerCli console it would be perfect, but if you have any other idea to get there i accept suggestions. :smileylaugh:

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can use the Set-Harddisk cmdlet to expand a disk.

Use the HelperVM parameter when you are expanding the system disk of a Windows OS.

Did you see something like this ?

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe “& ‘C:\script.ps1′”


In the .ps1 file you can enter all the PowerCLI cmdlets you need for the task.


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

GuilhermeStela
Enthusiast
Enthusiast
Jump to solution

To expand the disk i mount this line and work perfectly except by using HardDisk parameter:

Get-HardDisk -VM "%vm%" | Set-HardDisk -CapacityKB %extendedSize% -Confirm:$true

I've tried to put the String Name of the disk at the HardDisk parameter but it don't work...

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-HardDisk -VM "MV0857-gastela" | Set-HardDisk -CapacityKB 20000000 -HardDisk "Hard Disk 2" -Confirm:$true

Set-HardDisk : Cannot bind parameter 'HardDisk'. Cannot convert value "Hard Disk 2" to type "VMware.VimAutomation.ViCore.Types.V1.VirtualDevice.HardDisk". Error: "Invalid cast from 'System.String' to 'VMware.VimAutomation.ViCore.Types.V1
.VirtualDevice.HardDisk'."At line:1 char:78+ Get-HardDisk -VM MV0857-gastela | Set-HardDisk -CapacityKB 20000000 -HardDisk  <<<< "Hard Disk 2" -Confirm:$true

Yeah, i did see something like this but with one or more arguments thar makes the PowerShell executes PowerCli cmdlets.

Here is my try like you said:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe "& 'C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\extend.ps1'"

The term 'C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\extend.ps1' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.At line:1 char:2+ &  <<<< 'C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\extend.ps1'

As i was afraid the PowerShell don't execute Powercli cmdlets....

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You don't need the Harddisk parameter, the harddisk is passed through the pipeline from the Get-Harddisk cmdlet.

If the VM has more than 1 harddisk, you use a Where-clause to select the harddisk you want.

Get-HardDisk -VM "MV0857-gastela" |

where {$_.Name -eq "Hard Disk 2"} |

Set-HardDisk -CapacityKB 20000000 -Confirm:$true

You can call PowerCLI cmdlets, but make sure to connect first.

Something like this for example

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile  "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1"  -command "connect-viserver MyVC | out-null; Get-VM"

Did you set the PS execution policy ?

Do a

Get-ExecutionPolicy

To change you could do

Set-ExeecutionPolicy RemoteSigned

You only need to do this once.


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

0 Kudos
GuilhermeStela
Enthusiast
Enthusiast
Jump to solution

Right!

Here the VMs have more than one hard disk, and the Where-clause worked just fine. I just changed the Confirm parameter to $false.

To the one-line command i built like you showed, already with the connection:

C:\WINDOWS\system32\windowspowershell\v1.0 powershell.exe -PSConsoleFile  "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1"  -command "connect-viserver -server <myVCenter> -user <myUser> -password <myPassword>; Get-VM -name "MyVM""

About Execution Policy, yes, it is already as RemoteSigned....don't know why don't work

Well, that's all i need to continue from here. Thanks Luc.
For sure i'll need more help with Powercli. I have experience with programming with some  languages but ever learning... I begun on this area recently and i'm really enjoying.

If not bother, i'll appreciate if you appoint some other reading material beyond VMware's Documentation.

And yes, my english isn't that good. hahaha

Tks again!! =D

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I have a blog post called My PS library, that has some pointers.

As it happens there is a webcast tomorrow. The slides for that webcast contain more links to get started with PS and PowerCLI.


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

0 Kudos