用于向参数添加天数的脚本

时间:2017-01-26 15:33:55

标签: powershell methods

我需要协助创建PowerShell脚本以使用户帐户过期,并在到期日期后的30天内向Exchange添加外出邮件。以下是我到目前为止:

Param(
    $mailbox   = $(Read-Host "you must enter the first part of the email address"), 
    $StartTime = $(Read-Host "Please enter the start date and time of account expiration in MM/DD/YYYY HH:MM:SS"), 
    $Username  = $(Read-Host "please enter the AS400 username"),
    $EndTime   = $StartTime.AddDays(30)
)

Set-ADAccountExpiration -Identity $Username -DateTime $StartTime;
Set-MailboxAutoReplyConfiguration -Identity $mailbox -AutoReplyState Scheduled -StartTime $StartTime -EndTime $EndTime -ExternalMessage "The Person you are trying to contact is no longer employed by the Company" -InternalMessage "The Person you are trying to contact is no longer employed by Company XYZ for further assistance please contact the your local Store."
  

方法调用失败,因为[System.String]不包含名为“AddDays”的方法。

如何创建方法或有更简单的方法?

2 个答案:

答案 0 :(得分:0)

Read-Host返回一个字符串。字符串对象没有方法AddDays()。您需要将$StartTime设为DateTime值。

改变这个:

Param(
    ...
    $StartTime = $(Read-Host "..."),
    ...
)

进入这个:

Param(
    ...
    [DateTime]$StartTime = $(Read-Host "..."),
    ...
)

请注意,这仅适用,因为您要求的格式的输入字符串可以转换为DateTime。否则,您需要actually parse the string

答案 1 :(得分:0)

我有一些令人惊讶的相似的代码写了一段时间,我不记得这是否完全工作,我现在无法测试,但你应该至少能够了解如何解析输入确保一切有效在这里完成。

如果AD中的var byTotalViews = CalculateRanks(movieRankData, m => m.TotalViews.Value); var byTotalMoney = CalculateRanks(movieRankData, m => m.TotalMoney.Value); foreach (var movie in movieRankData) { movie.TotalViews.Rank = byTotalViews[movie]; movie.TotalMoney.Rank = byTotalMoney[movie]; } 与电子邮件SamAccountName相同,我已经更改了一下以适应您的使用,那么您只需要指定Alias和{ {1}}至少,它会“自动发现”AD帐户。

有任何问题请告诉我 - 如果出现问题,我明天会回来看看。

Mailbox