$tmpFile, 'delimiter' => $delimiter, 'has_header' => (bool)((int)$hasHeader), 'dry_run' => (bool)($config['powershell']['dry_run'] ?? false), ]; // Save options as JSON as the input to the PS script $metaFile = tempnam(sys_get_temp_dir(), 'create_users_meta_') . '.json'; file_put_contents($metaFile, json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); $scriptDir = $config['powershell']['script_dir'] ?? __DIR__ . '/../../scripts/powershell'; $script = $scriptDir . DIRECTORY_SEPARATOR . 'create_users_csv.ps1'; $exe = $config['powershell']['exe'] ?? 'powershell'; $executionPolicy = $config['powershell']['execution_policy'] ?? 'Bypass'; $cmd = sprintf( '%s -NoProfile -NonInteractive -ExecutionPolicy %s -File "%s" -InputFile "%s"', $exe, $executionPolicy, $script, $metaFile ); $output = []; $returnVar = null; if (!file_exists($script)) { $_SESSION['flash_error'] = 'PowerShell-Skript nicht gefunden: ' . $script; @unlink($tmpFile); @unlink($metaFile); header('Location: ../index.php?route=createuser'); exit; } exec($cmd . ' 2>&1', $output, $returnVar); $json = implode("\n", $output); @unlink($tmpFile); @unlink($metaFile); // Optional: log the CSV script command and raw output to help debugging @file_put_contents(__DIR__ . '/../logs/create_users_csv_output.log', date('Y-m-d H:i:s') . ' CMD: ' . $cmd . "\n" . $json . "\n\n", FILE_APPEND | LOCK_EX); $result = null; if ($json !== '') { $decoded = json_decode($json, true); if (is_array($decoded)) { $result = $decoded; } } if ($result === null) { $_SESSION['flash_error'] = 'Unbekannter Fehler beim Ausführen des PowerShell-Skripts: ' . ($json ?: 'Keine Ausgabe'); header('Location: ../index.php?route=createuser'); exit; } if (!empty($result['success'])) { $_SESSION['flash_success'] = $result['message'] ?? 'CSV verarbeitet.'; if (!empty($result['details'])) { $_SESSION['csv_details'] = $result['details']; } } else { $_SESSION['flash_error'] = $result['message'] ?? 'Fehler beim Verarbeiten der CSV.'; } header('Location: ../index.php?route=createuser'); exit;