Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Powershell script for VMware

Status
Not open for further replies.

seacros

Technical User
Jan 23, 2008
31
US
I am looking for a script that I could run to scan my Virtual Center server (database) that would generate the following information:
Host and IP
Guest and IP
Which guest are on which Host

Is this possible?

Thanks
 
this may point you in a direction

Try
{
Connect-VIServer $Global:strVIServer
#
$vmhosts = get-vmhost | Select-Object Name, Id, CustomFields, MoRef, Summary
If ($vmhosts -eq $null)
{
}
Else
{
ForEach($vmhost In $vmhosts){
#clear variables, set up the fake class, construct
$clsVMHost = "" | Select VMWareUUID, VMWareName, VMWareNetbiosname, VMWareSCCMID, VMWareMAC, SMSSCCMID
$clsVMHost.VMWareUUID = [string]""
$clsVMHost.VMWareName = [string]""
$clsVMHost.VMWareNetbiosname = [string]""
$clsVMHost.VMWareSCCMID = [string]""
$clsVMHost.VMWareMAC = [string]""
$clsVMHost.SMSSCCMID = [string]""
#
#get-view of vmhost
$vmhostview = get-view $vmhost.Id
$clsVMHost.VMWareUUID = $vmhostview.Summary.Hardware.Uuid
$clsVMHost.VMWareName = $vmhostview.Name
#populate netbiosname variable
If ($clsVMHost.VMWareName.Contains(".")){
$clsVMHost.VMWareNetbiosname = $clsVMHost.VMWareName.SubString(0, $clsVMHost.VMWareName.IndexOf(".")).ToUpper()
}
Else{
$clsVMHost.VMWareNetbiosname = $clsVMHost.VMWareName.ToUpper()
}#End If
#get the SCCM-ID custom field
$clsVMHost.VMWareSCCMID = $vmhost.CustomFields.get_item("SCCM-ID").ToString().ToUpper()
#NetworkAdapter, get the MAC of the first NIC
$colNetworkAdapters = ""
$colNetworkAdapters = Get-VMHostNetworkAdapter -VMHost $vmhostview.Name -Physical
ForEach($objNetworkAdapter In $colNetworkAdapters)
{
$clsVMHost.VMWareMAC = $objNetworkAdapter.Mac.ToUpper()
Break
}#Next
 
Thanks for the input!!

Would I need to format this script any certain way? I assume this is a powershell script.

SDC
 
I copied and pasted this and saved it *.ps1. I get an error when I try to run it. Am i missing something?

{
Connect-VIServer $Global:strVIServer
#
$vmhosts = get-vmhost | Select-Object Name, Id, CustomFields, MoRef, Summary
If ($vmhosts -eq $null)
{
}
Else
{
ForEach($vmhost In $vmhosts){
#clear variables, set up the fake class, construct
$clsVMHost = "" | Select VMWareUUID, VMWareName, VMWareNetbiosname, VMWareSCCMID, VMWareMAC, SMSSCCMID
$clsVMHost.VMWareUUID = [string]""
$clsVMHost.VMWareName = [string]""
$clsVMHost.VMWareNetbiosname = [string]""
$clsVMHost.VMWareSCCMID = [string]""
$clsVMHost.VMWareMAC = [string]""
$clsVMHost.SMSSCCMID = [string]""
#
#get-view of vmhost
$vmhostview = get-view $vmhost.Id
$clsVMHost.VMWareUUID = $vmhostview.Summary.Hardware.Uuid
$clsVMHost.VMWareName = $vmhostview.Name
#populate netbiosname variable
If ($clsVMHost.VMWareName.Contains(".")){
$clsVMHost.VMWareNetbiosname = $clsVMHost.VMWareName.SubString(0, $clsVMHost.VMWareName.IndexOf(".")).ToUpper()
}
Else{
$clsVMHost.VMWareNetbiosname = $clsVMHost.VMWareName.ToUpper()
}#End If
#get the SCCM-ID custom field
$clsVMHost.VMWareSCCMID = $vmhost.CustomFields.get_item("SCCM-ID").ToString().ToUpper()
#NetworkAdapter, get the MAC of the first NIC
$colNetworkAdapters = ""
$colNetworkAdapters = Get-VMHostNetworkAdapter -VMHost $vmhostview.Name -Physical
ForEach($objNetworkAdapter In $colNetworkAdapters)
{
$clsVMHost.VMWareMAC = $objNetworkAdapter.Mac.ToUpper()
Break
}#Next
 
sorry, yes you are missing a number of closing }
this portion was taken from a larger script responsible for creating SCCM computer instances for each ESX host and producing hw inventory for them through idmifs

you will need to define $strVIServer
you will want to put in write-host etc for debugging info later
 
I am such a newbie at this. I am not sure where to put the missing } it keeps asking me for.

SDC
 
Take out the leading open brace, the very first line, which is not close - that's the compile time error meant.
 
i think you need a closing one at the end to close out the Else from about line 7?
 
Here is what I now get:
[vSphere PowerCLI] C:\> .\scripts\tryThis.ps1
Missing closing '}' in statement block.
At C:\scripts\tryThis.ps1:40 char:7
+ <<<<
+ CategoryInfo : ParserError: (CloseBraceToken:TokenId) [], ParseException
+ FullyQualifiedErrorId : MissingEndCurlyBrace
 
sorry, i didnt post the code as a working example, it was more a change of you to get a look at the get-vmhost, the need to use the get-view $vmhost.Id and the Get-VMHostNetworkAdapter -VMHost $vmhostview.Name -Physical
 
no problem. Now i am on the right track. Thanks for pointing me in the right direction.

SDC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top