Scripts for configure VM

How to batch extend VM disk

Add-PSSnapin vmWARE.VimAutomation.Core

$vc="vCenterHostName"

connect-viserver $vc

$VMinfo = get-content "c:\VMName.csv"

foreach ($VM in $VMinfo){

Get-HardDisk -vm $VM | where {$_.Name -eq "Hard Disk 1"} |Set-HardDisk -CapacityGB 60 -Confirm:$false

} 

根据实际情况更改VC名字和Hard disk number and size.

How to batch create VM disks

Add-PSSnapin vmware.vimautomation.core
$vc="vcname"
Connect-viserver $vc
$vminfo=get-content "C:\vmname.csv"
foreach ($vm in $vminfo){
New-HardDisk -vm $vm -CapacityGB 2 -StorageFormat:EagerZeroedThick -Confirm:$false
New-HardDisk -vm $vm -CapacityGB 1 -Confirm:$false
}
Disconnect-VIServer -Confirm:$false 

Finding VMs with Duplicate MAC Addresses

At one of my customers‘ sites today, I saw an error message that I‘ve not seen before: VM MAC Conflict. "Well, that‘s certainly not good," I thought, as I poked around at the error message. To my chagrin, I could only find that error message for a single VM in the environment, and that error message wouldn‘t tell me with which other VM it was conflicting. So, I could only think of one way to figure out what was going on with this conflict: look at the MAC Address assigned to every NIC on every VM in the environment, and figure out what was causing the conflict. Easy!

No, really, it was easy. Had I done it by hand, I would certainly have driven myself crazy, but PowerCLI made it nice and easy. I just used this command:

