Solved

powershell Scripting - powercli for vmware

Posted on 2015-01-28
4
172 Views
Last Modified: 2015-02-11
Hi Guys,

I have run this script and for some reason it added the harddisks quite a few times, please can somone point out what i am doing wrong here.

thank you in advance.

$vm="DEV-FIN-APP-01","DEV-FIN-WEB-01","DEV-SQL-FIN-01","DEV-RDS-FIN-01"

$vm | % { 

    If ($_="DEV-FIN-APP-01")

        {
        
        Set-VM -VM $_ -NumCpu 4 -MemoryGB 8 -Notes "Request 55274 " -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 50 -ThinProvisioned:$false -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 100 -ThinProvisioned:$false -Confirm:$false

        }


    if ($_="DEV-FIN-WEB-01")
        {

        Set-VM -VM $_ -NumCpu 2 -MemoryGB 4 -Notes "Request 55274 " -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 50 -ThinProvisioned:$false -Confirm:$false
       

        }

   

     if ($_="DEV-SQL-FIN-01")
        {

        Set-VM -VM $_ -NumCpu 4 -MemoryGB 16 -Notes "Request 55274 " -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 50 -ThinProvisioned:$false -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 50 -ThinProvisioned:$false -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 300 -ThinProvisioned:$false -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 150 -ThinProvisioned:$false -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 300 -ThinProvisioned:$false -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 150 -ThinProvisioned:$false -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 75 -ThinProvisioned:$false -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 300 -ThinProvisioned:$false -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 150 -ThinProvisioned:$false -Confirm:$false

        }

      if ($_="DEV-RDS-FIN-01")

        {

        Set-VM -VM $_ -NumCpu 2 -MemoryGB 8 -Notes "Request 55274 " -Confirm:$false
        New-HardDisk -VM $_ -CapacityGB 50 -ThinProvisioned:$false -Confirm:$false
 
        }



	#Get-VM $_ | get-networkadapter | set-networkadapter -networkname "LND_WEB_2264" -Confirm:$false#

	#Enable copy and paste

	Get-vm $_ | New-AdvancedSetting -Name isolation.tools.copy.disable -Value FALSE -Confirm:$false -Force:$true

	Get-vm $_ | New-AdvancedSetting -Name isolation.tools.paste.disable -Value FALSE -Confirm:$false -Force:$true


}

Open in new window

0
Comment
Question by:Kay
  • 2
4 Comments
 

Author Comment

by:Kay
ID: 40575212
I also got this error on the powershell screen:

DEV-RDS-FIN-01       PoweredOff 2        8.000          
New-HardDisk : 28/01/2015 14:15:43    New-HardDisk        Operation is not valid due to the current state of the object.    
At line:46 char:9
+         New-HardDisk -VM $_ -CapacityGB 50 -ThinProvisioned:$false -Confirm:$fal ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-HardDisk], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewHardDisk
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40575286
Would this work for you?  There seems to be some repetition in the 3rd vm...
$vm1=@{ name = 'DEV-FIN-APP-01'; cpu=4; mem=8; notes='Request 55274 ';drv=@(50,100)}
$vm2=@{ name = 'DEV-FIN-WEB-01'; cpu=2; mem=4; notes='Request 55274 ';drv=@(50)}
$vm3=@{ name = 'DEV-SQL-FIN-01'; cpu=4;mem=16;notes='Request 55274 ';drv=@(50,50,300,150,300,150,75,300,150)}
$vm4=@{ name = 'DEV-RDS-FIN-01'; cpu=2;mem=8;notes='Request 55274 ';drv=@(50)}
$vms = $vm1,$vm2,$vm3,$vm4

foreach ($vm in $vms) {
    set-vm -vm $vm.name -numcpu $vm.cpu -memoryGB $vm.mem -Notes $vm.notes -confirm:$false
    foreach ($size in $vm.drv) {
        new-harddisk -vm $vm.name -capacityGB $size -ThinProvisioned:$false -Confirm:$false
    }

}

Open in new window

0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40575787
In your original script, the If conditions are using "=" when you should use "-eq".
If ($_="DEV-FIN-APP-01")
should be
If ($_ -eq "DEV-FIN-APP-01")
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40591944
How are you making out?
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show you HOW TO: Suppress Configuration Issues and Warnings Alert displayed in Summary status for ESXi 6.5 after enabling SSH or ESXi Shell.
When rebooting a vCenters 6.0 and try to connect using vSphere Client we get this issue "Invalid URL: The hostname could not parsed." When we get this error we need to do some changes in the vCenter advanced settings to fix the issue.
Teach the user how to join ESXi hosts to Active Directory domains Open vSphere Client: Join ESXi host to AD domain: Verify ESXi computer account in AD: Configure permissions for domain user in ESXi: Test domain user login to ESXi host:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question