Powershell哈希表作为C#中自定义cmdlet的参数 [英] Powershell hashtables as argument to custom cmdlet in C#

查看:87
本文介绍了Powershell哈希表作为C#中自定义cmdlet的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在PS(2.0)中使用自定义类:

Ive been using a custom class for some time like this in PS (2.0):

import-module .\MyClassLib.dll

$task = New-Object MyClassLib.OracleScript -Property @{
                                                            Files="MyScript.sql" 
                                                            Database="TEST"
                                                            User="USER" 
                                                            Password="PASSWORD"  
                                                        }
$result = $task.Execute()

这很好用.

但是我想用C#创建一个CmdLet来代替工作.因此,在创建了cmdlet之后,我认为我可以执行以下操作之一:

However i wanted to create a CmdLet in C# to do the work instead. So after creating the cmdlet i thought I could do one of the following:

Invoke-OracleScript @{
                            Files="Script.sql" 
                            Database="db"
                            User="user" 
                            Password="password"  
                           }

Invoke-OracleScript @{
                            Files="Script.sql"; 
                            Database="db";
                            User="user"; 
                            Password="password";  
                           }

Invoke-OracleScript -Property @{
                            Files="Script.sql"
                            Database="db"
                            User="user"
                            Password="password"  
                           }

但是没有运气. :(.

But no luck. :(.

我不断收到类似这样的错误:

I keep getting errors like:

  • 找不到文件System.Collections.Hashtable(它认为整个哈希表是Files参数)
  • 找不到与参数名称"Property"匹配的参数
  • 还有另外两个.

我的课:

[Cmdlet(VerbsLifecycle.Invoke, "OracleScript", ConfirmImpact = ConfirmImpact.High, SupportsShouldProcess = true, SupportsTransactions = false)]
public class Invoke_OracleScript : Cmdlet, IOracleScript
{
    [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
    public string Files { get; set; }

    [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
    public string Database { get; set; }
    ....

如果我改用这样的参数:-Files" -Database",则效果很好,但是所有内容必须在1行上,这对读取非常不利.因此,哈希表确实是我最大的愿望:).

If I instead use parameters like this: -Files "" -Database "", it works just fine, but everything has to be on 1 line which is very bad for reading. So the hashtable is really my biggest wish :).

有人可以向我解释我在这里缺少什么吗? (ParameterSets ?,我在文档中找不到的秘密属性,其他)

Can anyone explain to me what im missing here? (ParameterSets?, a secret attribute i've been unable to find in the docs, other)

亲切的问候

推荐答案

如果您要做的只是将cmdlet调用放在多行中,则可以使用backtick(`)将命令扩展到下一行:/p>

If all you care is to make the cmdlet call to be in multiple lines, you can use backtick (`) to extend the command to the next line:

get-process -Name notepad `
            -Computername localhost `
            -Verbose

或者,您可以创建自定义对象:

Or, you can create custom object:

$process = new-object psobject
$process | add-member -name name -value notepad -type noteproperty
$process | add-member -name computername -value localhost -type noteproperty
$process | get-process

或者,你在做什么:

$process = new-object psobject -property @{ name="notepad";
                                            computername = "localhost";}

$process | get-process

我认为哈希表作为对象是v3.0(当前在CTP中)添加的功能

I think hashtable as objects was a feature that was added in v3.0 ( currently in CTP)

这篇关于Powershell哈希表作为C#中自定义cmdlet的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