Hi all/Luc.D
We have requirement to find serial number of esxihosts .
We have been given list of vms and asked to find serial number of esxi hosts on which the vm is running.
although DRS can move vm but i tried to find this info after Changing DRS modeof virtual machines to manual.
I am thinking to do following
1:changing DRS mode of individual vm to manual.
2:finding the esxi host of that vm
3:and using esxli to get serial number of esxi host.
created following script.
$vc=Read-Host "provide the vcenter name"
$cred=Get-Credential
Connect-VIServer -Server $vc -Credential $cred
$vm_list=Get-Content -path "C:\namesvm.txt"
foreach ($vm in $vm_list)
{
$x=get-vm $vm
$drs_manual=set-vm -vm $x -drsautomationlevel manual
$esxiname=$vm.vmhost
$esxcli=get-esxcli -vmhost $esxiname
$esxcli.hardware.platform.get().SerialNumber
}
disconnect-viserver
I have following question.
How to put sleep or wait function between the time script change drsmode of vm and finding the esxiname to feed to esxcli.
or is there any other way to achieve the above goal.
Any help is much appreciated.
Try like this
$vc=@("vc1.servers.chrysler.com","vc2.servers.chrysler.com")
Connect-VIServer -Server $vc -user "administrator@vsphere.local" -password ""
$csv_info = Import-Csv c:\namesvm.csv
$report = foreach ($line in $csv_info)
{
$x=Get-VM $line.vmname
$esxcli=Get-EsxCli -VMHost $x.VMHost
$esxcli.hardware.platform.get().SerialNumber |
select @{N='VMHost';E={$x.VMHost.Name}},
@{N='Serial';E={$_}},
@{N="vmname";E={$x.Name}}
}
$report | Export-Csv .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I would take the approach of setting up a vCenter alarm to run your script (with some modifications of course) after a successful vMotion event. Probably best approach would be to do something like sending an smtp trap to orchestrator to have it run the workflow that you want.
This way it triggers only when needed and doesn't have to be schedule based.
Here are some resources:
Automatically Securing Virtual Machines Using vCenter Orchestrator - VMware vSphere Blog
thnaks for your response but currently we have not integrated vRO with vcenters.so right now looking suggestions in powercli code.
could you check the code and suggest any modification.
Would this work?
$vc=Read-Host "provide the vcenter name"
$cred=Get-Credential
Connect-VIServer -Server $vc -Credential $cred
$vm_list=Get-Content -path "C:\namesvm.txt"
foreach ($vm in $vm_list)
{
$x=Get-VM $vm
$x=Set-VM -VM $x -DrsAutomationLevel Manual
while($x.DrsAutomationLevel -ne 'Manual'){
sleep 1
$x = Get-VM -Name $vm
}
$esxcli=Get-EsxCli -VMHost $x.VMHost.Name
$esxcli.hardware.platform.get().SerialNumber
}
Disconnect-VIServer
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for your reply .This is working but for for some reasons it loops for 3 to 4 times .
Also following things i want to avoid .
1:passingvirtual machines names from notepad as it always gives space issues .is there any way to avoid or trim space.
2:also output which iam getting again not in presentable format.
could you suggest any other way of doing this to convert it into csv or excel format .
Thanks in advace.
Hi LUC.D,
iamcreating separte script for disabling drs but for the time being assume drs is set to manual.
i created following script to get serial number using importing csv file to avoid space issues in importing .txt.
$vc=@("vc1.servers.chrysler.com","vc2.servers.chrysler.com")
#$cred=Get-Credential
Connect-VIServer -Server $vc -user "administrator@vsphere.local" -password ""
$csv_info = Import-Csv c:\namesvm.csv
$csv_info.count
foreach ($line in $csv_info)
{
$x=get-vm $line.vmname
$esxiname=$x.vmhost
$n=$esxiname|select name
$n
$esxcli=get-esxcli -vmhost $esxiname
$sn=$esxcli.hardware.platform.get().SerialNumber
$sn
}
this is printing esxi name and corresponding serial number .could you modify this code so that i can create an empty array which can store info of vmname.esxiname and serial number .
Thanks.
Try like this
$vc=@("vc1.servers.chrysler.com","vc2.servers.chrysler.com")
#$cred=Get-Credential
Connect-VIServer -Server $vc -user "administrator@vsphere.local" -password ""
$csv_info = Import-Csv c:\namesvm.csv
$csv_info.count
foreach ($line in $csv_info)
{
$x=Get-VM $line.vmname
$esxcli=Get-EsxCli -VMHost $x.VMHost
$esxcli.hardware.platform.get().SerialNumber |
select @{N='VMHost';E={$x.VMHost.Name}},
@{N='Serial';E={$_}}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
that does not work .
however i modified again little as follows.
foreach ($line in $csv_info)
{
$x=get-vm $line.vmname
$x.name
$esxiname=$x.vmhost
#$esxiname
$n=$esxiname.name
$n
$esxcli=get-esxcli -vmhost $esxiname
$sn=$esxcli.hardware.platform.get().SerialNumber
$sn
}
so $x.name prints vmname
$n prints esxiname
$sn prints serial number
following is the output
i have seen expressions wherein each of these variables can be passed to an array to genrate output in the form {vmname ,esxiname ,serialnumber}
could you suggest something like this.
something like folowing in orange.
$out = @()
foreach ($line in $csv_info)
{
$x=get-vm $line.vmname
$v=$x.name
$v
$esxiname=$x.vmhost
#$esxiname
$n=$esxiname.name
$n
$esxcli=get-esxcli -vmhost $esxiname
$sn=$esxcli.hardware.platform.get().SerialNumber
$sn
$row = "|Select vmname,esxihost,serialnumber
$row.vmname = $vc
$row.esxihost = $n
$row.serialnumber = $sn
$out +=$row
}
What exactly didn't work?
Did you get any errors?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
well it worked this time .also i added another column vmname.
foreach ($line in $csv_info)
{
$x=Get-VM $line.vmname
$esxcli=Get-EsxCli -VMHost $x.VMHost
$esxcli.hardware.platform.get().SerialNumber |
select @{N='VMHost';E={$x.VMHost.Name}},
@{N='Serial';E={$_}},@{N="vmname"; e={$x}}
}
output
how to convert this to csv or excel .can export-csv be put inside for loop. what shoud be edited to convert this to csv or excel.
Thanks.
Try like this
$vc=@("vc1.servers.chrysler.com","vc2.servers.chrysler.com")
Connect-VIServer -Server $vc -user "administrator@vsphere.local" -password ""
$csv_info = Import-Csv c:\namesvm.csv
$report = foreach ($line in $csv_info)
{
$x=Get-VM $line.vmname
$esxcli=Get-EsxCli -VMHost $x.VMHost
$esxcli.hardware.platform.get().SerialNumber |
select @{N='VMHost';E={$x.VMHost.Name}},
@{N='Serial';E={$_}},
@{N="vmname";E={$x.Name}}
}
$report | Export-Csv .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thnak you very much .it worked .appreciate your help.