RSS

how to 将多个不同命令的输出同时显示

02 Dec

需求:将get-mailbox 和Get-MailboxStatistics的输出组合起来

   1: Get-MailboxStatistics yangxun | fl displayname,totalitemsize 
   2:  
   3: DisplayName   : Xun Yang (MS)
   4: TotalItemSize : 154352990B 
   5:  
   6: get-mailbox yangxun | fl displayname,distinguishedname 
   7:  
   8: DisplayName       : Xun Yang (MS)
   9: DistinguishedName : CN=Xun Yang,OU=MS Team,DC=ms,DC=ad 

需要同时输出DisplayName,DistinguishedName,TotalItemSize

步骤:

针对1个对象:

New-Object psobject | Add-Member -PassThru NoteProperty displayname (get-mailbox yangxun).displayname |    Add-Member -PassThru NoteProperty TotalItemSize  (Get-MailboxStatistics yangxun).TotalItemSize |    Add-Member -PassThru NoteProperty DistinguishedName (get-mailbox yangxun).DistinguishedName | fl

   1: displayname       : Xun Yang (MS)
   2: TotalItemSize     : 154362463B
   3: DistinguishedName : CN=Xun Yang,OU=MS Team,DC=ms,DC=ad 

方法02

get-user yangxun | select name,@{name="MailboxSize";expression={ (Get-MailboxStatistics $_.name).totalitemsize }},@{name="DN";expression={(get-mailbox $_.name).distinguishedname}} | fl

   1: Name        : Xun Yang
   2: MailboxSize : 157923884B
   3: DN          : CN=Xun Yang,OU=MS Team,DC=ms,DC=ad

———————

批量处理呢?

方法01

Get-User -ResultSize Unlimited | Where { $_.RecipientType -eq ‘UserMailbox’ } | ForEach { $Users = @{} } { $Users[$_.SamAccountName] = $_ }

Get-Mailbox -ResultSize Unlimited |
  ForEach {
    New-Object psobject |
    Add-Member -PassThru NoteProperty displayname $_.displayname |
    Add-Member -PassThru NoteProperty DistinguishedName $Users[$_.SamAccountName]|
    Add-Member -PassThru NoteProperty TotalItemSize (Get-MailboxStatistics $_.name).TotalItemSize
  }

—–

   1: Get-User yangxun* | Where { $_.RecipientType -eq 'UserMailbox' } | ForEach { $Users = @{} } { $Users[$_.SamAccountName] = $_ }
   2: Get-Mailbox yangxun* -ResultSize Unlimited |
   3: >>   ForEach {
   4: >>     New-Object psobject |
   5: >>     Add-Member -PassThru NoteProperty displayname $_.displayname |
   6: >>     Add-Member -PassThru NoteProperty DistinguishedName $Users[$_.SamAccountN
   7: ame]|
   8: >>     Add-Member -PassThru NoteProperty TotalItemSize (Get-MailboxStatistics $_
   9: .name).TotalItemSize
  10: >>   }
  11: >> 
  12:  
  13: displayname                DistinguishedName          TotalItemSize
  14: -----------                -----------------          -------------
  15: 杨峋                       ms.ad/MS Team/... 3232170B
  16: 杨迅                       ms.ad/MS Team/... 3232142B
  17: Xun Yang (MS)              ms.ad/MS Team/... 154464004B 
  18:  

方法02

get-user yangxun* | select name,@{name="MailboxSize";expression={ (Get-MailboxStatistics $_.name).totalitemsize }},@{name="DN";expression={(get-mailbox $_.name).distinguishedname}} | fl

   1: Name        : yangxun1
   2: MailboxSize : 3232170B
   3: DN          : CN=yangxun1,OU=MS Team,DC=ms,DC=ad
   4:  
   5: Name        : yangxun2
   6: MailboxSize : 3232142B
   7: DN          : CN=yangxun2,OU=MS Team,DC=ms,DC=ad
   8:  
   9: Name        : Xun Yang
  10: MailboxSize : 158027212B
  11: DN          : CN=Xun Yang,OU=MS Team,DC=ms,DC=ad

如何?还算很cool么?

 

不过还是有所疑问,为何给数组赋予get-user yangxun的返回值,却仅得到一个DN呢?get-user yangxun返回的值不是一堆属性么?

   1: [PS] C:\>$a["yangxx"] = get-user yangxun
   2: [PS] C:\>$a 
   3:  
   4: Name                           Value
   5: ----                           -----
   6: yangxx                         ms.ad/MS Team/Xun Yang
   7: yangx                          bbbbb
   8: 1                              aaaaa
   9: 0                              bbbbb
Advertisements
 
1 Comment

Posted by on December 2, 2009 in Powershell

 

One response to “how to 将多个不同命令的输出同时显示

  1. December 2, 2009 at 17:40

    把这段代码打包成一个function吧,或者做成代码块应该都可以解决问题

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: