简体   繁体   English

通过C#执行PowerShell Function脚本

[英]Executing PowerShell Function Script Through C#

I'm trying to learn how to incorporate PowerShell into a WPF/C# GUI I'm working on.我正在尝试学习如何将 PowerShell 合并到我正在处理的 WPF/C# GUI 中。 I'd like the user to be able to click a button, have the PowerShell script execute, and then returns the information and have it write the output to a richtextbox .我希望用户能够单击一个按钮,执行 PowerShell 脚本,然后返回信息并将 output 写入richtextbox

Here is the PowerShell:这是 PowerShell:

Function Get-MappedPrinters {

[Cmdletbinding()]
Param(
    [alias('dnsHostName')]
    [Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
    [string]$ComputerName = $Env:COMPUTERNAME
)

$id = Get-WmiObject -Class win32_computersystem -ComputerName $ComputerName |
Select-Object -ExpandProperty Username |
ForEach-Object { ([System.Security.Principal.NTAccount]$_).Translate([System.Security.Principal.SecurityIdentifier]).Value }

$path = "Registry::\HKEY_USERS\$id\Printers\Connections\"

Invoke-Command -Computername $ComputerName -ScriptBlock {param($path)(Get-Childitem $path | Select PSChildName)} -ArgumentList $path | Select -Property * -ExcludeProperty PSComputerName, RunspaceId, PSShowComputerName

} }

And here is the C#这是 C#

    private void SystemTypeButton_Click(object sender, RoutedEventArgs e)
    {
        using (PowerShell ps = PowerShell.Create())
        {
            ps.AddScript(File.ReadAllText(@"..\..\Scripts\systemtype.ps1"), true).AddParameter("ComputerName", ComputerNameTextBox.Text).AddCommand("Out-String");

            var results = ps.Invoke();
            MainRichTextBox.AppendText(results.ToString());
        }
    }

However, it is only returning the object and not its properties.但是,它只返回 object 而不是它的属性。 "System.Collections.ObjectModel.Collection1[System.Management.Automation.PSObject]" . "System.Collections.ObjectModel.Collection1[System.Management.Automation.PSObject]"

Is there a way to iterate through the object?有没有办法遍历 object?

You can iterate thru the object using foreach loop like any other array.您可以像任何其他数组一样使用foreach循环遍历 object。

Also it is advisable to handle exceptions by adding a try catch block, and handling powershell errors by getting the error buffer using ps.Streams.Error can be helpful also.此外,建议通过添加 try catch 块来处理异常,并通过使用ps.Streams.Error获取错误缓冲区来处理 powershell 错误也很有帮助。

using (PowerShell ps = PowerShell.Create())
{
     ps.AddScript(File.ReadAllText(@"..\..\Scripts\systemtype.ps1"), true).AddParameter("ComputerName", ComputerNameTextBox.Text).AddCommand("Out-String");
    Try
    {
         System.Collections.ObjectModel.Collection<PSObject> results = ps.Invoke();
    }
    catch (Exception e)
    {
         Console.WriteLine(e.Message);
    }

    foreach (var test in results)
          MainRichTextBox.AppendText(test.ToString());
}

Related questions:相关问题:

Get Powershell errors from c# 从 c# 获取 Powershell 错误

How to read PowerShell exit code via c# 如何通过c#读取PowerShell退出码

C# Powershell Pipeline foreach-object C# Powershell 每个对象的管道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM