Powershell在7天内返回邮箱并驻留在指定的服务器上。

时间:2016-05-18 21:11:56

标签: powershell scripting exchange-server-2010 powershell-v5.0

我需要在7天内创建的邮箱上自动应用Exchange 2010保留策略。但我还需要排除任何非美国服务器,因为这些服务器由海外IT部门管理。

这是我的脚本的前言,允许在没有任何人为干预的情况下进行交换连接。我可以在Windows中安排这个。

If (Test-Path C:\temp\mycred.xml) {$UserCredential = Import-CliXML C:\temp\mycred.xml}
else{
Get-Credential | Export-CliXml C:\temp\mycred.xml
$UserCredential = Import-CliXML C:\temp\mycred.xml}

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://munprdcasht04.exchange.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential
import-PSSession $Session

这是我目前用于返回7天内创建的邮箱的代码

Get-Mailbox -ResultSize Unlimited| Where-Object {
($_.WhenCreated –ge ((Get-Date).Adddays(-7)))} | 
ft -auto Name,WhenCreated,Retentionpolicy,servername

这对我有用但是当我添加一个运算符来限制特定服务器上的邮箱时,命令完成但不打印任何结果,所以我假设有0个匹配的记录。

Get-Mailbox -ResultSize Unlimited| Where-Object {
   ($_.WhenCreated –ge ((Get-Date).Adddays(-7))) -and
   ($_.ServerName -contains "munprdmbxa") |
   ft -auto Name,WhenCreated,Retentionpolicy,servername

我还没有考虑实际启用保留政策,因为我只想在潜入冒险之前返回我的目标数据。我感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

仅当字符串完全匹配时,powershell中的

-contains才会返回True。它在单个字符串上更像-eq,但也能够确定元素是否包含在集合中。

e.g

$test = "aaa"
$test -contains "aaa"

$test = "aaa","bbb","ccc"
$test -contains "aaa"

上面的两个将返回True,但是,对于子字符串它不会返回true,并且外卡不能使用它。

$test = "aaa.domain.com"
$test -contains "aaa"

$test = "aaa.domain.com"
$test -contains "*aaa*"

会返回False

因此,如果服务器名称是带有域后缀等的FQDN,则该字符串将不是完全匹配。如果改为使用-like操作,则可以将给定字符串中的字符串子集与通配符匹配:

e.g:

  $test = "aaa.domain.com"
  $test -like "*aa*"

  $test = "aaa.domain.com"
  $test -like "aa*.com"

两者都会返回True

因此,使用-like运算符更有可能避免误报结果。

相关问题