beben
June 4, 2018, 10:20am
1
Hello
I have a little problem with a script and export-cvs
no result with this scrip
get-content -Path 'Sw_Ala-PileBureaux.cfg' | foreach-object {
$UneLigne = $_
if ($UneLigne.Contains("interface port1.") -or $UneLigne.Contains("interface port2."))
{
$interface = $UneLigne
$interface = $interface.Substring(14)
Write-Host "Port :" $interface.Trim()
}
if ($UneLigne.Contains("description"))
{
$description = $UneLigne
$description = $description.Substring(12)
Write-Host "Description" : $description.Trim()
}
if ($UneLigne.Contains("switchport") -and $UneLigne.Contains("vlan"))
{
$vlan = $UneLigne
$vlandebut = $vlan.LastIndexOfAny("add")
$vlan = $vlan.Substring($vlandebut + 1)
Write-Host "Vlan :" $vlan.Trim()
}
} | export-csv 'Results.csv' -notype
I don’t understand because I use export-csv in other scrip and no problem.
Do you have any idea ?
Write-Host executes direct to the console. Even using Write-Output would just write the string length using Export-Csv (at least in my tests it did). I’d use PSCustomObject to create the table to export.
get-content -Path 'Sw_Ala-PileBureaux.cfg' | foreach-object {
$UneLigne = $_
if ($UneLigne.Contains("interface port1.") -or $UneLigne.Contains("interface port2."))
{
$interface = $UneLigne
$interface = $interface.Substring(14)
}
if ($UneLigne.Contains("description"))
{
$description = $UneLigne
$description = $description.Substring(12)
}
if ($UneLigne.Contains("switchport") -and $UneLigne.Contains("vlan"))
{
$vlan = $UneLigne
$vlandebut = $vlan.LastIndexOfAny("add")
$vlan = $vlan.Substring($vlandebut + 1)
}
[PSCustomObject]@{
Port = $interface.Trim()
Description = $description.Trim()
Vlan = $vlan.Trim()
}
} | export-csv 'Results.csv' -notype
The Contains operator or Method (as your using) is to determine if something exists in an array:
PS C:\Users\Rob> "one","two" -contains "two"
True
You should be using the -Like operator with a wildcard:
$UneLigne -like "*interface port1.*" -or $UneLigne -like "*interface port2.*"