这个powershell Foreach-Object声明有什么问题?

时间:2013-04-02 14:29:19

标签: powershell

我有一个名为“Get-Connections”的函数,它返回一些sql连接的列表,例如:

PS C:\Windows\system32> Get-Connections

Id                    : e1a2fd17-91aa-4975-b1ee-1a7df65e6d6b
DataSource            : myDataSource1
InitialCatalog        : myInitCatalog
UseIntegratedSecurity : True
DisplayName           : testConnection

Id                    : 2688f2af-1b49-405f-aa92-417a43b76dca
DataSource            : myDataSource1
InitialCatalog        : myInitCatalog
UseIntegratedSecurity : True
DisplayName           : testConnection

我还有一个名为“Remove-connection”的函数,它通过Id删除连接:

Remove-Connection "2688f2af-1b49-405f-aa92-417a43b76dca"

现在我尝试使用

删除所有连接
Get-Connections | % { Remove-Connection $_.Id }

这不起作用,例外是:

Remove-Connection : Cannot convert 'System.Object[]' to the type 'System.Guid' required by parameter 'Id'. Specified method is not supported.

这实际上是有效的:

Get-Connections | % { $_.Id } | % { Remove-Connection "$_" }

之前的陈述有什么问题?

更新1。

获取连接:

[Cmdlet(VerbsCommon.Get, "Connections", SupportsShouldProcess = true)]
    public class GetConnectionsCommand : Cmdlet
    {
        private List<ConnectionDto> Connections { get; set; }

        public void GetConnections()
        {
            var binding = new BasicHttpBinding();
            var address = new EndpointAddress(Address);

            var repositoryService = new WcfConnectionRepositoryServiceProxy(binding, address);

            Connections = repositoryService.GetAll().ToList();
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            GetConnections();
        }

        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            WriteObject(Connections);
        }
    }

删除连接:

 [Cmdlet(VerbsCommon.Remove, "Connection", SupportsShouldProcess = true)]
        public class RemoveConnectionCommand : Cmdlet
        {
            [Parameter(Position = 0, ParameterSetName = "Id", 
                Mandatory = true, ValueFromPipeline = true, 
                 ValueFromPipelineByPropertyName = true,
                HelpMessage = "Please enter the ID of the connection to remove")]
            [ValidateNotNullOrEmpty]
            public Guid Id { get; set; }

            private void RemoveConnection()
            {
                var binding = new BasicHttpBinding();
                var address = new EndpointAddress(Address);

                var repositoryService = new WcfConnectionRepositoryServiceProxy(binding, address);
                repositoryService.Delete(Id);
            }

            protected override void BeginProcessing()
            {
                base.BeginProcessing();
                RemoveConnection();
            }
        }

DTO:

[DataContract(Name = "ConnectionDto"]
    public class ConnectionDto
    {
        [DataMember]
        public Guid Id { get; set; }

        [DataMember]
        public string DataSource { get; set; }

        ...

1 个答案:

答案 0 :(得分:0)

我终于找到了答案。我必须在括号中设置Get-Connections命令:

(Get-Connections) | % { Remove-Connection $_.Id }