Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中获取此Powershell输出的正确方法是什么#_C#_Powershell_Powershell Module_Powershell Sdk - Fatal编程技术网

C# 在C中获取此Powershell输出的正确方法是什么#

C# 在C中获取此Powershell输出的正确方法是什么#,c#,powershell,powershell-module,powershell-sdk,C#,Powershell,Powershell Module,Powershell Sdk,我没有得到下面的任何错误,但我也没有得到输出。下面是Powershell cmd和调用它的C#方法。我想知道它是否编写正确,以及如何从powershell获得输出。当我从PowerShell窗口运行时,它工作正常 Pwsh命令: public class GetRowAndPartitionKeys : Cmdlet { [Parameter(Mandatory = false)] public List<string> Properties

我没有得到下面的任何错误,但我也没有得到输出。下面是Powershell cmd和调用它的C#方法。我想知道它是否编写正确,以及如何从powershell获得输出。当我从PowerShell窗口运行时,它工作正常

Pwsh命令:

public class GetRowAndPartitionKeys : Cmdlet
    {
        [Parameter(Mandatory = false)]
        public List<string> Properties { get; set; } = new List<string>();
    }

    [Cmdlet( VerbsCommon.Get, "RowAndPartitionKeys" )]
    public class GetRowAndPartitionKeyCmd : GetRowAndPartitionKeys

    {
        protected override void ProcessRecord()
        {            
            WriteObject ("Hi");
        }
    }

}
公共类GetRowAndPartitionKeys:Cmdlet
{
[参数(强制=假)]
公共列表属性{get;set;}=new List();
}
[Cmdlet(VerbsCommon.Get,“RowAndPartitionKeys”)]
公共类GetRowAndPartitionKeyCmd:GetRowAndPartitionKeys
{
受保护的覆盖无效ProcessRecord()
{            
书面对象(“Hi”);
}
}
}
C#方法:

公共异步任务运行脚本()
{
//使用默认运行空间创建新的托管PowerShell实例。
//包装在using语句中,以确保资源已清理。
string scriptContents=“导入模块”C:\Users\…\Powershell.dll”;
使用(PowerShell ps=PowerShell.Create())
{
//指定要运行的脚本代码。
ps.AddScript(scriptContents).AddCommand(“获取行和分区键”);
//执行脚本并等待结果。
var pipelineObjects=await ps.InvokeAsync().ConfigureAwait(false);
foreach(pipelineObjects中的变量项)
{
Console.WriteLine(item.BaseObject.ToString());
}
返回pipelineObjects;
}
与类似,以下自包含示例代码表明,在纠正代码中的以下问题后,该方法原则上是有效的:

  • .AddScript()
    .AddCommand()
    调用之间缺少
    .AddStatement()
    调用;这是将(基于脚本块的)
    导入模块调用和
    获取行和分区键
    调用视为单独语句所必需的

  • 伪代码行
    string scriptContents=“导入模块”C:\Users\…\Powershell.dll”
    缺少一个结束
    (可能只是在此处发布的工件)

  • 此外,下面还添加了故障排除代码

当编排代码在PowerShell中时,通过
编译的实际C#项目将与PowerShell(核心)SDK包的版本7.1.2结合使用

运行创建和运行测试项目的代码后,您可以自己检查和试验这些项目。
/module
是定义
Get RowAndPartitionKeys
cmdlet的模块DLL的项目,
/app
是调用它的应用程序的项目):

