VMware Cloud Community
Sarvani5
Contributor
Contributor

Cannot append CSV content to the following file: C:\*****\Out.csv and ConvertFrom-StringData : Data

HI Team,

Iam getting two errors first is missing one column output by saying the error below:

Export-CSV : Cannot append CSV content to the following file: C:\***\Datastore_SpaceUsage_Report.csv. The appended object does not have a property that corresponds to the following column: Vcenter

and second is, when script is executed with same server details, I am getting below error. Please help me to rectify this issue.

ConvertFrom-StringData : Data item 'DatastoreName' in line '
DatastoreName=MR**10' is already defined.

[CmdletBinding()]
param(
[Parameter(Position=0)]
[String[]]$Datastore=@()
#[Parameter(Position=1)]
#[String[]]$Company
)
#$DatastoreName=$DatastoreName.Split(',').Split(' ')

$time_now = Get-Date -format "yyyy_MM_dd HH_mm_ss"
Import-Module "C:\****\\Scripts\LOGGER.psm1" -Force
$logDirectory = "C:\****\\Logs\"
New-Log "$logDirectory" "Datastore_Usage_$time_now.log"

write-log $Datastore
foreach($dsname in $Datastore)
{
Write-log "element: $dsname"
}

#$OFS = "`n"
#[String]$Datastores=@()
#$DatastoreName=([string]$Datastores)
#write-host "Datastores are $DatastoreName"
#$DatastoreName=$DatastoreName.Split(',').Split(' ')
#$OFS+$DatastoreName

write-log $dsname

 

