0

たとえば、いくつかの SQL 接続のリストを返す「Get-Connections」という関数があります。

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

Id で接続を削除する「Remove-connection」という関数もあります。

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; }
        
        ...
4

1 に答える 1