Quantcast
Channel: VMware Communities : All Content - VMware PowerCLI
Viewing all 16717 articles
Browse latest View live

Get List of VMs, Datastores and VMDK / path per Cluster

$
0
0

Hi Folks,

 

as I´m new to this stuff, I´m wondering if anybody has already done some scripting work to gather a list of all virtual machines, its corresponding datastore(s) and the path to the VMDK-Files (or at least the name of it) - I know this part is already solved - found a couple of them ;-)

But my problem is, I need this script drilled down per Cluster or Datacenter. As I said I´m completly new to this CLI-Stuff and don´t have a clue about how to solve this..

 

Cheers

Markus


script not returning any datastore info

$
0
0

hi everyone, I think I am missing something simple but cant find the tail of it.

here is a commonly used script that Ive been using in the past but ever since i moved to a different machine its not returning the datastore info.

 

My CSV is full of great info but the column for Datastore is empty.  Thanks in advance.

$VCServerName = "myVcenter"
$VC = Connect-VIServer $VCServerName
$ExportFilePath = "c:\Export-VMInfo.csv"
$Report = @()
$VMs = Get-VM
$Datastores = Get-Datastore | select Name, Id
$VMHosts = Get-VMHost | select Name, Parent
ForEach ($VM in $VMs) {
      $VMView = $VM | Get-View
      $VMInfo = {} | Select VMName,Powerstate,OS,Folder,IPAddress,ToolsStatus,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb, DiskFree, DiskUsed
      $VMInfo.VMName = $vm.name
      $VMInfo.Powerstate = $vm.Powerstate
      $VMInfo.OS = $vm.Guest.OSFullName
      $VMInfo.IPAddress = $vm.Guest.IPAddress[0]
      $VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
      $VMInfo.Host = $vm.host.name
      $VMInfo.Cluster = $vm.host.Parent.Name
      $VMInfo.Datastore = ($Datastores | where {$_.ID -match (($vmview.Datastore | Select -First 1) | Select Value).Value} | Select Name).Name
      $VMInfo.NumCPU = $vm.NumCPU
      $VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)
      $VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)
      $VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB),2)
      $VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree
      $Report += $VMInfo
}
$Report = $Report | Sort-Object VMName
IF ($Report -ne "") {
$report | Export-Csv $ExportFilePath -NoTypeInformation
}
$VC = Disconnect-VIServer -Confirm:$False

Need to get vm ip info to change nic type

$
0
0

Hi,

I need to change the nic type on vms, but in order to do so I will need to get all of the pertinent IP information. This is because the server reverts to DHCP afer rebooting, following the changing of the nic with the command below. I have seen various commands, but the simpliest only works with 32 bit powercli which I do not have running at present (nor do I want to). Has anyone done this?

Thanks

get-vm servername | get-networkadapter | set-networkadapter -type "Vmxnet3" -Confirm:$false

 

 

 

get cdp nic info on all esxi servers

$
0
0

Is there a way to get cdp info on all nics of the esxi server in vcenter?

Problem changing SCSI Controller across multiple hard disks

$
0
0

I have two SCSI controllers

I have 4 LUNs (rawPhysical) that I'm trying to move to the 2nd SCSI controller.

relevent code so far is pretty much:

 

 

#gets the harddisks for "vmname"

$getDisk = Get-HardDisk -VM vmname

 

#this vm has two controllers, I want the second one, there's probably a better way to do this

$getCont = Get-ScsiController -VM vmname | Select -skip 1

 

#change the hard disks to the 2nd controller

Set-HardDisk -hardDisk $getDisk -Controller $getCont -confirm:$false

 

 

The error I'm getting is included in the text file. I can give more information if necessary. Thanks

 

Also, is there any way to add a hard disk to an existing scsi controller as it's created (new-harddisk)?

Need help with this script please...LucD ? :-)

$
0
0

Everything works as expected, but I have an issue with two parts of the script. They are the bits where it itterates the NIC and Hard Drives ( Pink and Blue sections below ).

 

Problem with the NIC loop ( Pink ) - It only shows nic 0, and if there are multiple nics - they are not output in the resulting CSV file. Also only returns NIC 0 MAC address and non of the other nic's MAC addresses.....

 

Problem with the disk loop ( Blue ) - it only returns the first TWO disks even though some of my VM's have 4 or even 5 disks...

 

Any idea why this is happening. This script is not my own work, but I have found it very useful for what I need...

 

Script :

 

 

Clear

$VirtualCenter = Read-Host "Please enter the name of the Virtual Center Server"

 