$code = {
param(
[string]$viname,
[string]$SessionId,
[string]$dsname,
#[string]$Company,
$Credentials,
$csvfile,
$OutfileVell

)
#try
#{
Import-Module "C:\****\\Scripts\LOGGER.psm1" -Force
$logDirectory = "C:\****\\Logs\"
$logname = "Datastore_Usage_$time_now.log"
$logname = $logname.replace("'","")
New-Log "$logDirectory" $logname

$global:OutfileVell = "C:\****\\Outputfile\Datastore_SpaceUsage_Report.csv"
$newcsv = {} | Select "Vcenter","DatastoreName","Initial_Status","Final_Status","Priority","Incident_Number","Incident_reported_date"| Export-Csv $OutfileVell -NoTypeInformation

$global:csvfile = Import-Csv $OutfileVell

Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Confirm:$false | Out-Null
$Connectvi=Connect-VIServer $viname -Credential $Credentials -Session $SessionId
$Vcenter=$Connectvi.Name
write-log "Connected to vcenter $Vcenter"


$Exists = Get-Datastore -Server $viname|where-object{$_.Name -match $dsname} -ErrorAction SilentlyContinue
#Write-Host "Exists is $Exists"


if ($Exists)
{
#write-host "$Exists datastore is present in $viname"
$worklog= "Current Datastore space Usage is: "
$Notes= "High Datastore space Usage in $dsname with"
$desc="High Datastore space Usage in $dsname"

#$datastore=Get-Datastore |where-object{$_.Name -match $dsname}|Select Name,FreeSpaceGB,CapacityGB,
#@{N='Used Space(%)';E={[math]::Round((($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*50),1)}}

#write-log $datastore

#$datastore_usage=$datastore.'Used Space(%)'


$datastore_usage= 96
#write-host "$datastore_usage"

write-log "$Exists Datastore present in Vcenter and datastore usage is $datastore_usage%"
#write-host "$Exists Datastore present in Vcenter and datastore usage is $datastore_usage%"

if($datastore_usage -ge 95)
{
#Write-Host "Datastore usage is $datastore_usage%. Hence Monitoring for 5 Minutes"
Write-log "Datastore usage is $datastore_usage%. Hence Monitoring for 5 Minutes"

Start-Sleep -Seconds 1

#$datastore_usage1= Get-Datastore |where-object{$_.Name -match $dsname}|Select Name,FreeSpaceGB,CapacityGB,
# @{N='Used Space(%)';E={[math]::Round((($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*50),1)}}
$datastore_usage1= 92

if($datastore_usage1 -ge 95)
{
Write-log "Datastore Usage is $datastore_usage1% after monitoring for 5 Minutes. Hence proceeding with P2"

#write-host "$dsname is the CI for incident"
$inc=( & "C:\****\\Scripts\Incident_Creation2-Vell.ps1" -work_log ("$worklog"+"$datastore_usage1%") -notes ("$Notes"+"$datastore_usage1%") -desc "$desc" -impact '2-Significant/Large' -urgency '2-High' -ErrorAction SilentlyContinue)

$outputstat= "Post monitoring for 5 Minutes Datastore free space is $datastore_usage1%. "+"$out"

$priority="High"

#Write-Host $outputstat
#Write-Host $out
#Write-host $Incident_Number
Write-log $out
Write-log $Incident_Number

#$csvfile.Datastore_Status=$outputstat

}
elseif(($datastore_usage1 -ge 90) -and($datastore_usage1 -le 95))
{
Write-log "Datastore Usage is $datastore_usage1% after monitoring for 5 Minutes. Hence proceeding with P4"
start-sleep -Seconds 2

$inc=( & "C:\****\\Scripts\Incident_Creation2-Vell.ps1" -work_log ("$worklog"+"$datastore_usage1%") -notes ("$Notes"+"$datastore_usage1%") -desc "$desc" -impact '4-Minor/Localized' -urgency '4-Low' -ErrorAction SilentlyContinue)

$outputstat= "Post monitoring for 5 Minutes Datastore free space is $datastore_usage1%. "+"$out"
$Initial="Initial value is $datastore_usage1%"

write-log $outputstat
#Write-Host $outputstat
$priority="Low"
#Write-Host $out
#Write-host $Incident_Number
Write-log $out
Write-log $Incident_Number

}
elseif($datastore_usage1 -le 90)
{
$outputstat="Datastore Usage is back to normal with $datastore_usage1%. Hence no incident created"
write-log $outputstat
#Write-Host $outputstat

}
}
elseif(($datastore_usage -ge 90) -and ($datastore_usage -le 95))
{
Write-log "Datastore usage is greater than 90%.Hence Monitoring for 5 Minutes"
#Write-Host "Datastore usage is greater than 90%.Hence Monitoring for 5 Minutes"

Start-Sleep -Seconds 1

$datastore_usage2=92
#$datastore_usage2= Get-Datastore |where-object{$_.Name -match $dsname}|Select Name,FreeSpaceGB,CapacityGB,
# @{N='Used Space(%)';E={[math]::Round((($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB*50),1)}}
if($datastore_usage2 -ge 95)
{

Write-log "Datastore Usage is $datastore_usage2% after monitoring for 5 Minutes. Hence proceeding with P2"

$inc=( & "C:\****\\Scripts\Incident_Creation2-Vell.ps1" -work_log ("$worklog"+"$datastore_usage2%") -notes ("$Notes"+"$datastore_usage2%") -desc "$desc" -impact '2-Significant/Large' -urgency '2-High' -ErrorAction SilentlyContinue)

$outputstat= "Post monitoring for 5 Minutes Datastore free space is $datastore_usage2%. "+"$out"
#write-host $outputstat
$priority="High"
#Write-Host $out
#Write-host $Incident_Number
Write-log $out
Write-log $Incident_Number

}

elseif(($datastore_usage2 -ge 90) -and ($datastore_usage2 -le 95))
{
Write-log "Datastore Usage is $datastore_usage1% after monitoring for 5 Minutes. Hence proceeding with P4"
start-sleep -Seconds 3

$inc=( & "C:\****\\Scripts\Incident_Creation2-Vell.ps1" -work_log ("$worklog"+"$datastore_usage2%") -notes ("$Notes"+"$datastore_usage2%") -desc "$desc" -impact '4-Minor/Localized' -urgency '4-Low' -ErrorAction SilentlyContinue)

$outputstat= "Post monitoring for 5 Minutes Datastore free space is $datastore_usage2%. "+"$out"

#write-host $outputstat
$priority="Low"
#Write-Host $out
# Write-host $Incident_Number
Write-log $out
Write-log $Incident_Number

}
elseif($datastore_usage2 -le 90)
{
$outputstat= "Datastore Usage is back to normal with $datastore_usage2%.Hence no incident is created."
write-log $outputstat
#write-host $outputstat

}

}

#Write-Host "*********************************************"
#write-host "Atcsvfile $viname,$dsname"

$csvfile.Vcenter = $viname
#write-host "at $viname"
$csvfile.DatastoreName = $dsname
$csvfile.Initial_Status = "Initial Datastore usage is $datastore_usage%"
$csvfile.Final_Status= $outputstat
$csvfile.Priority=$Priority
$csvfile.Incident_Number=$Incident_Number
$csvfile.Incident_reported_date=$creation_date

write-host $csvfile
#$csvfile.GetType()

#write-log "$csvfile.Datastore is entered"
}
else
{

Write-log " $dsname is not present in $viname"
#Write-Host "*********************************************"
#write-host "Datastore not exists"
#$Exception = $_.Exception
#$Error_line_number = $_.InvocationInfo.ScriptLineNumber
#$Exception_msg = $Exception.Message
#Write-Log $Exception_msg,$Error_line_number
#$csvfile.Vcenter = $viname
#$csvfile.Datastore = $dsname
#$csvfile.Initial_Status = "Datastore is not present in $viname. Hence Unable to connect."
#$csvfile.Datastore_Status= $null
#$csvfile | Export-CSV $Outfile –Append -Force
}
#}

<#catch
{

$Exception = $_.Exception
$Error_line_number = $_.InvocationInfo.ScriptLineNumber
$Exception_msg = $Exception.Message
#$csvfile = Import-Csv $outfile
##"Vcenter","VMName","Status"
$csvfile.Vcenter = $Exception_msg + $Error_line_number
$csvfile.Datastore = $Exception_msg + $Error_line_number
$csvfile.Initial_Status = $Exception_msg + $Error_line_number
$csvfile.Monitored_Status = $Exception_msg + $Error_line_number
$csvfile.Priority=$Priority
$csvfile.Incident_Number=$Exception_msg + $Error_line_number
Write-Log "error occured"
$csvfile | Export-CSV $OutfileVell –Append -Force

}#>
}