$tmpDir=(新项-Force-Type目录(连接路径temp:$PID)).FullName
$tmpModuleDir=(新项-Force-Type目录(连接路径$tmpDir模块)).FullName
$tmpAppDir=(新项-Force-Type目录(连接路径$tmpDir-app)).FullName
$tmpPublishDir=(新项-Force-Type目录(连接路径$tmpDir publish)).FullName
$tmpModuleDll=连接路径$tmpPublishDir module.dll
推送位置
# ---
编写Verbose-vb“使用示例cmdlet创建模块DLL…”
设置位置$tmpModuleDir
dotnet新类库--强制>$null | |$(退出$LASTEXITCODE)
dotnet添加包Microsoft.PowerShell.SDK>$null | |$(退出$LASTEXITCODE)
@'
使用制度;
使用System.Collections.Generic;
使用系统、管理、自动化;
名称空间演示{
公共类GetRowAndPartitionKeys:Cmdlet
{
公共列表属性{get;set;}
}
[Cmdlet(VerbsCommon.Get,“RowAndPartitionKeys”)]
公共类GetRowAndPartitionKeyCmd:GetRowAndPartitionKeys
{
受保护的覆盖无效ProcessRecord()
{
书面对象(“Hi”);
}
}
}
“@|设置内容Class1.cs
dotnet publish-o$tmpppublishdir>$null | |$(退出$LASTEXITCODE)
# ---
编写Verbose-vb“创建导入模块DLL并调用示例cmdlet的控制台应用程序…”
设置位置$tmpAppDir
dotnet新控制台--强制>$null | |$(退出$LASTEXITCODE)
dotnet添加包Microsoft.PowerShell.SDK>$null | |$(退出$LASTEXITCODE)
@"
使用制度;
使用System.Collections.Generic;
使用系统、管理、自动化;
使用System.Threading.Tasks;
名称空间演示{
公共静态类应用程序{
静态void Main(字符串[]参数)
{
var unused=new Foo().RunScript().Result;
}
}    
公开课Foo{
公共异步任务RunScript()
{
字符串scriptContents=@“导入模块-详细”$tmpModuleDll“;
使用(PowerShell ps=PowerShell.Create())
{
ps.AddScript(scriptContents.AddStatement().AddCommand(“获取行和分区键”);
var pipelineObjects=await ps.InvokeAsync().ConfigureAwait(false);
//---故障排除代码
//打印导入模块调用的详细输出
foreach(ps.Streams.Verbose中的var v){Console.WriteLine(“Verbose:+v.ToString());}
//打印任何错误。
foreach(ps.Streams.Error中的变量e){Console.WriteLine(“错误:+e.ToString());}
// ---                
foreach(pipelineObjects中的变量项)
{
Console.WriteLine(item.BaseObject.ToString());
}
返回pipelineObjects;
}
}
}
}
“@| Set Content Program.cs
# ---
编写Verbose-vb“编译和调用控制台应用程序…”
网络运行
流行位置
写详细的-vb@“
测试项目位于$tmpDir中。
要清理,请运行:
删除项目“$tmpdi”
 public async Task<IEnumerable<object>> RunScript(  )
        {
            // create a new hosted PowerShell instance using the default runspace.
            // wrap in a using statement to ensure resources are cleaned up.
           string scriptContents = "Import-Module 'C:\Users\...\Powershell.dll";
            using( PowerShell ps = PowerShell.Create() )
            {
                // specify the script code to run.
            ps.AddScript( scriptContents ).AddCommand( "Get-RowAndPartitionKeys" );


            // execute the script and await the result.
            var pipelineObjects = await ps.InvokeAsync().ConfigureAwait( false );
           
            foreach( var item in pipelineObjects )
            {
                Console.WriteLine( item.BaseObject.ToString() );
            }
            return pipelineObjects;
        }
$tmpDir = (New-Item -Force -Type Directory (Join-Path temp: $PID)).FullName

$tmpModuleDir = (New-Item -Force -Type Directory (Join-Path $tmpDir module)).FullName
$tmpAppDir = (New-Item -Force -Type Directory (Join-Path $tmpDir app)).FullName
$tmpPublishDir = (New-Item -Force -Type Directory (Join-Path $tmpDir publish)).FullName

$tmpModuleDll = Join-Path $tmpPublishDir module.dll

Push-Location

# ---

Write-Verbose -vb "Creating module DLL with sample cmdlet..."

Set-Location $tmpModuleDir

dotnet new classlib --force >$null || $(exit $LASTEXITCODE)

dotnet add package Microsoft.PowerShell.SDK >$null || $(exit $LASTEXITCODE)

@'
using System;
using System.Collections.Generic;
using System.Management.Automation;

namespace demo {
    public class GetRowAndPartitionKeys : Cmdlet 
    {
      public List<string> Properties { get; set; }
    }
    
    [Cmdlet( VerbsCommon.Get, "RowAndPartitionKeys" )]
    public class GetRowAndPartitionKeyCmd : GetRowAndPartitionKeys
    {
      protected override void ProcessRecord()
      {
          WriteObject ("Hi");
      }
    }
}
'@ | Set-Content Class1.cs

dotnet publish -o $tmpPublishDir >$null || $(exit $LASTEXITCODE)

# ---

Write-Verbose -vb "Creating console application that imports the module DLL and calls the sample cmdlet..."
Set-Location $tmpAppDir

dotnet new console --force >$null  || $(exit $LASTEXITCODE)

dotnet add package Microsoft.PowerShell.SDK >$null || $(exit $LASTEXITCODE)

@"
using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Threading.Tasks;

namespace demo {

    public static class App {
        static void Main(string[] args)
        {
            var unused = new Foo().RunScript().Result;
        }
    }    

    public class Foo {
        public async Task<IEnumerable<object>> RunScript()
        {
            string scriptContents = @"Import-Module -Verbose ""$tmpModuleDll""";
            using(PowerShell ps = PowerShell.Create())
            {
                ps.AddScript(scriptContents).AddStatement().AddCommand("Get-RowAndPartitionKeys");
                var pipelineObjects = await ps.InvokeAsync().ConfigureAwait( false );
                
                // --- TROUBLESHOOTING CODE

                // Print verbose output from the Import-Module call
                foreach (var v in ps.Streams.Verbose) { Console.WriteLine("VERBOSE: " + v.ToString()); }
        
                // Print any errors.
                foreach (var e in ps.Streams.Error) { Console.WriteLine("ERROR: " + e.ToString()); }
        
                // ---                

                foreach (var item in pipelineObjects)
                {
                    Console.WriteLine(item.BaseObject.ToString());
                }

                return pipelineObjects;
            }
        }
    }
}
"@ | Set-Content Program.cs

# ---

Write-Verbose -vb "Compiling and invoking the console application..."
dotnet run 

Pop-Location

Write-Verbose -vb @"
The test projects are located in $tmpDir.
To clean up, run:

   Remove-Item "$tmpdir" -Recurse
 
"@