2013-04-02 24 views
0

我有一個名爲「獲取-連接」功能,它返回一些SQL連接的列表,例如:這個PowerShell Foreach-Object語句有什麼問題?

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 "2688f2af-1b49-405f-aa92-417a43b76dca" 

現在我tryed使用

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

Get-Connections的返回類型是什麼?一個列表或數組? – Jackie

+0

或者更具體地說,'Get-Connections'返回的對象的'Id'屬性的類型是什麼?你有沒有嘗試過'Get-Connections | %{Remove-Connection $ _。Id.ToString()}'? – alroc

+0

Get-Connections的類型是ConnectionDTO的列表。 ConnectionDTO的Id屬性是一個GUID,Remove-Connection也需要一個GUID。 您的示例也不起作用:Remove-Connection:無法綁定參數「Id」。無法將值「System.Object []」轉換爲鍵入「System.Guid」。錯誤:?「GUID應該包含32個 數字與4個短線 –

回答

0

我終於找到了答案。我必須在括號中設置Get-Connections命令:

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