<#$OFS = "`n"
#[String[]]$DatastoreName=@()
#$DatastoreName=Read-Host "Enter Datastores"
$DatastoreName=$DatastoreName.Split(',').Split(' ')
$OFS+$DatastoreName#>

 

##Providing Vcenter details as input

$global:VC_input_file = "C:\****\\Inputfile\inputfile.csv"
(Get-Content $VC_input_file) | Set-Content $VC_input_file -Encoding UTF8
$global:csv = Import-Csv -path $VC_input_file

$global:OutfileVell = "C:\****\\Outputfile\Datastore_SpaceUsage_Report.csv"
$newcsv = {} | Select "Vcenter","DatastoreName","Initial_Status","Final_Status","Priority","Incident_Number","Incident_reported_date"| Export-Csv $OutfileVell -NoTypeInformation

$global:csvfile = Import-Csv $OutfileVell

$sJOb = @{
ScriptBlock = $code
ArgumentList = $global:DefaultVIServer.Name, $global:DefaultVIServer.SessionId, $dsname, $Credentials,$global:csvfile,$OutfileVell
}
$jobs = @()

#write-Host "before loop $Datastore"

foreach($viname in $csv)
{
foreach($dsname in $Datastore)
{
#write-host "$dsname"
$viname=$line.VCenterName
Connect-VIServer -Server $viname -Credential $Credentials
$sJob.ArgumentList=$global:DefaultVIServer.Name,$global:DefaultVIServer.SessionId,$dsname,$Credentials,$global:csvfile,$OutfileVell
Start-Sleep -Seconds 2
$jobs += Start-Job @sjob
Start-Sleep -Seconds 2
}
}
#Start-Job @sjob

