Hi,
Afaik, no. There isn’t any "host level" information leaked up to the guest by default and Bginfo can’t pull anything like that on its own, since that’s not something that VMware presents to the VM.
I have found like below. But no luck.
https://williamlam.com/2011/01/how-to-extract-host-information-from.html
Having never done this before, does anyone have any really good tips as to what the best method is for going about this?
Any links or direction is greatly appreciated!
Let's start what you already tried out.
How did you implement that?
And what didn't work?
Also, since a VM can be vMotion-ed between ESXi nodes, does that information need to be refreshed in such a case?
Can BGInfo even do that?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
.
That is not using the guestinfo option from WIlliam's post
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So , We have multiple vcenter server. (I am using same domain credentials for login) I want to do a dump of the vCenter to VM mappings to a CSV somewhere globally accessible (UNC share). How can we do that ?
Desired output:
VCenterIP,VMName
1.1.1.2,VM01
1.1.2.3,VM12
10.10.1.1,VM03
Thanks,
Provided you are connected to all the vCenters, you could do
Get-VM |
Select Name,
@{N = 'vCenter'; E = {([uri]$_.ExtensionData.Client.ServiceUrl).Host}} |
Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc. Btw, I will connect multiple vcenter like below. is it make sense ? is there any extra suggestion?
# PowerCLI_Custom_Functions.ps1
# Usage:
# 0) Edit $vCenterList to reference the vCenters in your environment.
# 1) Call 'Update-Credentials' to create/update a ViCredentialStoreItem to securely store your username and password.
# 2) Call 'Connect-vCenters' to open simultaneously connections to all the vCenters in your environment.
# 3) Do PowerCLI things.
# 4) Call 'Disconnect-vCenters' to cleanly close all ViServer connections because housekeeping.
Import-Module VMware.PowerCLI
$vCenterList = @("vcenter1", "vcenter2", "vcenter3", "vcenter4", "vcenter5")
function Update-Credentials {
$newCredential = Get-Credential
ForEach ($vCenter in $vCenterList) {
New-ViCredentialStoreItem -Host $vCenter -User $newCredential.UserName -Password $newCredential.GetNetworkCredential().password
}
}
function Connect-vCenters {
ForEach ($vCenter in $vCenterList) {
Connect-ViServer -Server $vCenter
}
}
function Disconnect-vCenters {
Disconnect-ViServer -Server * -Force -Confirm:$false
}
That will work.
The only change I would consider, first check if for a specific Center there is already a VICredentialStoreItem present.
The issue with that could be when you want to change credentials for a specific vCenter.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
When you first attempt to connect to a second vCenter server you will receive the following warning.
Working with multiple default servers? Select [Y] if you want to work with more than one default servers. In this case, every time when you connect to a different server using Connect-VIServer, the new server connection is stored in an array variable together with the previously connected servers. When you run a cmdlet and the target servers cannot be determined from the specified parameters, the cmdlet runs against all servers stored in the array variable. Select [N] if you want to work with a single default server. In this case, when you run a cmdlet and the target servers cannot be determined from the specified parameters, the cmdlet runs against the last connected server. You can change your preference at any time using the DefaultServerMode parameter of Set-PowerCLIConfiguration. [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
Like the message says, set with the Set-PowerCLIConfiguration cmdlet the DefaultServerMode to Multiple.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
ok I have added below lines. now its works.
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false
You could do that in 1 call to Set-PowerCliConfiguration.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can do in powercli config
Lastly , I want to add guest hostname (no FQDN only short name) inside below script. how can we add it?
Which script?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
sorry 🙂
Get-VM | Select Name, @{N = 'vCenter'; E = {([uri]$_.ExtensionData.Client.ServiceUrl).Host}} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Note that this data is only available when the VMware Tools are running.
Get-VM |
Select Name,
@{N='Hostname';E={$_.Guest.HostName.Split('.')[0]}},
@{N = 'vCenter'; E = {([uri]$_.ExtensionData.Client.ServiceUrl).Host}} |
Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks you very much.
1 - Is there a best practice on having an encrypted/obscured password in a ps script?
2 - I want to improve my script. it looks to me like amateur script. 🙂
Also , I have tried below clixml. but I can see my username. I don't want to do it too.
Get-Credential | Export-CliXml $Env:Appdata\DontNameMeCredential.xml
$Credential = Import-CliXml $Env:Appdata\DontNameMeCredential.xml
script:
# PowerCLI_Custom_Functions.ps1
# Usage:
# 0) Edit $vCenterList to reference the vCenters in your environment.
# 1) Call 'Update-Credentials' to create/update a ViCredentialStoreItem to securely store your username and password.
# 2) Call 'Connect-vCenters' to open simultaneously connections to all the vCenters in your environment.
# 3) Do PowerCLI things.
# 4) Call 'Disconnect-vCenters' to cleanly close all ViServer connections because housekeeping.
Import-Module VMware.PowerCLI
$vCenterList = @("vcenter01","vcenter02")
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false
function Update-Credentials {
$newCredential = Get-Credential
ForEach ($vCenter in $vCenterList) {
New-ViCredentialStoreItem -Host $vCenter -User $newCredential.UserName -Password $newCredential.GetNetworkCredential().password
}
}
function Connect-vCenters {
ForEach ($vCenter in $vCenterList) {
Connect-ViServer -Server $vCenter
}
}
function Disconnect-vCenters {
Disconnect-ViServer -Server * -Force -Confirm:$false
}
#Create a secure string for the password
$SecureStringPassword = ConvertTo-SecureString "SecretPassword" -AsPlainText -Force
#Create a PSCredential object
$VIcred = New-Object System.Management.Automation.PSCredential('domain\username', $SecureStringPassword)
Connect-vCenters
Get-VM |
Select Name,
@{N = 'vCenter'; E = {([uri]$_.ExtensionData.Client.ServiceUrl).Host}} |
Export-Csv -Path C:\temp\report.csv -NoTypeInformation -UseCulture
For credentials you might have a look at the SecretManagement module.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks So , I want to improve my script. how can we do that ? it looks to me like amateur script.