# Module CsvHandler - Lecture/ecriture CSV automates function Read-AutomateCsv { [CmdletBinding()] param( [Parameter(Mandatory)] [string]$CsvPath ) if (-not (Test-Path $CsvPath)) { throw "Fichier CSV introuvable : $CsvPath" } $rows = Import-Csv -Path $CsvPath -Delimiter ";" Write-Log -Message "CSV charge : $($rows.Count) lignes depuis $CsvPath" -Level INFO return $rows } function Get-BaseUrl { [CmdletBinding()] param( [Parameter(Mandatory)] [PSCustomObject]$Automate ) $ip = $Automate."Current Ip" $httpsPort = $Automate.HttpsPort $httpPort = $Automate.HttpPort # HTTPS si port > 0 et != -1 if ($httpsPort -and $httpsPort -ne "" -and [int]$httpsPort -gt 0 -and [int]$httpsPort -ne -1) { if ([int]$httpsPort -eq 443) { return "https://$ip" } return "https://${ip}:$httpsPort" } # HTTP si port > 0 if ($httpPort -and $httpPort -ne "" -and [int]$httpPort -gt 0 -and [int]$httpPort -ne -1) { if ([int]$httpPort -eq 80) { return "http://$ip" } return "http://${ip}:$httpPort" } return $null } function Get-ApiBasePath { [CmdletBinding()] param( [Parameter(Mandatory)] [PSCustomObject]$Automate ) $restUrl = $Automate.RestServiceURL if ($restUrl -and $restUrl -ne "") { return $restUrl.TrimEnd("/") } # Fallback v1 return "/api/rest/v1" } function Get-Credentials { [CmdletBinding()] param( [Parameter(Mandatory)] [PSCustomObject]$Automate, [string]$DefaultUsername = "admin", [string]$DefaultPassword = "" ) $username = $DefaultUsername $password = $DefaultPassword # Override depuis le CSV si renseigne if ($Automate.Username -and $Automate.Username -ne "") { $username = $Automate.Username } if ($Automate.Password -and $Automate.Password -ne "") { $password = $Automate.Password } return @{ Username = $username Password = $password } } function Write-OutputCsv { [CmdletBinding()] param( [Parameter(Mandatory)] [array]$InputRows, [Parameter(Mandatory)] [hashtable]$DeviceData, [Parameter(Mandatory)] [string]$OutputPath ) # Trouver le nombre max de devices parmi tous les automates $maxDevices = 0 foreach ($key in $DeviceData.Keys) { $count = $DeviceData[$key].Count if ($count -gt $maxDevices) { $maxDevices = $count } } Write-Log -Message "Max devices par automate : $maxDevices" -Level INFO # Construire les lignes de sortie $outputRows = @() foreach ($row in $InputRows) { # Copier toutes les proprietes existantes $obj = [ordered]@{} foreach ($prop in $row.PSObject.Properties) { $obj[$prop.Name] = $prop.Value } # Ajouter les colonnes dynamiques DeviceId_N / DeviceType_N $ip = $row."Current Ip" $devices = @() if ($DeviceData.ContainsKey($ip)) { $devices = $DeviceData[$ip] } for ($i = 1; $i -le $maxDevices; $i++) { if ($i -le $devices.Count) { $obj["DeviceId_$i"] = $devices[$i - 1].DeviceId $obj["DeviceType_$i"] = $devices[$i - 1].DeviceType } else { $obj["DeviceId_$i"] = "" $obj["DeviceType_$i"] = "" } } $outputRows += [PSCustomObject]$obj } # Creer le repertoire de sortie si necessaire $outputDir = Split-Path $OutputPath -Parent if ($outputDir -and -not (Test-Path $outputDir)) { New-Item -ItemType Directory -Path $outputDir -Force | Out-Null } $outputRows | Export-Csv -Path $OutputPath -Delimiter ";" -NoTypeInformation -Encoding UTF8 Write-Log -Message "CSV de sortie ecrit : $OutputPath ($($outputRows.Count) lignes)" -Level SUCCESS } Export-ModuleMember -Function Read-AutomateCsv, Get-BaseUrl, Get-ApiBasePath, Get-Credentials, Write-OutputCsv