(get-vm | get-networkadapter | ? {$_.MacAddress -eq "<offending MAC Address>").parent

Lo-and-behold, it returned 2 VMs. One was the known VM that had flagged the error and the other was a powered-off VM. Maybe that‘s why it wasn‘t also flagging the error, but regardless, we easily identified the source of the problem and were able to resolve it.

P.S. if you ever need to find all duplicate MAC addresses in an environment, you can use these commands:

$allNICs = get-vm | get-networkAdapter
 $dupeMACs = (compare-object $allNICs.macAddress ($allNICs.macAddress | select -unique)).inputObject

 foreach ($thisMAC in $dupeMACs){
    "="*17 + "`n$thisMAC`n" + "="*17
    ($allNICs | ? {$_.macAddress -eq $thisMAC}).parent.name
 }

Report VM with 3 days older VM Snapshots

#Script Name : VMSnapShot3DaysOld.ps1
#Purpose : Get the report of VMS Snapshot 3 Days Old and save it in CSV file

If(!(Get-PSSnapin | Where {$_.Name -Eq "VMware.VimAutomation.Core"}))
{
Add-PSSnapin VMware.VimAutomation.Core
}
$VCServer = Read-Host ‘Enter VC Server name‘
$vcUSERNAME = Read-Host ‘Enter user name‘
$vcPassword = Read-Host ‘Enter password‘ -AsSecureString
$vccredential = New-Object System.Management.Automation.PSCredential ($vcusername, $vcPassword)

$LogFile = "VMSnapShot3DaysOld_" + (Get-Date -UFormat "%d-%b-%Y-%H-%M") + ".csv"

Write-Host "Connecting to $VCServer..." -Foregroundcolor "Yellow" -NoNewLine
$connection = Connect-VIServer -Server $VCServer -Cred $vccredential -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null
$Result = @()

If($? -Eq $True)

{
Write-Host "Connected" -Foregroundcolor "Green"
$Results = @()
$datetime = Get-Date

$threeDaysAgo = (Get-Date).AddDays(-3)
Get-VM | Foreach-Object {
Get-Snapshot -VM $ | Foreach-Object {
If($.Created -lt $threeDaysAgo) {
$Result += $ | Select VM,@{Name="Snapshot Name";E={$.Name}},@{Name="Date Created";E={$_.Created}}
}
}
}

$Result | Export-Csv -NoTypeInformation $LogFile
}
Else
{
Write-Host "Error in Connecting to $VIServer; Try Again with correct user name & password!" -Foregroundcolor "Red"
}

Disconnect-VIServer * -Confirm:$false

http://www.vmwarearena.com/powercli-script-report-virtual-machines-3-days-older-vm-snapshots/

Report Powered off VM’s older than 30 days

#Script Name:   VMPoweredOff30DaysAgo.ps1
#Purpose    :   Get the report of VMS Powered Off 30 Days ago               

If(!(Get-PSSnapin | Where {$_.Name -Eq "VMware.VimAutomation.Core"}))
{
    Add-PSSnapin VMware.VimAutomation.Core
}
$VCServer = Read-Host ‘Enter VC Server name‘
$vcUSERNAME = Read-Host ‘Enter user name‘
$vcPassword = Read-Host ‘Enter password‘ -AsSecureString
$vccredential = New-Object System.Management.Automation.PSCredential ($vcusername, $vcPassword)

$LogFile = "VMPoweredOff_" + (Get-Date -UFormat "%d-%b-%Y-%H-%M") + ".csv" 

Write-Host "Connecting to $VCServer..." -Foregroundcolor "Yellow" -NoNewLine
$connection = Connect-VIServer -Server $VCServer -Cred $vccredential -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null
$Global:Report = @()

If($? -Eq $True)

{
    Write-Host "Connected" -Foregroundcolor "Green" 

    $PoweredOffAge = (Get-Date).AddDays(-30)
    $Output = @{}
    $PoweredOffvms = Get-VM | where {$_.PowerState -eq "PoweredOff"}
    $EventsLog = Get-VIEvent -Entity $PoweredOffvms -Finish $PoweredOffAge  -MaxSamples ([int]::MaxValue) | where{$_.FullFormattedMessage -like "*is powered off"}
    If($EventsLog)
    {
        $EventsLog | %{ if($Output[$_.Vm.Name] -lt $_.CreatedTime)
            {
                $Output[$_.Vm.Name] = $_.CreatedTime
            }
        }
    }
    $Result = $Output.getEnumerator() | select @{N="VM";E={$_.Key}},@{N="Powered Off Date";E={$_.Value}}

    If($Result)
    {
        $Result | Export-Csv -NoTypeInformation $LogFile
    }
    Else
    {
        "NO VM‘s Powered off last 30 Days"
    }
}
Else
{
    Write-Host "Error in Connecting to $VIServer; Try Again with correct user name & password!" -Foregroundcolor "Red"
}

Disconnect-VIServer * -Confirm:$false

http://www.vmwarearena.com/powercli-script-report-powered-off-vms-older-30-days/

原文地址:http://blog.51cto.com/549687/2114696

时间: 2024-09-30 10:10:55

Scripts for configure VM的相关文章

Scripts for configure portgroup

Export portgroup from the host Add-PSSnapin vmWARE.VimAutomation.Core $vc="Vcname" connect-viserver $vc $date = Get-Date -Format 'yyyyMMdd' $VLANinfo = foreach ($cluster in get-cluster) { foreach ($esx in (Get-VMHost -Location $cluster)) { forea

Configure,Makefile.am, Makefile.in, Makefile

无论对于一个初学者还是一个资深的Linux程序员,编写Makefile文件都是一件很麻烦的事:再者,开发人员应该把主要的精力放在程序代码的编写上,而在Makefile文件花费太多的精力显然是不明智的:还有,对于不同的处理器架构,往往编译器不同,环境不同,特别是一些嵌入式系统中的各种程序的编译,于是移植问题也使Makefile文件编写趋于复杂,也显得这个问题很重要.对于这些问题Linux的高手们早已想到了,所以他们开发了一些能够自动生成Makefile文件的工具.他们就是下面这些工具: 〉GNU

Configure,make,make install详解

转:http://my.oschina.net/qihh/blog/66113?fromerr=6ej3CfGJ 无论对于一个初学者还是一个资深的Linux程序员,编写Makefile文件都是一件很麻烦的事:再者,开发人员应该把主要的精力放在程序代码的编写上,而在Makefile文件花费太多的精力显然是不明智的:还有,对于不同的处理器架构,往往编译器不同,环境不同,特别是一些嵌入式系统中的各种程序的编译,于是移植问题也使Makefile文件编写趋于复杂,也显得这个问题很重要.对于这些问题Linu

Configure,Makefile.am, Makefile.in, Makefile文件

一 软件安装关于 makefile文件问题 如果拿到的工程文件中,没有Makefile文件,而只有configure.in和Makefile.am文件,我们是不能够直接进行编译的,必须根据configure.in和Makefile.am文件生成编译所需的Makefile文件.具体操作步骤如下: 1.执行aclocal,产生aclocal.m4文件 aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configur

QEMU KVM libvirt 手册(1)

安装 对虚拟化的支持通常在BIOS中是禁掉的,必须开启才可以. 对于Intel CPU,我们可以通过下面的命令查看是否支持虚拟化. # grep "vmx" /proc/cpuinfo flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdp

AngularJS -- Module (模块)

什么是AngularJS的模块 我们所说的模块,是你的AngularJS应用程序的一个组成部分,它可以是一个Controller,也可以是一个Service服务,也可以是一个过滤器(Filter),也可以是一个directive(指令)等等…都是属于一个模块! 大多数的应用程序都是有一个自己的函数入口方法Main ,用它来进行初始化,以及加载装配各个模块,然后这些模块的组合,构成了你的应用程序,对吧? 但是,but, AngularJS应用程序却不是这样的哦,它没有main 方法,没有函数入口.

oracle DBA反映ODA中的虚拟机无法分配内存

调查最终原因是由于oracle数据库本身参数设置的问题. 1. 怎么给虚拟机调整内存和cpu:oakcli configure vm   uat-ihub-oracle-db    -prefnode 1     -maxvcpu 8 –vcpu 8   -maxmemory 16G  -memory 16G 2. 添加磁盘:       oakcli create vdisk    uatdb_vdisk -repo sharedrepo -size 50G -type shared     

ovirt-engine

wget http://resources.ovirt.org/pub/yum-repo/ovirt-release36.rpm ovirt-3.6-dependencies.repo [ovirt-3.6-epel] name=Extra Packages for Enterprise Linux 7 - $basearch #baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch mirrorlist=https://mi

云计算概述与KVM

一.云计算概述 云计算主要是一种按需付费的网络模式,这种模式提供可用的.便捷的.按需的网络访问,进入可配置的计算资源共享池(资源包括网络,服务器,存储,应用软件,服务),这些资源能够被快速提供,只需投入很少的管理工作,或与服务供应商进行很少的交互.分为三大层:IaaS(基础设施即服务,面向运维人员).PaaS(平台即服务,面向开发人员),SaaS(软件即服务,面向终端用户). 虚拟化分类:服务器虚拟化.桌面虚拟化.应用虚拟化,私有云 1.1 云计算的特点和优势 1)云计算是一种使用模式 2)云计