I have a big list of VMs and they are on different Hosts and managed by differentes vCenter.
I'm trying to create a script in order to get all Advanced Settings in order to correct them based on audit report.
I'm using the below script and it return 0 details:
# vCenter Login
$vCUser="domain\user"
$vCPass="PASSWORD"
# LIST OF vCenter
$vCenterIP = "X.X.X.X"
foreach ($IPAddress in $vCenterIP){
# Connection to vCenter
Connect-VIServer $IPAddress -User $vCUser -Password $vCPass -port 443
}
#Variables
$Date = get-date
$Datefile = ( get-date ).ToString("yyyy-MM-dd-hhmmss")
$ErrorActionPreference = "SilentlyContinue"
# Variable to change
$CreateCSV= "yes"
$GridView = "no"
$FileCSV = New-Item -type file "C:\Report\VMWARE_REPORT_$datefile.csv"
$output += ‘<H1>[2017 - Report Monthly Virtual Machine</H1>’
$output += ‘<H2>Date and time</H2>’,$date
Write-Host "Gathering VM statistics"
$report = @()
foreach($vm in Get-View -ViewType Virtualmachine){
Get-VM $server| Get-AdvancedSetting | Select Name, Value
}
#Output
if ($GridView -eq "yes") {
$report | Out-GridView }
if ($CreateCSV -eq "yes") {
$report | Export-Csv $FileCSV -NoTypeInformation }
#file CSV
$filename = "C:\Report\VMWARE_REPORT_$datefile.csv"
#Disconnect session from VC
Disconnect-VIserver -Confirm:$false
Try like this
$vCUser="domain\user"
$vCPass="PASSWORD"
# LIST OF vCenter
$vCenterIP = "X.X.X.X"
foreach ($IPAddress in $vCenterIP){
# Connection to vCenter
Connect-VIServer $IPAddress -User $vCUser -Password $vCPass -port 443
}
#Variables
$Date = Get-Date
$Datefile = ( Get-Date ).ToString("yyyy-MM-dd-hhmmss")
$ErrorActionPreference = "SilentlyContinue"
# Variable to change
$CreateCSV= "yes"
$GridView = "no"
$FileCSV = New-Item -Type File -Path "C:\Report\VMWARE_REPORT_$datefile.csv"
Write-Host "Gathering VM settings"
$report = Get-VM -PipelineVariable vm | Get-AdvancedSetting |
Select @{N='VM';E={$vm.Name}},Name, Value
#Output
if ($GridView -eq "yes") {
$report | Out-GridView
}
if ($CreateCSV -eq "yes") {
$report | Export-Csv -LiteralPath $FileCSV -NoTypeInformation -UseCulture
}
#Disconnect session from VC
Disconnect-VIserver -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this
$vCUser="domain\user"
$vCPass="PASSWORD"
# LIST OF vCenter
$vCenterIP = "X.X.X.X"
foreach ($IPAddress in $vCenterIP){
# Connection to vCenter
Connect-VIServer $IPAddress -User $vCUser -Password $vCPass -port 443
}
#Variables
$Date = Get-Date
$Datefile = ( Get-Date ).ToString("yyyy-MM-dd-hhmmss")
$ErrorActionPreference = "SilentlyContinue"
# Variable to change
$CreateCSV= "yes"
$GridView = "no"
$FileCSV = New-Item -Type File -Path "C:\Report\VMWARE_REPORT_$datefile.csv"
Write-Host "Gathering VM settings"
$report = Get-VM -PipelineVariable vm | Get-AdvancedSetting |
Select @{N='VM';E={$vm.Name}},Name, Value
#Output
if ($GridView -eq "yes") {
$report | Out-GridView
}
if ($CreateCSV -eq "yes") {
$report | Export-Csv -LiteralPath $FileCSV -NoTypeInformation -UseCulture
}
#Disconnect session from VC
Disconnect-VIserver -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you it's working
just the CSV obtained when I open it in Excel the first line containe : Column1, Column2, Column3 and the second line contain VM, Name, Value
is there any way to delete the first line?
Also I would like to have the vCenter IP or name on the report in order to be able to trace everything
Are you sure you copied my script correctly?
I see the expected column names in the CSV.
This adds the vCenter to each row
$vCUser="domain\user"
$vCPass="PASSWORD"
# LIST OF vCenter
$vCenterIP = "X.X.X.X"
foreach ($IPAddress in $vCenterIP){
# Connection to vCenter
Connect-VIServer $IPAddress -User $vCUser -Password $vCPass -port 443
}
#Variables
$Date = Get-Date
$Datefile = ( Get-Date ).ToString("yyyy-MM-dd-hhmmss")
$ErrorActionPreference = "SilentlyContinue"
# Variable to change
$CreateCSV= "yes"
$GridView = "no"
$FileCSV = New-Item -Type File -Path "C:\Report\VMWARE_REPORT_$datefile.csv"
Write-Host "Gathering VM settings"
$report = Get-VM -PipelineVariable vm | Get-AdvancedSetting |
Select @{N='VM';E={$vm.Name}},@{N='vCenter';E={([uri]$vm.ExtensionData.Client.ServiceUrl).Host}},Name, Value
#Output
if ($GridView -eq "yes") {
$report | Out-GridView
}
if ($CreateCSV -eq "yes") {
$report | Export-Csv -LiteralPath $FileCSV -UseCulture
}
#Disconnect session from VC
Disconnect-VIserver -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That is the NoTypeInformation switch on the Export-Csv cmdlet.
Change that line to
$report | Export-Csv -LiteralPath $FileCSV -UseCulture -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD, it's much better now but the first line still show Column 1...
I'm checking the reason why it show this details
Can you Attach (bottom right) the script as you are running it?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
#Initialize PowerCLI
#add-pssnapin VMware.VimAutomation.Core
# vCenter Login
$vCUser="domaine\user"
$vCPass="password"
# LIST OF vCenter
$vCenterIP = "X.X.X.X"
foreach ($IPAddress in $vCenterIP){
# Connection to vCenter
Connect-VIServer $IPAddress -User $vCUser -Password $vCPass -port 443
}
#Variables
$Date = Get-Date
$Datefile = ( Get-Date ).ToString("yyyy-MM-dd-hhmmss")
$ErrorActionPreference = "SilentlyContinue"
# Variable to change
$CreateCSV= "yes"
$GridView = "no"
$FileCSV = New-Item -Type File -Path "C:\Users\S852547ADM\Desktop\PowerShell_Script\Report\AdvancedSettings_VMs_$datefile.csv"
Write-Host "Gathering VM settings"
$report = Get-VM -PipelineVariable vm | Get-AdvancedSetting |
Select @{N='VM';E={$vm.Name}},@{N='vCenter';E={([uri]$vm.ExtensionData.Client.ServiceUrl).Host}},Name, Value
#Output
if ($GridView -eq "yes") {
$report | Out-GridView
}
if ($CreateCSV -eq "yes") {
$report | Export-Csv -LiteralPath $FileCSV -UseCulture -NoTypeInformation
}
#Disconnect session from VC
Disconnect-VIserver -Confirm:$false
Sorry, but I can't reproduce what you are seeing.
With your script, everything looks ok for me.
Are you sure you are looking at the correct file?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Interesting, I guess the issue is with Excel file when I import it
Try opening the file with Notepad or Notepad++
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It's Ok when I open the CSV using Notepad++ or if I open it directly in excel but if I import it it add the line
it's fine and thank you for your help
stay safe