简体   繁体   English

将变量从C#传递到Powershell

[英]Passing Variables from C# to Powershell

I am working on a C# project that that is supposed to grab a string variable (file path) and pass it to PowerShell script to have further commands done with it. 我正在研究一个C#项目,该项目应该获取一个字符串变量(文件路径),并将其传递给PowerShell脚本以使用它完成更多命令。 I have been looking around online and through Stack and have not been able to find something that works for me... 我一直在网上浏览并通过Stack查找,但是找不到适合我的东西...

Here is my C# code as it stands right now: 这是我现在的C#代码:

string script = System.IO.File.ReadAllText(@"C:\my\script\path\script.ps1");

using (Runspace runspace = RunspaceFactory.CreateRunspace())
{

    runspace.Open();
    PowerShell ps = PowerShell.Create();
    ps.Runspace = runspace;
    ps.AddScript(script);
    ps.Invoke();
    ps.AddCommand("LocalCopy");

    foreach (PSObject result in ps.Invoke())
    {
        Console.WriteLine(result);
    }
}                   

Here is my PowerShell script: 这是我的PowerShell脚本:

Function LocalCopy
{
    Get-ChildItem -path "C:\Users\file1\file2\file3\" -Filter *.tib -Recurse | 
    Copy-Item -Destination "C:\Users\file1\file2\local\"
}

What I want to do is have the first part of the the script: "C:\\Users\\file1\\file2\\file3\\" replaced with (what i am assuming would be) a variable that I could pass from the C# code to the PowerShell script. 我想做的是将脚本的第一部分: "C:\\Users\\file1\\file2\\file3\\"替换为(我假设是)可以从C#代码传递给PowerShell脚本。 I am very new to working with PowerShell and am not quite sure how I would go about doing something like this. 我对使用PowerShell非常陌生,并且不确定如何做这样的事情。

---EDIT--- - -编辑 - -

I am still having issues with my code, but i am not getting any errors. 我的代码仍然有问题,但没有收到任何错误。 I believe that it is because the variable is still not being passed through... 我相信这是因为变量仍然没有通过...

C# code: C#代码:

string script = System.IO.File.ReadAllText(@"C:\\my\\script\\path\\script.ps1"); 字符串脚本= System.IO.File.ReadAllText(@“ C:\\ my \\ script \\ path \\ script.ps1”);

using (Runspace runspace = RunspaceFactory.CreateRunspace())
{

    runspace.Open();
    PowerShell ps = PowerShell.Create();
    ps.Runspace = runspace;
    ps.AddScript(script);
    ps.Invoke();
    ps.AddArgument(FilePathVariable);
    ps.AddCommand("LocalCopy");

    foreach (PSObject result in ps.Invoke())
    {
        Console.WriteLine(result);
    }
}

PowerShell code: PowerShell代码:

Function LocalCopy
{
    $path = $args[0]
    Get-ChildItem -path $path -Filter *.tib -Recurse | 
    Copy-Item -Destination "C:\Users\file1\file2\local\"
}

Any help would be much appreciated. 任何帮助将非常感激。 Thanks! 谢谢!

I would go the route Anand has shown to pass a path into your script. 我会选择Anand所示的路线,将路径传递到您的脚本中。 But to answer the question posed by your title, here's how you pass variable from C#. 但是要回答标题所提出的问题,这是如何从C#传递变量。 Well this is really how you set the variable in the PowerShell engine. 好的,这就是您在PowerShell引擎中设置变量的方式。

ps.Runspace.SessionStateProxy.SetVariable("Path", @"C:\Users\file1\file2\file3\");

Note: in C# for file paths you really want to use verbatim @ strings. 注意:在C#中,对于文件路径,您确实要使用逐字@字符串。

Update: based on your comments, try this: 更新:根据您的评论,尝试以下操作:

runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddScript(script, false); // Use false to tell PowerShell to run script in current 
                             // scope otherwise LocalCopy function won't be available
                             // later when we try to invoke it.
ps.Invoke();
ps.Commands.Clear();
ps.AddCommand("LocalCopy").AddArgument(FilePathVariable);
ps.Invoke();
ps.AddArgument("C:\Users\file1\file2\file3\");

you can use args to fetch the argument in powershell. 您可以使用args在powershell中获取参数。

$path = $args[0]

MSDN MSDN

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

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