So I have a little background in programming and I have become extremely interested in PowerShell CLI. I was given the task to find and enter certain fields that the Server Team manager wants to have so we can create a CI when we switch software. Initially it was supposed to be done manually but I have found a script that can do it but I need to add some other information that my supervisor is looking for. Where is the best place to learn the syntax and structure for the language? Below is the code and I would like the script to find the
VM Host
Switchport
Data Stores
Data Center
Notes for the Equipment
Application Name
Application Info
App Owner
If it is for testing, development or production
System POC
Application Client PC
Domain
This is the script I have found. Just need to be pointed in the right direction to add the other fields to be filled. Thank you so much.
#Specify the output CSV file bellow
$ExportPath = "Output CSV file FullPath"
#Temporary variable to run this script faster
$VmInfo = Get-VM
$VMS = ($VmInfo).Name
#Getting the required value
$VCenter = @()
foreach ($VM in $VMS)
{
$HostServer = (($VmInfo | ? {$_.Name -eq $VM}).Host).Name
$VMSysInfo = Get-VMGuest -VM $VM
$MyObject = New-Object PSObject -Property @{
VMName = $VM
VMHostName = $VMSysInfo.HostName
VMIP = $VMSysInfo.IPAddress
VMInstalledOS = $VMSysInfo.OSFullName
PowerState = ($VmInfo | ? {$_.Name -eq $VM}).PowerState
NumberOfCPU = ($VmInfo | ? {$_.Name -eq $VM}).NumCpu
MemoryGB = (($VmInfo | ? {$_.Name -eq $VM}).MemoryMB/1024)
VMDataS = (Get-Datastore -VM $VM).Name
HostServer = (($VmInfo | ? {$_.Name -eq $VM}).Host).Name
HostCluster = (Get-Cluster -VMHost $HostServer).Name
}
$VCenter += $MyObject
}
$VCenter |
Select VMName,
VMHostName,
@{N='VMIPAddress';E={$_.VMIP -join '; '}},
VMInstalledOS,
PowerState,
NumberOfCPU,
MemoryGB,
@{N='VMDataStore';E={$_.VMDataS -join '; '}},
HostServer,
HostCluster |
Export-Csv $ExportPath -NoTypeInformation
Where is the information, more specifically the Application related info, available ?
Is that available as Custom Fields on the VM ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Some of it is custom information like in the notes. If I am not able to get all fields I would like to get as many as I can. The information is available in the summary of each server when it is selected.
This adds some of the properties.
Check if they are ok, and what is missing.
#Specify the output CSV file bellow
$ExportPath = "Output CSV file FullPath"
#Temporary variable to run this script faster
$VmInfo = Get-VM
$VMS = ($VmInfo).Name
#Getting the required value
$VCenter = @()
foreach ($VM in $VMS)
{
$HostServer = (($VmInfo | ? {$_.Name -eq $VM}).Host).Name
$VMSysInfo = Get-VMGuest -VM $VM
$MyObject = New-Object PSObject -Property @{
VMName = $VM
VMHostName = $VMSysInfo.HostName
VMIP = $VMSysInfo.IPAddress
VMInstalledOS = $VMSysInfo.OSFullName
PowerState = ($VmInfo | ? {$_.Name -eq $VM}).PowerState
NumberOfCPU = ($VmInfo | ? {$_.Name -eq $VM}).NumCpu
MemoryGB = (($VmInfo | ? {$_.Name -eq $VM}).MemoryMB/1024)
VMDataS = (Get-Datastore -VM $VM).Name
HostServer = (($VmInfo | ? {$_.Name -eq $VM}).Host).Name
HostCluster = (Get-Cluster -VMHost $HostServer).Name
Datacenter = (Get-Datacenter -VM $vm).Name
Notes = $vm | Select -ExpandProperty Description
Portgroup = (Get-VirtualPortGroup -VM $vm).Name
}
$VCenter += $MyObject
}
$VCenter |
Select VMName,
VMHostName,
@{N='VMIPAddress';E={$_.VMIP -join '; '}},
VMInstalledOS,
PowerState,
NumberOfCPU,
MemoryGB,
@{N='VMDataStore';E={$_.VMDataS -join '; '}},
HostServer,
HostCluster,
Datacenter,
Notes,
Portgroup |
Export-Csv $ExportPath -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much for the help. I want to understand this so over the weekend I am going to go through a little boot camp on my own for power shell. The syntax and little knowledge for VMWare and how to navigate it is still elementary.
Would you be able to point me in the right direction to learn PowerCLI and how to use it for vSphere?
I would suggest to start with diving into PowerShell.
There is a good free ebook here.
PowerCLI is in fact a PSSnapin/module on top of PowerShell.
There are several learning sources available, books, courses... (let Google or Bing do the work).
But the most important part for learning, is actually dive in, set yourself a tast (start simple), try to find a solution, look up how other have solved a similar issue.
And if you're still stuck, post the question in this community.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference