using Microsoft.Extensions.Logging; using System.Diagnostics; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; namespace File_Folder_Helper.Day; internal record Drive(string Share, bool Use, string User); internal static class Helper20231024 { internal static void NetUse(ILogger logger, string argsZero) { Process? process; string arguments; string[] segments; string standardError; string standardOutput; string fileName = "net"; StringBuilder stringBuilder = new(); string json = File.ReadAllText(Path.Combine(argsZero, ".json")); string decrypted = File.ReadAllText(Path.Combine(argsZero, ".password")); List drives = JsonSerializer.Deserialize(json, DrivesSourceGenerationContext.Default.ListDrive) ?? throw new NullReferenceException(); foreach (Drive drive in drives) { if (!drive.Use) continue; arguments = $"use * \"{drive.Share}\" /p:yes /user:{drive.User} {decrypted}"; _ = stringBuilder.Clear(); segments = arguments.Split(' '); for (int j = 0; j < segments.Length - 1; j++) _ = stringBuilder.Append(segments[j]).Append(' '); logger.LogInformation("// {stringBuilder}", stringBuilder); ProcessStartInfo processStartInfo = new(fileName, arguments) { RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false }; try { process = Process.Start(processStartInfo); if (process is null) continue; for (int j = 1; j < 45; j++) { _ = process.WaitForExit(1000); if (process.HasExited) break; } if (!process.HasExited) logger.LogError("// Never exited!"); else { standardError = process.StandardError.ReadToEnd(); standardOutput = process.StandardOutput.ReadToEnd(); logger.LogInformation("// {standardError}{Environment.NewLine}{Environment.NewLine}// {standardOutput}", standardError, Environment.NewLine, Environment.NewLine, standardOutput); try { _ = Directory.CreateDirectory($"{drive.Share}\\Tmp\\Phares"); string reg = """ Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\ODBC\ODBC.INI\SPCEPIWORLD] "Driver"="C:\\WINDOWS\\system32\\SQLSRV32.dll" "Description"="InfinityQS SPC (Si)" "Server"="messqlec1.infineon.com\\PROD1,53959" "Database"="SPCEPIWORLD" "LastUser"="" "Trusted_Connection"="Yes" [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\ODBC\ODBC.INI\ODBC Data Sources] "SPCEPIWORLD"="SQL Server" """; string bat = """ @ECHO OFF NET USE G: /delete NET USE H: /delete NET USE M: /delete NET USE P: /delete NET USE R: /delete NET USE T: /delete NET USE V: /delete NET USE Y: /delete NET USE G: \\mesfs.infineon.com\EC_Engineering /PERSISTENT:YES NET USE H: \\mesfs.infineon.com\EC_TempHumidity_Controls /PERSISTENT:YES NET USE M: \\mesfs.infineon.com\EC_Maintenance /PERSISTENT:YES NET USE P: \\mesfs.infineon.com\EC_Production /PERSISTENT:YES NET USE R: \\mesfs.infineon.com\EC_Customer_Service /PERSISTENT:YES NET USE T: \\mesfs.infineon.com\EC_Materials /PERSISTENT:YES NET USE V: \\mesfs.infineon.com\EC_Engineering /PERSISTENT:YES NET USE Y: \\mesfs.infineon.com\EC_EpiReactorRecipes /PERSISTENT:YES """; File.WriteAllText($"{drive.Share}\\Tmp\\Phares\\SPCEPIWORLD_hklm.reg", reg); File.WriteAllText($"{drive.Share}\\Tmp\\Phares\\MESA-Users-Drives.bat", bat); File.WriteAllText($"{drive.Share}\\Tmp\\Phares\\lnk.txt", """"\\messa04ec.infineon.com\EC_SPC_Si\SPC\Projects\Active\ir epi services database.ipj""""); } catch (Exception) { } } } catch (Exception ex) { logger.LogError(ex, "Error:"); } } } } [JsonSourceGenerationOptions(WriteIndented = true)] [JsonSerializable(typeof(Drive))] internal partial class DriveSourceGenerationContext : JsonSerializerContext { } [JsonSourceGenerationOptions(WriteIndented = true)] [JsonSerializable(typeof(List))] internal partial class DrivesSourceGenerationContext : JsonSerializerContext { }