您的位置:首页 > 职场人生

用命令来操作vSphere:二十七 PowerCLI脚本—谁创建了虚拟机

2011-07-10 11:51 453 查看
有人问我,同时在客户现场工作,谁创造了一个虚拟机,这是一个在大多数环境中常见的问题,管理员权限是正常的,并创建一个VM是创建一个新的Word文档一样简单。
通过日志拖拽几分钟后,我发现的创造者,并告诉顾客,很容易,我猜想,但我们怎么可能使这更容易?
对这个问题的答案是否定的这个博客会给大多数读者惊喜的... ... PowerCLI ! PowerCLI!
一个快速的脚本和解决在AD中的用户帐户,我可以添加自定义字段,每个虚拟机,让我知道谁创造了虚拟机,并??在创建时,所有的显示如下图所示,在每个VM的注释:



Connect-VIServer MYVISERVER
# Uncomment the next line to test this script and tell you what it would do !
# $WhatIfPreference = $true
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
    Add-PSSnapin VMware.VimAutomation.Core
}
if (-not (Get-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue)) {
    Add-PSSnapin Quest.ActiveRoles.ADManagement
}
$VMs = Get-VM | Sort Name
$VM = $VMs | Select -First 1
If (-not $vm.CustomFields.ContainsKey("CreatedBy")) {
    Write-Host "Creating CreatedBy Custom field for all VM's"
    New-CustomAttribute -TargetType VirtualMachine -Name CreatedBy | Out-Null
}
If (-not $vm.CustomFields.ContainsKey("CreatedOn")) {
    Write-Host "Creating CreatedOn Custom field for all VM's"
    New-CustomAttribute -TargetType VirtualMachine -Name CreatedOn | Out-Null
}
Foreach ($VM in $VMs){
    If ($vm.CustomFields["CreatedBy"] -eq $null -or $vm.CustomFields["CreatedBy"] -eq ""){
        Write-Host "Finding creator for $vm"
        $Event = $VM | Get-VIEvent -Types Info | Where { $_.Gettype().Name -eq "VmBeingDeployedEvent" -or $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent" -or $_.Gettype().Name -eq "VmClonedEvent"}
        If (($Event | Measure-Object).Count -eq 0){
            $User = "Unknown"
            $Created = "Unknown"
        } Else {
            If ($Event.Username -eq "" -or $Event.Username -eq $null) {
                $User = "Unknown"
            } Else {
                $User = (Get-QADUser -Identity $Event.Username).DisplayName
                if ($User -eq $null -or $User -eq ""){
                    $User = $Event.Username
                }
                $Created = $Event.CreatedTime
            }
        }
        Write "Adding info to $($VM.Name)"
        Write-Host -ForegroundColor Yellow "CreatedBy $User"
        $VM | Set-CustomField -Name "CreatedBy" -Value $User | Out-Null
        Write-Host -ForegroundColor Yellow "CreatedOn $Created"
        $VM | Set-CustomField -Name "CreatedOn" -Value $Created | Out-Null
    }
}

脚本是相当直截了当,值得一提的不过几个警告:
?此脚本使用的AD cmdlet任务来解决在AD中的用户名,如果你没有安装,那么你可以安装或使用微软的AD cmdelts或我将做同样的事情,我以前用过的功能。
?如果虚拟机是在很久以前创建的,不幸的是,获取VIEvent cmdlet的没有办法从事件开始启动,所以我需要检索该VM的所有事件,然后过滤他们。
?如果虚拟机的是从虚拟中心中删除,然后重新添加它会有的人重新导入或添加虚拟机,不是原始的创建者的名称。

一旦我们的信息添加到虚拟机的,我们当然可以做一些很酷的报告,如创建虚拟机的:
Get-VM | Select Name -ExpandProperty CustomFields | Where {$_.key -eq "CreatedBy"} | Out-GridView



甚至是谁创建了大多数的虚拟机:
Get-VM | Select Name -ExpandProperty CustomFields | Where {$_.key -eq "CreatedBy"} | Group-Object | Select Count, Name | Sort Count -Descending |Out-GridView


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  职场 虚拟机 休闲