static string RunPowerShellCommand(string copyaspowershell) { try { int timeoutSeconds = 30; // 30 saniye de cevap gelmezse boş döner using (PowerShell PowerShellInstance = PowerShell.Create()) { PowerShellInstance.AddScript(copyaspowershell); IAsyncResult result = PowerShellInstance.BeginInvoke(); bool completed = result.AsyncWaitHandle.WaitOne(timeoutSeconds * 1000); if (!completed) { PowerShellInstance.Stop(); return "PowerShell Komutu Zaman Aşımına Uğradı"; } Collection<PSObject> PSOutput = new Collection<PSObject>(PowerShellInstance.EndInvoke(result)); if (PowerShellInstance.HadErrors) { string errorMessage = string.Join("\n", PowerShellInstance.Streams.Error); return $"PowerShell Error:\n{errorMessage}"; } string output = string.Join("\n", PSOutput); return output; } } catch (Exception ex) { return "Hata!!\n" + ex.Message; } } |