gpt4 book ai didi

c# - Powershell 结果集未在 C# 中获取

转载 作者:太空狗 更新时间:2023-10-29 21:53:04 26 4
gpt4 key购买 nike

单击 button1 时,将执行以下代码,该代码将运行 PowerShell 脚本以获取当前的 SQL Server 实例。但是,当它运行时,结果集(结果变量)从 PowerShell 输出中计数为 0 行。当我在 native PowerShell 中运行相同的代码时,它会显示 3 行实例名称。

如果我遗漏了什么,有人可以告诉我吗?

private void button1_Click(object sender, EventArgs e)
{
//If the logPath exists, delete the file
string logPath = "Output.Log";
if (File.Exists(logPath))
{
File.Delete(logPath);
}
string[] Servers = richTextBox1.Text.Split('\n');

//Pass each server name from the listview to the 'Server' variable
foreach (string Server in Servers)
{
//PowerShell Script
string PSScript = @"
param([Parameter(Mandatory = $true, ValueFromPipeline = $true)][string] $server)

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force;
Import-Module SQLServer;
Try
{
Set-Location SQLServer:\\SQL\\$server -ErrorAction Stop;
Get-ChildItem | Select-Object -ExpandProperty Name;
}
Catch
{
echo 'No SQL Server Instances';
}
";

//Create PowerShell Instance
PowerShell psInstance = PowerShell.Create();

//Add PowerShell Script
psInstance.AddScript(PSScript);

//Pass the Server variable in to the $server parameter within the PS script
psInstance.AddParameter("server", Server);

//Execute Script
Collection<PSObject> results = new Collection<PSObject>();
try
{
results = psInstance.Invoke();
}
catch (Exception ex)
{
results.Add(new PSObject((Object)ex.Message));
}

//Loop through each of the results in the PowerShell window
foreach (PSObject result in results)
{
File.AppendAllText(logPath, result + Environment.NewLine);
// listBox1.Items.Add(result);
}
psInstance.Dispose();
}
}

最佳答案

为了获得可能的 PowerShell 错误,我会尝试某事。像这样:

private void button1_Click(object sender, EventArgs e)
{
//If the logPath exists, delete the file
string logPath = "Output.Log";
if (File.Exists(logPath)) {
File.Delete(logPath);
}

string[] Servers = richTextBox1.Text.Split('\n');

//Pass each server name from the listview to the 'Server' variable
foreach (string Server in Servers) {
//PowerShell Script
string PSScript = @"
param([Parameter(Mandatory = $true, ValueFromPipeline = $true)][string] $server)

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force;
Import-Module SQLServer;
Try
{
Set-Location SQLServer:\\SQL\\$server -ErrorAction Stop;
Get-ChildItem | Select-Object -ExpandProperty Name;
}
Catch
{
echo 'No SQL Server Instances';
}
";
using (PowerShell psInstance = PowerShell.Create()) {
psInstance.AddScript(PSScript);
psInstance.AddParameter("server", Server);
Collection<PSObject> results = psInstance.Invoke();
if (psInstance.Streams.Error.Count > 0) {
foreach (var errorRecord in psInstance.Streams.Error) {
MessageBox.Show(errorRecord.ToString());
}
}
foreach (PSObject result in results) {
File.AppendAllText(logPath, result + Environment.NewLine);
// listBox1.Items.Add(result);
}
}

}
}

关于c# - Powershell 结果集未在 C# 中获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41388874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com