powershell 计算多个值的属性

powershell 计算多个值的属性

我正在编写 Outlook 库存 powershell 脚本..我想要检索:

  • 帐户显示名称
  • 帐户的电子邮件
  • 账户类型(POP3、IAMP 或 Exchange)
  • 交换模式(在线、缓存)

代码:

Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.Accounts |
   Select-Object DisplayName, SmtpAddress, UserName, AccountType, ExchangeConnectionMode |
   Sort-Object  -Property SmtpAddress | Format-Table

这可以完成工作,但如果我可以使用计算属性来显示值作为自定义友好名称,而不是数值,那就太酷了AccountTypeExchangeConnectionMode例如:

AccountType
0 = Exchange
1 = IMAP
2 = POP3
Else = numeric value (real value)

ExchangeConnectionMode
700 = Cached
800 = Online
Else = N/A (or whatever similar text, for non exchange accounts)

我之前已经使用过计算属性,但只使用数学计算和单个值(没有多个值)。我猜会需要“ foreach”或“ if”... 不知道怎么做 :(

有什么线索吗?

答案1

阅读并关注使用计算属性使用哈希表

使用外表OlExchangeConnectionMode枚举OlAccountType 枚举,下面的代码片段应该可以完成这项工作:

$AccntTypes = @{ '0' = 'Exchange'; 
                 '1' = 'IMAP';
                 '2' = 'POP3';
                 '3' = 'HTTP'; 
                 '4' = 'EAS';
                 '5' = 'unknown'}
$ExchConnModes = @{'0' = 'Exchange'; 
                   '800' = 'Online';
                   '700' = 'CachedConnectedFull';
                   '600' = 'CachedConnectedDrizzle'; 
  # (incomplete; please update from `OlExchangeConnectionMode` Enumeration)
                    }

Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.Accounts |
   Select-Object DisplayName, SmtpAddress, UserName, 
    @{Name="AccountType";
      Expression={$AccntTypes[[string]$_.AccountType]}} , 
    @{Name="ExchangeConnectionMode";
      Expression={$ExchConnModes[[string]$_.ExchangeConnectionMode]}} |
   Sort-Object  -Property SmtpAddress | Format-Table

答案2

太棒了!

最后我使用“开关”和您提供的有用信息解决了。

Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$namespace.Accounts | Select-Object DisplayName, SmtpAddress, UserName, @{name='AccountType(txt)';expression={
switch ($_.AccountType)
 {
    0 {"Exchange"}
    1 {"IMAP"}
    2 {"POP3"}
    3 {"HTTP"}
    4 {"EAS (Exchange ActiveSync) on mobile devices"}
    5 {"Unknown"}
 }
 }}, @{name='ExchangeConnectionMode(txt)';expression={
switch ($_.ExchangeConnectionMode)
 {
    0 {"N/A"}
    100 {"Offline (Online mode - Work offline selected)"}
    200 {"CachedOffline (Cache mode - Work offline selected)"}
    300 {"Disconnected (Lost Exchange connection)"}
    400 {"CachedDisconnected (Cache mode - Lost Exchange connection)"}
    500 {"CachedConnectedHeaders (Cache mode - Only headers are     downloaded)"}
    600 {"CachedConnectedDrizzle (Cache mode - First headers then full items are downloaded)"}
    700 {"CachedConnectedFull (Cached mode - Full items are downloaded)"}
    800 {"Online"}
 }
 }} | Sort-Object -Property SmtpAddress | Format-Table 

我将保存您的代码以供进一步使用;)非常感谢!!!

相关内容