$FileLocation = Read-Host "Please Enter Complete Path and file name to save the output.  Must end in .csv or .txt"

$Cred = Get-Credential

Connect-VIServer $VirtualCenter -Credential $Cred

$stats = @()

 

#Uncomment the next two lines if you would like to inventory just a cluster instead of all VMs in vCenter

#$VMCluster = Read-Host "Please enter the name of the HA Cluster"

#$ServerList = Get-VM -Location $VMCluster

 

#If the two lines abover are uncommented, then comment the next line

$ServerList = Get-VM

 

Foreach ($Guests in $ServerList) {

    $Guest = $Guests.Name.ToUpper()

    Write-Progress -Activity "Creating VMware Guest Inventory" -Status "Processing VM Guest $Guest"# Display progress bar

    $VMGuest = Get-VM $Guest | Get-View

    $VM = Get-VM $Guest

    $ESXHost = (Get-VM $Guest).Host.Name.ToUpper()

    $VMHost = get-vmhost $ESXHost | Get-View

    $row = New-Object System.Object

    $row | Add-Member -Type NoteProperty -Name "Guest" -Value $VMGuest.Name.ToUpper()

    $row | Add-Member -Type NoteProperty -Name "Power State" -Value $VM.Guest.State

    $row | Add-Member -Type NoteProperty -Name "Guest OS Full Name" -Value $VM.Guest.OSFullName

    $row | Add-Member -Type NoteProperty -Name "Guest RAM (MB)" -Value $VM.MemoryMB

    $row | Add-Member -Type NoteProperty -Name "Guest vCPU Count" -Value $VM.NumCPU

    $row | Add-Member -Type NoteProperty -Name "Guest VMTools Status" -Value $VMGuest.Guest.ToolsStatus

    $row | Add-Member -Type NoteProperty -Name "Guest VMTools Version" -Value $VMGuest.Guest.ToolsVersion

    $row | Add-Member -Type NoteProperty -Name "Guest VMTools Version Status" -Value $VMGuest.Guest.ToolsVersionStatus

    $row | Add-Member -Type NoteProperty -Name "Guest VMTools Running Status" -Value $VMGuest.Guest.ToolsRunningStatus

   

   $NICCount = 0

    ForEach ($vNic in $VM.Guest.Nics){

 

        

        $NIC_IP = "Guest IP for NIC " + $NICCount + ""

        $NIC_MAC = "Guest MAC Address for NIC " + $NICCount + ""

        $NIC_vSwitch = "Guest vSwitch Network for NIC " + $NICCount + ""

        $row | Add-Member -Type NoteProperty -Name $NIC_IP -Value $VMGuest.Guest.IpAddress

        $row | Add-Member -Type NoteProperty -Name $NIC_MAC -Value $vNic.MacAddress

        $row | Add-Member -Type NoteProperty -Name $NIC_vSwitch -Value $vNic.NetworkName

        $NICCount++

    }

 

 

     $DiskCount = 0

    $DT = @()

    ForEach ($vDisk in $VM.Guest.Disks) {

        $DriveLetter = "Guest Drive " + $DiskCount + ""

        $DriveSize = "Guest Drive " + $DiskCount + " Size"

        $DriveFree = "Guest Drive " + $DiskCount + " Free Space"

        $vDiskCap = [math]::Round(($vDisk.Capacity) / 1GB)

        $vDiskFree = [math]::Round(($vDisk.FreeSpace) / 1GB)

        $row | Add-Member -Type NoteProperty -Name $DriveLetter -Value $vDisk.Path

        $row | Add-Member -Type NoteProperty -Name $DriveSize -Value $vDiskCap

        $row | Add-Member -Type NoteProperty -Name $DriveFree -Value $vDiskFree

        $DiskCount++

        $DriveTotals = "" + $row.$DriveLetter + " " + $row.$DriveSize + ";"

        $DT += $DriveTotals

        }

 


    $row | Add-Member -Type NoteProperty -Name "Host Name" -Value $VMHost.Summary.Config.Name.ToUpper()

    $row | Add-Member -Type NoteProperty -Name "# of Sessions on Host" -Value $VMHost.vm.Count

    $row | Add-Member -Type NoteProperty -Name "Host is Member of Cluster" -Value (Get-Cluster -VMHost $ESXHost).Name.ToUpper()

    $row | Add-Member -Type NoteProperty -Name "Host Vendor" -Value $VMHost.Hardware.SystemInfo.Vendor

    $row | Add-Member -Type NoteProperty -Name "Host Model" -Value $VMHost.Hardware.SystemInfo.Model

    $HostRam = [math]::Round(($VMHost.Summary.Hardware.MemorySize) / 1GB)

    $row | Add-Member -Type NoteProperty -Name "Host RAM" -Value $HostRam

    $row | Add-Member -Type NoteProperty -Name "Host CPU Model" -Value $VMHost.Summary.Hardware.CpuModel

    $row | Add-Member -Type NoteProperty -Name "Host CPU Count" -Value $VMHost.Summary.Hardware.NumCpuThreads

    $row | Add-Member -Type NoteProperty -Name "Host CPU Speed" -Value $VMHost.Summary.Hardware.CpuMhz

    $row | Add-Member -Type NoteProperty -Name "Host Product Name" -Value $VMHost.Summary.Config.Product.Name

    $row | Add-Member -Type NoteProperty -Name "Host Product Version" -Value $VMHost.Summary.Config.Product.Version

    $row | Add-Member -Type NoteProperty -Name "Host Product Build" -Value $VMHost.Summary.Config.Product.Build

    $row | Add-Member -Type NoteProperty -Name "Host Service Console" -Value $VMHost.Config.Network.ConsolevNic[0].Spec.IP.IPAddress

    $row | Add-Member -Type NoteProperty -Name "Host Service Console Subnet Mask" -Value $VMHost.Config.Network.ConsolevNic[0].Spec.IP.SubnetMask

    $row | Add-Member -Type NoteProperty -Name "Host Service Console 1" -Value $VMHost.Config.Network.ConsolevNic[1].Spec.IP.IPAddress

    $row | Add-Member -Type NoteProperty -Name "Host Service Console 1 Subnet Mask" -Value $VMHost.Config.Network.ConsolevNic[1].Spec.IP.SubnetMask

    $row | Add-Member -Type NoteProperty -Name "Host vMotion IP Address" -Value $VMHost.Config.vMotion.IPConfig.IpAddress

    $row | Add-Member -Type NoteProperty -Name "Host vMotion Subnet Mask" -Value $VMHost.Config.vMotion.IPConfig.SubnetMask

   

    $stats += $row

   

   

}

$stats | Export-Csv -Force .\$FileLocation -NoTypeInformation

Invoke-Item .\$FileLocation

 

 

Thanks for your help

 

PS. I'm a complete NOOB with Powercli, so trying to find my way around it still.

 

PPS - If anyone has an even better VM Inventory script - please be so kind as to pointing me to it....

PowerCLI 5.1 problem - Set-VM "Index was outside the bounds of the array"

$
0
0

Hello,
We have a problem with vSphere PowerCLI 5.1 Release 1 build 793510.

 

We have a user - "test_user" in our vCenter Server to which we provide administrative permissions only on resource pool - "TestResourcePool".
We have a VM - "test-vm" under this resource pool.

 

Now we need to execute some simple commands from PowerCLI under "test_user".
We successfully connecting to vCenter server by "Connect-VIServer".
We successfully receive a list of VMs of "TestResourcePool" by "Get-VM".
But if we try to reconfigure VM, for example "Set-VM test-vm -NumCPU 1", we receive error: Set-VM "Index was outside the bounds of the array" At line:1 char:7 - FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetVM.

 

While we can successfully reconfigure VM from Windows vSphere Client.

 

We found that if we provide any empty permission (create new role in vCenter and uncheck all privileges) for "test_user" on ESXi host level, then reconfiguring VM from PowerCLI completes successfully.

 

But we need that user has no permissions on ESXi host level and cannot see any information about host from Windows vSphere Client.

 

And if we execute "Set-VM test-vm -NumCPU 1" under user which have administrative permissions on whole vCenter Server, not only on certain resource pool, command completes successfully.

 

How can we resolve this issue?

Using PowerCLI with Flexclone rapidclone to Re-deploy several servers Daily

$
0
0

Hi,

 

We are Currently using vSphere4.1 with NetApp SAN and have installed Flexclone Rapidclone. We have deployed several servers successfully from a Flexclone template using rapidclone.

 

The servers in question are Windows 2008 RDS servers, which we would like to destroy and recreate on a daily basis.

 

We would like to make use of the Flexclone "Re-deploy" feature, which  will power off the servers and redploy the image to them, however we want to do this on an automated nightly schedule.

 

There is not a lot of examples on the web on how to do this and with what application.

 

I guess the obvious choice would be PowerCLI, but have no idea how to write the script.

 