#$j = Start-Job @sjob
#Start-Sleep -Seconds 3
#Receive-Job -Job $jobs -Wait

#Get-Job

# Wait for it all to complete

#Wait for all jobs to finish.
While ($(Get-Job -State Running).count -gt 0)
{
Start-Sleep -Milliseconds 3
}

#Get-Job -ChildJobState Completed -HasMoreData:$True
#$path= "C:\****\\Outputfile\Datastore_SpaceUsage_Report.csv"
#$csvfile_output=Get-job | Receive-Job 6>&1
#$csvfile_output|Export-csv -Path $path
[string]$csvfile_output = Get-job | Receive-Job 6>&1
$CharArray =$csvfile_output.Split("@")
foreach ($output in $csvfile_output)
{


$output = $output -replace '{' -replace '}' -replace ';',"`n" -replace "'" | ConvertFrom-StringData
$output | ForEach-Object{ [pscustomobject]$_ } | Export-CSV $OutfileVell –Append -NoTypeInformation -Force
}



$a = "<style>"
#$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "TABLE{border-width: 3px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 3px;padding: 0px;border-style: solid;border-color: black;background-color:blue}"
$a = $a + "TD{border-width: 3px;padding: 6px;border-style: solid;border-color: black;background-color:white}"
$a = $a + "</style>"

# HTML table headers
#$strHTML = "<table border='2'cellpadding='1' cellspacing='2' style='text-align:left;'><tr><th>Vcenter</th><th>Hostname</th><th>Initial_Value</th><th>Final_Value</th><th>Priority</th><th>Incident_Number</th></tr>"
$data= Import-Csv -Path "C:\****\\Outputfile\Datastore_SpaceUsage_Report.csv"|ConvertTo-Html -head $a
$Servers=$Datastore -join','

 

0 Kudos
7 Replies
LucD
Leadership
Leadership

Your 1st error is due to the fact that you can't append to a CSV file when the lines you want to append have one or more properties that are not in the CSV (as columns).

For the 2nd error, you would need to see which line from $csvfile_output causes that error.
Perhaps add a debug line in that loop where you display the content of $output.

PS: 336 lines of code can perhaps better be included as an attached file




Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership

Btw, how is that other thread of yours going?
You never gave any feedback after my last reply.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sarvani5
Contributor
Contributor

Hi Team,

For the first error the CSV columns names and count is same as the data i want to append, But still i am getting error only for Vcenter(1st column), remaining data is exporting properly.

For the 2nd error: I am getting as below.

ConvertFrom-StringData : Data item 'DatastoreName' in line '
DatastoreName=M**10' is already defined.
At C:\****\Scripts\Datastoreconnect.ps1:347 char:93
+ ... -replace '}' -replace ';',"`n" -replace "'" | ConvertFrom-StringData
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [ConvertFrom-StringData],
PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.C
onvertFromStringDataCommand

0 Kudos
Sarvani5
Contributor
Contributor

HI ,In other thread mentioned background job issue is resolved.

But there also i am getting mentioned 2 errors, since i am following same approach to get output from CSV to HTML table format

0 Kudos
LucD
Leadership
Leadership

How do you expect someone to analyse this issue without you sharing the content of the CSV you start with, or at least showing how you created that CSV?

Thanks for letting me know that the issue in the other thread was fixed.
Always great to get feedback



Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sarvani5
Contributor
Contributor

How i created the CSV is in the code only which i shared in the earlier post.

 

$global:OutfileVell = "C:\****\\Outputfile\Datastore_SpaceUsage_Report.csv"
$newcsv = {} | Select "Vcenter","DatastoreName","Initial_Status","Final_Status","Priority","Incident_Number","Incident_reported_date"| Export-Csv $OutfileVell -NoTypeInformation

$global:csvfile = Import-Csv $OutfileVell

 

However the issue is fixed now. Thank you

0 Kudos
LucD
Leadership
Leadership

And you are not going to tell us what the issue was?
Just like you did in your other thread.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos