L'objectif de ce script est de lister le Template utilisé lors de la création d'une VM
<#
. AUTEUR : Antoine JOVELIN
. FONCTION : Indiquer la date de création et le createur d'une VM
. UTILISATION :
- Lancer le script
#>
cls
# Declaration des fonctions
function vcenter-connect{
$VC = Read-Host -Prompt " Entrer le nom de vCenter "
$cred = Get-Credential
Write-Host;
Write-Host -ForegroundColor cyan "Connexion à vCenter. Merci de patienter..."
Connect-VIServer $VC -Credential $cred
Write-Host;
}
function vcenter-disconnect{
Write-Host;
Write-Host -ForegroundColor cyan "Déconnexion de vCenter..."
Disconnect-viserver -Server $Global:DefaultVIServer -Confirm:$false -Force
Write-Host;
}
function Get-VMEvents {
param(
[Parameter(ValueFromPipeline = $true)]
[ValidatenotNullOrEmpty()]
$VM,
[String[]]$types,
[string]$category,
[switch]$All
)
$si = get-view ServiceInstance
$em = get-view $si.Content.EventManager
$EventFilterSpec = New-Object VMware.Vim.EventFilterSpec
$EventFilterSpec.Type = $types
if ($category) {
$EventFilterSpec.Category = $category
}
if ($VM) {
$EventFilterSpec.Entity = New-Object VMware.Vim.EventFilterSpecByEntity
switch ($VM) {
{ $_ -is [VMware.Vim.VirtualMachine] } { $VMmoref = $vm.moref }
{ $_ -is [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl] } { $VMmoref = $vm.Extensiondata.moref }
default { $vmmoref = (get-view -ViewType virtualmachine -Filter @{'name' = $VM }).moref }
}
$EventFilterSpec.Entity.Entity = $vmmoref
$em.QueryEvents($EventFilterSpec)
}
if ($All) {
$em.QueryEvents($EventFilterSpec)
}
}
function get-vmcreationdate {
param(
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Object[]]$VMnames
)
process {
foreach ($vm in $VMnames) {
$ReportedVM = "" | Select VMname, CreatedTime, CreatedMonth, CreationMethod, Creator
if ($CollectedEvent = $vm | Get-VMEvents -types 'VmBeingDeployedEvent', 'VmRegisteredEvent', 'VmClonedEvent', 'VmBeingCreatedEvent' -ErrorAction SilentlyContinue) {
if ($CollectedEvent.gettype().isArray) { $CollectedEvent = $CollectedEvent | ? { $_ -is [vmware.vim.VmRegisteredEvent] } }
$CollectedEventType = $CollectedEvent.gettype().name
$CollectedEventMonth = "{0:MMMM}" -f $CollectedEvent.CreatedTime
$CollectedEventCreationDate = $CollectedEvent.CreatedTime
$CollectedEventCreator = $CollectedEvent.Username
switch ($CollectedEventType) {
'VmClonedEvent' { $CreationMethod = 'Cloned' }
'VmRegisteredEvent' { $CreationMethod = 'RegisteredFromVMX' }
'VmBeingDeployedEvent' { $CreationMethod = 'VmFromTemplate' }
'VmBeingCreatedEvent' { $CreationMethod = 'NewVM' }
default { $CreationMethod = 'Error' }
}
$ReportedVM.VMname = $CollectedEvent.vm.Name
$ReportedVM.CreatedTime = $CollectedEventCreationDate
$ReportedVM.CreatedMonth = $CollectedEventMonth
$ReportedVM.CreationMethod = $CreationMethod
$ReportedVM.Creator = $CollectedEventCreator
}
else {
if ($?) {
if ($vm -is [VMware.Vim.VirtualMachine]) { $ReportedVM.VMname = $vm.name } else { $ReportedVM.VMname = $vm.ToString() }
$ReportedVM.CreatedTime = 'NoEvent'
$ReportedVM.CreatedMonth = 'NoEvent'
$ReportedVM.CreationMethod = 'NoEvent'
$ReportedVM.Creator = 'NoEvent'
}
else {
$ReportedVM = $null
Write-Warning "$VM could not be found, typo?"
}
}
$ReportedVM
}
}
}
# Debut du script
vcenter-connect
Write-Host;
Write-Host -fore cyan "
########################################################
# Indiquer la date de création et le créateur d'une VM #
########################################################"
Write-Host;
$reponse = Read-Host -Prompt " Entrer le nom d'une VM "
get-vmcreationdate -VMnames $reponse
Write-Host;
vcenter-disconnect