Can anyone provide any information on how to go about starting this script or whether there is a recording tool that will convert mouse movements in vSphere into PowerCLI commands. I have tried to use Onyx 2.0 but for some reason it does not produce any output when cloning  using "NetApp - Provisioning & Cloning" from the context menu in vShpere.

 

Any help would be very much appreciated.

 

Regards,

 

Scots.


Automating Cisco Nexus v1000 deployment

$
0
0

We are trying to automate V1000 deployment to VMHosts through PowerCli.

So far we have not been successful

I've tried several approachs including instructions and extensions from:

 

http://labs.vmware.com/flings/vdspowercli (Get-VDS won't list the V1000)

http://www.lucd.info/2009/10/09/dvswitch-scripting-part-1-creation/ (Required modification in order to see it)

http://powerclibook.com/Forum/viewtopic.php?f=4&t=2 (Won't list it)

 

 

We're looking forward to automate adding a new host to the DVS and configure its network cards.

 

Any help will be appreciated.

Get vCenter database size

$
0
0

Hello,

 

I'm trying to find a way to get the vCenter database size by using PowerCLI.  I didn't find a command in the help.

 

Wich commande should I use?  Should I write a script?

 

Thank you.

How to get IP/MAC info of ILO board as shown in hardware status tab

$
0
0

Hi,

 

I know that there are HP scripts to collect IPC/MAC info of ILO board (hpconfg get_network.xml) and then use VMware powercli IPMI script to feed DPM.

as published on http://www.vpeeling.com/?tag=scripting

 

Add-PSSnapin vmware.VimAutomation.core -ErrorAction SilentlyContinue

Connect-VIserver -Server your.vcenter.server

$VMHosts = @(Import-Csv "C:\scripts\host-info.csv")

$IPMIUser = "dpmuser"
$IPMIPass = "dpmpass"

foreach ($VMhost in $VMHosts) {

$esxMoRef = get-vmhost $VMHost.Hostname | % {Get-View $_.Id}
$IpmiInfo = New-Object Vmware.Vim.HostIpmiInfo
$IpmiInfo.BmcIpAddress = $VMHost.iLOIP
$IpmiInfo.BmcMacAddress = $VMHost.iLOMAC
$IpmiInfo.Login = $IPMIUser
$IpmiInfo.Password = $IPMIPass
$esxMoRef.UpdateIpmi($IpmiInfo)

}

 

But, the question i got recently. How can we pull this info through vCenter? The vClient has the tab named Hardware status and we see that info.

hw_status.PNG

 

Has anyone tried this path?

