Hi Experts
I have a script which runs error free, I can see the information on the screen which is very clear and readable.
Now I need to output the information to a CSV file
How can i do that
##########################################################################################################################
##################################################################################
#Function Draw Line
##################################################################################
function Drawline {
for($i=0; $i -lt (get-host).ui.rawui.buffersize.width; $i++) {write-host -nonewline -foregroundcolor cyan "-"}
}
function DrawSpace {
for($i=0; $i -lt (get-host).ui.rawui.buffersize.width; $i++) {write-host -nonewline -foregroundcolor cyan " "}
}
###################################################################################
####################################################################################
# Clear existing VC connections
####################################################################################
Clear-Host
try {
Disconnect-VIServer -Server $global:DefaultVIServers -Confirm:$false -Force -ErrorAction Stop
Write-Warning -Message " All Virtual Center connections are Disconnected "
}
Catch {
Write-host "Administrator Message : There are no existing Virtual centre connection we are good to proceed " -ForegroundColor Green
}
########################################################################################
########################################################################################
# Connect to source and destination Virtual centers
########################################################################################
$VCnames = Import-Csv -Path '\\XXX\project$\XXXX\ps1\vcenters.csv'
#Use this option to enter credentials
#$cred = get-credential -Message "Enter your AdminUser Credentials to AccessVirtual Center"
Foreach ($VC in $VCnames )
{
If (Connect-VIServer -Server $VC.VCenters -Credential ( Import-Clixml -Path D:\Temp\XXX-T\ZZZ.xml) -WarningAction Ignore )
{
write-Host -foregroundcolor Yellow " You are connected Connected to $vc"
}
Else
{
write-Host -foregroundcolor Cyan " Failed to connect to Source and Destination vCenters..." $VCnames.Vcenters
}
}
Clear-Host
#############################################################################################
#############################################################################################
#
# *********************KICK OFF THE VMOTION CHECKS**********************************
#
#
#############################################################################################
# Import VMs from a CSV file and read data which are not commented
$VMdetails = Get-Content -Path '\\XXXX\XXXX$\XXXX\XXX\NSX-T_Migration_Scripts\Migration.CSV' |Select-String '^[^#]' | ConvertFrom-Csv -UseCulture
Foreach ($VMdetail in $VMdetails) {
###############################################################################################
# Check VM Availability
###############################################################################################
$VM = Get-VM -Name $VMdetail.VMNAME -ErrorAction SilentlyContinue
If ($VM) {
Write-Host "VM is a alive on the source VC..." -ForegroundColor White -BackgroundColor black $VM
DrawSpace
}
Else {
Write-Host 'The VM Name ==>' $VMdetail.VMNAME' Cannot be found in the Virtual Centers' -ForegroundColor Red -BackgroundColor white
Drawline
DrawSpace
continue
}
###############################################################################################
# Check Destination ESXi Host Availability
# If found proceed or skip to next record
###############################################################################################
$destination = Get-cluster $vmdetail.TgCluster -ErrorAction SilentlyContinue |Get-VMHost |Select-Object -First 1 #-Name vhst6012.rabobank.corp #
if ($destination){
Write-Host "VM will be placed on Cluster" $vmdetail.TgCluster "and ESXi host..." -ForegroundColor Green $destination
}
Else {
Write-host "Destination ESXi host" $vmdetail.TgCluster "is not Accessible" -ForegroundColor Red -BackgroundColor white
#continue
}
####################################################################################
# Check if the Network Adapter for the VM is found
# If found proceed or skip to next record
####################################################################################
$networkAdapterStatus = Get-NetworkAdapter -vm $VM |Select-Object name,Type,Networkname,ConnectionState -ErrorAction SilentlyContinue
If ($networkAdapterStatus){
Write-Host "VM Network Adaptor info..." -ForegroundColor Green $networkAdapterStatus
}
Else {
Write-Host "Network Adpater cannot be attahced and migration will fail" -ForegroundColor Red -BackgroundColor white
#continue
}
####################################################################################
# Check if the destination VDS switch is availabl
####################################################################################
#$DesvDSsWitch = Get-VDSwitch -Name $VMdetail.TgSwitch -ErrorAction SilentlyContinue |Get-VDPortgroup -Name $VMdetail.TgPortGroup -ErrorAction SilentlyContinue
$DesvDSsWitch =get-cluster -Name $vmdetail.tgcluster |Get-VMHost -ErrorAction SilentlyContinue |Get-VDSwitch -Name $vmdetail.TgSwitch -ErrorAction SilentlyContinue
If ($DesvDSsWitch){
Write-Host " Target vDS Switch..." $DesvDSsWitch "Found on Target CLuster... " $Vmdetail.TgCluster -ForegroundColor Green
}
Else {
Write-Host "vDS Switch" $VMdetail.TgSwitch " Cannot be found " -ForegroundColor Red -BackgroundColor white
# continue
}
####################################################################################
# Check if the destination portGroup is available on the vDS switch is availabl
####################################################################################
$DesvDpOrtGroup = Get-VDSwitch -Name $VMdetail.TgSwitch -ErrorAction SilentlyContinue |Get-VDPortgroup -Name $VMdetail.TgPortGroup -ErrorAction SilentlyContinue
If ($DesvDpOrtGroup){
Write-Host " Target Portgroup..." $DesvDpOrtGroup "Found on Target vDS Switch... " $Vmdetail.TgCluster -ForegroundColor Green
}
Else {
Write-Host "Target Portgroup" $VMdetail.TgPortGroup " Canno't be found on Target vDS Switch " $VMdetail.TgSwitch -ForegroundColor Red -BackgroundColor white
# continue
}
####################################################################################
# Check VM Total VMDK size
# Proceed , if unavailable skip to the next record
####################################################################################
$VMSize = ((Get-VM -Name $VMdetail.VMNAME -ErrorAction SilentlyContinue |Get-HardDisk |Measure-Object -Sum capacityGB).Sum)
if ($VMSize) {
Write-Host "Total size of VM is... " -ForegroundColor green $VMSize 'GB'
}
Else {
Write-Host " VM Size cannot be calculated " $vmdetail.VMNAME -ForegroundColor Red -BackgroundColor white
#Continue
}
####################################################################################
# Check if the destination DATASTORE is found
# Proceed , if unavailable skip to the next record
####################################################################################
$DesDatastore = get-cluster -Name $vmdetail.tgcluster |Get-Datastore $VMdetail.TgDatastore -ErrorAction SilentlyContinue
if ($DesDatastore) {
Write-Host "VM will be moved to following datastore... " -ForegroundColor green $DesDatastore
}
Else {
Write-Host " Destination datastore not found" $vmdetail.TgDatastore -ForegroundColor Red -BackgroundColor white
#Continue
}
####################################################################################
# Check destination DATASTORE free space
# Proceed , if unavailable skip to the next record
####################################################################################
$DesDatastoreFreeSpace = [math]::Round((get-cluster -Name $vmdetail.tgcluster |Get-Datastore $VMdetail.TgDatastore|Measure-Object -Sum FreespaceGB).sum)
if ($DesDatastoreFreeSpace) {
Write-Host "Target Datastore Free Space... " -ForegroundColor green $DesDatastoreFreeSpace 'GB'
}
Else {
Write-Host " Destination datastore not found" $vmdetail.TgDatastore -ForegroundColor Red -BackgroundColor white
#Continue
}
$DesCapCheck= $DesDatastoreFreeSpace - $VMSize
if ($DesCapCheck -lt $VMSize) {
Write-Host "Insufficent Datastore capacity to move VM... Migration will Fail.." $DesCapCheck 'GB' -ForegroundColor Red -BackgroundColor white
}
Else {
Write-Host "Sufficent capacity to move VM.. " $VMdetail.VMNAME "Check for Each VM individually .. " -ForegroundColor Yellow
}
DrawSpace
DrawSpace
Drawline
}
The foreach doesn't put anything in the pipeline, hence you can't pipe the output to another cmdlet.
There are a couple of ways around this.
Collect everything in an array.
foreach($obj in $array){
$report += $obj
}
$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Use the call operator (&)
$obj
}} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The foreach doesn't put anything in the pipeline, hence you can't pipe the output to another cmdlet.
There are a couple of ways around this.
Collect everything in an array.
foreach($obj in $array){
$report += $obj
}
$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Use the call operator (&)
$obj
}} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you LucD