Powershell 返回在 7 天内创建并驻留在指定服务器上的邮箱. [英] Powershell return mailboxes created within 7 days and resides on specified server.

查看:51
本文介绍了Powershell 返回在 7 天内创建并驻留在指定服务器上的邮箱.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to automate applying exchange 2010 retention policy on mailboxes that have been created within 7 days. But i also need to exclude any non-US based servers as these are managed by IT overseas.

这是我的脚本的前言,它允许在没有任何人工交互的情况下连接到交换.这样我就可以在 Windows 中安排它.

This is the preface to my script that allows for connection to exchange without any human interaction. Tis way i can schedule this in 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天内创建的邮箱的代码

This is my current code to return mailboxes created within 7 days

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

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

This works for me but when i add a operator to limit mailboxes on specific servers, the command completes but does not print any results so I'm assuming there are 0 matching records.

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

我还没有真正考虑启用保留策略,因为我只是想在潜入冒险之前返回我的目标数据.我很感激任何帮助.

I haven't yet looked into actually enabling the retention policy as i am just trying to return my target data prior to diving into that adventure. I appreciate any help.

推荐答案

-contains 在 powershell 中,仅当字符串完全匹配时才返回 True.它的作用更像是在单个字符串上的 -eq,但也有能力确定一个元素是否包含在一个集合中.

-contains in powershell returns True only when a string is exact matched. It acts more like -eq on a single string but also has ability to determine if an element is contained in a set.

例如

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

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

以上两个将返回True,但是,它不会为子字符串返回true,并且通配符不适用于它.

The two above would return True, however, it would not return true for a sub-string and wild card doesn't work with it.

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

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

会返回 False

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

Thus if the server name is FQDN with domain suffix etc, the string would not be an exact match. If you use -like operation instead, you can match a subset of string within the given string with wildcard:

例如:

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

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

都将返回 True

因此使用 -like 运算符更有可能避免假阴性结果.

Thus using -like operator would more likely to avoid false negative results.

这篇关于Powershell 返回在 7 天内创建并驻留在指定服务器上的邮箱.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