Powercli or SDK (C#), its doesn't matter.

 

thanks in advance

 

A.S.

list of every triggers configurable in vCenter

$
0
0

Hi,

 

I’m looking to extract the whole alarms triggers for every alarm type (hosts, VM, etc…) for either event or condition

 

I checked the Get-AlarmActionTrigger or Get-AlarmDefinition but it lists only the alarms already defined within vCenter.

Any idea?

 

Thank you

 

Eric

Automating the deployment of VM's from a CSV

$
0
0

All, I have been banging my head against a wall to get a script working.  I keep getting this "Could not find Template with name "Templatename".    I have tried just about everything and still nothing.   Here is the comjmand:  Get-Template -Name ALPJMFMCS64_34G.  Do i need to specify a cluster to look in?  I have connected to a VM Host but this keeps coming up.  Any help would be appreciated.

Thanks,

Schneider Electric Network Shutdown Module and Powercli

$
0
0

I am trying to get my vmware environment to shut down using powercli when the shutdown module receives an alert that there is a power problem.  I was lucky to find the script that I needed here http://blog.mwpreston.net/2012/08/07/practise-makes-perfect-more-powercli-apc-powerchute-network-shutdown-goodness-now-with-power-on/ but I don't know if there is a way to get the shutdown module to use a script like this.  Is it possible to do this from a batch file instead?

 

Thanks in advance,

 

Matt

PowerCLI command that searches VMs that aren't in inventory Datastore search

$
0
0

I need a PowerCli command that will search for a specific VM on my datastores even if it's not in inventory.  Is there a way to do this as I'm trying to search for a VM that I know was backed up in DR backup however I'm not sure wich datastore it was backed up to.

Thanks in advance.


Custom firewall rule

$
0
0

Hello,

 

How can I create custom firewall rule which is not listed in default ? Port Numbers: 5000,5001

 

Thx,

How to get VM name in output along with each of its adapters mac address

$
0
0

I am trying to come up with a way to get a list of all VMs in an environemnt with their adapters mac address. I'm trying to hunt down a possible duplicate mac address in a large virtual environment

 

 

what I have so far:

 

 

Get-VM | Get-View | Select-Object -Property Name, @{N=”MAC”;E={$_.MacAddress}} | Export-Csv C:\filename.csv -NoTypeInformation

 

 

This will get me the adapter name and the mac address but not the VM name. Can someone help me with the rest? thanks.

Register VMs in a datastore in the vCenter inventory

$
0
0

Hi to all the scripting guru's out there,

 

I'm putting together a DR solution for our VM's, we are currently using the IBM Storwize V7000 with Remote Copy to replicate some Datastores.

 

Now I have done some extensive testing and found that when you perform a switch from PROD to DR there is a a bit of a process to get the VM's up and running.

 

1. I have to do a full rescan of the HBAs. Under storage adapters you can see the assigned storage devices as mounted, however you still need to go to Storage and go the the "Add Storage" wizard to readd the Datastore with the option resignaturing the volume. So this part isn't so painful except it appends a different naming convention to the datastore name, example a datastore named prd_lun100 gets named snap-5a18365a-prd_lun100 so I have to rename the datastore back to the default.

 

2. I have to go through the datastores and add every virtual machine into inventory which is very tedious, and the very part I need to automate.

 

I tried this one liner, which I found here -> http://www.wooditwork.com/2011/08/11/adding-vmx-files-to-vcenter-inventory-with-powercli-gets-even-easier/

 

New-VM -VMFilePath "[prd_lun100] SERVER01/SERVER01.vmx" -VMHost "VMHost01.local"

 

If possible I would like to expand on this, in the following ways:

 

1. Automate adding the datastores with the correct naming convention, by using either the naa identifier of the LUN or LUN ID#, possibly pull this info from a CSV list.

 

2. Add VM as per the one liner above but from a CSV list of some sort, however add is to a DRS Cluster rather ESXi Host, if possible prioritize adding VMs by some sort of groups flag in the CSV.

 

3. Power on VMs based on a priority.

 

I would really really appreciate if some one can help with this, I know probably a big ask, but scripting is not my forte and wouldn't know where to start.

Snapshot PS Script - Newbie

$
0
0

Hi, I'm just getting to grips with powershell scripting (newbie I'm afraid) and i'm trying to get my snapshot script to work, currently will only do part of the task, i.e it'll remove snapshots or create them, it wont do both, so it seems to be logic problem with my IF statement, in vb you could use goto's and subs

 

$VMserver =

Connect-VIServer -Server $VMsrv -Protocol https -Credential (Get-Credential)
$Now = Get-Date
$vmlist = Get-Content .\UAT_servers.txt
$myVMs = Get-VM $vmlist
$VMsWithSnaps = @()
foreach ($vm in $myVMs){
    $vmView = $vm | Get-View
if ($vmView.snapshot -ne $null){Write-host "$vm has snap"
            $VMsWithSnaps +=$vm
            Write-host "Showing VM'with snapshot detail"
            write-host "$VMsWithSnaps"
            Start-Sleep -s 10
            $snapshot = Get-Snapshot $VMsWithSnaps
            $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
            $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
            $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
            $caption = "Warning, this will remove snapshots"
            $message = "Do you want to proceed and remove snapshots $VMswithSnaps"
            $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
                 if($result -eq 0){
                    Foreach($vm in $VMsWithSnaps){Remove-Snapshot -snapshot $snapshot -Confirm:$True}
                    $VMsWithSnaps = ""
                    $Snapshot = ""}
                 if($result -eq 1){Write-Host "Please manually review and remove snapshots"| Disconnect-VIServer -Server * -Confirm:$False}
}Else{
if ($vmView.snapshot -eq $null){
    ForEach ($vm in $myVMs){new-snapshot -vm $vm -name "Pre-Patching" -description "Prior to Server Patching $Now"}
    }
write-host "Snapshot Routine Complete, Disconnecting from Virtual centre"
Disconnect-VIServer -Server $VMsrv -Confirm:$False
}}

 

Any ideas what I'm doing wrong?  Thanks

PowerCLI and vmotion progress

$
0
0

I have a basic script that allowes me to move a vm from one datastore to another which works great. However, I cannot get the script to show progression of the vmotion.

I am also looking to see if it will alert when the progression gets to 87% with either the powercli screen flashing or an audible alert.

 

Is this even possible? if so, what is the best way to do it?

 

My script.

get-vm -name servername | move-vm -datastore (get-datastore (New Datastore)

 

 

Thank you

Patrick

Viewing all 16717 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>