【问题标题】:Multiple lined Powershell block from C#来自 C# 的多行 Powershell 块
【发布时间】:2021-04-09 20:10:34
【问题描述】:

使用此代码从 C# 运行我的 Powershell 代码,一切正常

        using(PowerShell powershellCommand = PowerShell.Create())
        {
            powershellCommand.AddScript(File.ReadAllText(@"C:\Path_to_script")).Invoke();
            System.Collections.ObjectModel.Collection<System.Management.Automation.PSObject> results = powershellCommand.Invoke();
            foreach (PSObject result in results)
            {
                logg.AppendText(result + ""); 
            }
        }

脚本文件中的代码。

$env:tmp = "env_location"
Import-PSSession $(New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 
server_url) | Out-Null
$device = Get-Mailbox -Identity IDENTITY | Get-MailboxStatistics | Select-Object TotalItemSize

return $device

IDENTITY 是这里的问题,因为我希望它是动态的,因此不能硬编码到脚本文件中。 我已经尝试过Execute multiple line PowerShell Script from C# 中的解决方案,如下所示:

        using(PowerShell powershellCommand = PowerShell.Create())
        {
            string script = @"
            $env:tmp = 'env_location'
            Import-PSSession $(New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri server_url) | Out-Null
            $device = Get-Mailbox -Identity DYNAMIC_IDENTITY | Get-MailboxStatistics | Select-Object TotalItemSize

            return $device
            ";

            powershellCommand.AddScript(script)).Invoke();
            System.Collections.ObjectModel.Collection<System.Management.Automation.PSObject> results = powershellCommand.Invoke();
            foreach (PSObject result in results)
            {
                logg.AppendText(result + ""); 
            }
        }

感觉这应该可行,但根本没有输出。也没有崩溃,有什么想法吗?

谢谢

【问题讨论】:

  • 您肯定只是希望 Identity 在您的 PowerShell 脚本中成为一个变量,然后在您的 dotnet 程序中成为 pass it as a parameter to that script
  • 另外,您似乎在 PowerShell 命令上调用了两次调用。过去,当我不得不从 dotnet 应用程序调用脚本时,如果我期待 PowerShell 的输出,我倾向于改用 BeginInvoke()EndInvoke()
  • @Ash 谢谢,那太好了,但我不明白它的语法。我试过: powershellCommand.AddScript(File.ReadAllText(@"path_to_script")).AddCommand("Get-Mailbox").AddParameter("Identity","test-identity");和: powershellCommand.AddScript(File.ReadAllText(@"path_to_script")).AddParameter("$variable_in_script","test-identity");

标签: c# powershell


【解决方案1】:

按照我上面的cmets,试试这个。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace PowerShellSample
{
    public class Exchange
    {
        public static Collection<PSObject> GetTotalMailboxSize(string identity)
        {
            using(PowerShell powershellCommand = PowerShell.Create())
            {
                powershellCommand.AddScript(File.ReadAllText(@"C:\\Path_to_script")).AddParameter("Identity", identity);
                IAsyncResult poshTask = powershellCommand.BeginInvoke();
                Collection<PSObject> returnCollection = new Collection<PSObject>();
                foreach (PSObject psObj in powershellCommand.EndInvoke(poshTask))
                {
                    returnCollection.Add(psObj);
                }
                return returnCollection;
            }
        }
    }
}

PowerShell 脚本:

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true)]
    [System.String]
    $Identity
)

$env:tmp = "env_location"
Import-PSSession $(New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri server_url) | Out-Null
$Device = Get-Mailbox -Identity $Identity | Get-MailboxStatistics | Select-Object TotalItemSize

return $device

如果您想提供更多参数,您可以在方法中使用Dictionary&lt;string, Object&gt;。这样您就可以传递您的 ConnectionUri 或您可能需要的任何其他参数。

public static Collection<PSObject> GetTotalMailboxSize(string identity, Dictionary<string, Object> parameters)
{
    using(PowerShell powershellCommand = PowerShell.Create())
    {
        powershellCommand.AddScript(File.ReadAllText(@"C:\\Path_to_script"));
        foreach (var key in parameters.Keys)
        {
            powershellCommand.AddParameter(key, parameters[key]);
        }
        IAsyncResult poshTask = powershellCommand.BeginInvoke();
        Collection<PSObject> returnCollection = new Collection<PSObject>();
        foreach (PSObject psObj in powershellCommand.EndInvoke(poshTask))
        {
            returnCollection.Add(psObj);
        }
        return returnCollection;
    }
}

【讨论】:

  • 太棒了!非常感谢你花时间给我看这个:)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
  • 2022-01-23
  • 2013-06-06
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
相关资源
最近更新 更多