小编Bbb*_*Bbb的帖子

如何在 C# 中使用 powershell 调用命令?

如果我在 Powershell 中运行以下命令,它会按预期工作

invoke-command -computername [name] -scriptblock { ipconfig.exe > c:\ipconfig.txt }

但是当我尝试将其合并到 ac# 函数中时,我收到此错误

{“无法绑定参数“ScriptBlock”。无法将“System.String”类型的“ipconfig.exe > c:\ipconfig.txt”值转换为“System.Management.Automation.ScriptBlock”类型。 ”}

即使我将 scriptblock 参数值转换为 System.Management.Automation.ScriptBlock 对象?我做错了什么?

    private void btnInstallTest_Click(object sender, EventArgs e)
    {
        List<string> commands = new List<string>();

        commands.Add("invoke-command");
        List<Tuple<String, String>> parameters = new List<Tuple<string, string>>();
        parameters.Add(new Tuple<string, string>("computername", @"server"));
        parameters.Add(new Tuple<string, string>("ScriptBlock", @"ipconfig.exe > c:\ipconfig.txt"));

        Collection<PSObject> psobject = runRemotePowerShellCommands(commands, parameters, "server", @"domain\user", convertToSecureString("password"));          
    }

    private Collection<PSObject> runRemotePowerShellCommands(List<string> commands, List<Tuple<String, String>> parameters, string remoteMachineName, string domainAndUsername, SecureString securePW)
    {
        Collection<PSObject> psobjs = …
Run Code Online (Sandbox Code Playgroud)

c# powershell runspace scriptblock

1
推荐指数
1
解决办法
1765
查看次数

在PowerShell脚本中使用c#函数的正确语法是什么?

我能够使用一个简单的c#函数,但是当我介绍一些更复杂的东西时,如下面的内容,我会遇到语法错误,并且没有很多关于如何执行此操作的示例.

我已根据此处收到的建议对代码进行了更新,但此代码仍然无法正常运行

cls


$dagDistribution = $null;

        $distribution = 
        @'

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    using System.Security;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Threading;
    using System.Collections.Concurrent;
    using System.Diagnostics;
    using System.Security.Principal;    


    namespace MultiThreading
    {

        public class dagDistribution
        {

            public List<string> get(string dag)
            {
                DateTime start = DateTime.Now;

                var response = new ConcurrentBag<Collection<PSObject>>();
                var exceptions = new ConcurrentQueue<Exception>();

                string dagName = "hqdag1";

                string[] serversUnsorted = getDagMembers(dagName);
                var servers = …
Run Code Online (Sandbox Code Playgroud)

c# powershell

0
推荐指数
1
解决办法
315
查看次数

标签 统计

c# ×2

powershell ×2

runspace ×1

scriptblock ×1