Powershell 向参数添加天数的脚本
我需要帮助创建PowerShell脚本以使用户帐户过期,并在过期日期后30天内向Exchange添加外出邮件。以下是我目前的情况:Powershell 向参数添加天数的脚本,powershell,methods,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
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”的方法
如何创建方法或是否有更简单的方法?
readhost
返回字符串。字符串对象没有方法AddDays()
。您需要将$StartTime
设为DateTime
值
更改此项:
Param(
...
$StartTime = $(Read-Host "..."),
...
)
为此:
Param(
...
[DateTime]$StartTime = $(Read-Host "..."),
...
)
请注意,这只起作用,因为您要求的格式的输入字符串可以转换为
DateTime
。否则您就需要这样做。不久前,我为一些东西编写了一些令人惊讶的类似代码,我不记得这是否完全有效,我现在无法进行测试,但您至少应该能够了解如何解析输入并确保在这里做的一切都是有效的
为了更好地适应您的使用,我对其进行了一些更改,如果广告中的SamAccountName
与电子邮件Alias
相同,那么您只需指定Mailbox
和StartTime
,它将“自动发现”广告帐户
如果有任何问题,请让我知道——如果有问题,我会在明天回来工作时查看
Function Set-LeaverAccount {
Param(
[string]$MailBox = $null,
[string]$StartTime = $null,
[string]$UserName = $null,
[string]$ExternalMessage = "The person you are emailing has left the company, please contact admin@company.com for more details",
[string]$InternalMessage = $null,
[uint16]$OutOfOfficeDays = 30
)
#region Parse Mailbox
$MailBoxAlias = $MailBox
if (!(Get-Mailbox $MailBox -EA SilentlyContinue)){
do {
$Input = Read-Host "Email Alias"
$MailBox = Get-Mailbox $Input -EA SilentlyContinue
} while (!$MailBox)
Write-Host "Mailbox of $($MailBox.Name) Selected"
} else { $Mailbox = Get-Mailbox }
#endregion
#region Parse DateTime
if (![DateTime]::Parse($StartTime)){
do {
$Input = Read-Host "Date/Time of Expiration"
[DateTime]$StartTime = [datetime]::MaxValue
} while (![DateTime]::TryParse($Input,[ref]$StartTime))
Write-Host "Start Time Set To $($StartTime.ToString())"
}
$EndTime = $StartTime.AddDays($OutOfOfficeDays)
#endregion
#region Parse AD Username
if ($UserName -eq $null){
$UserName = $MailBoxAlias #If not set use alias as AD Name
}
if (!(Get-AdUser $UserName -EA SilentlyContinue)){
do {
$Input = Read-Host "AD Username"
$User = Get-AdUser $Input -EA SilentlyContinue
} while ($User.count -ne 1)
Write-Host "User $($User.Name) Selected"
} else {
$User = Get-AdUser $UserName
}
#endregion
if ($InternalMessage -eq $null) { $InternalMessage = $ExternalMessage }
Set-ADAccountExpiration -Identity $User -DateTime $StartTime
Set-MailboxAutoReplyConfiguration -Identity $Mailbox -AutoReplyState Scheduled -StartTime $StartTime -EndTime $EndTime -ExternalMessage $ExternalMessage -InternalMessage $InternalMessage
}
Set-LeaverAccount -MailBox "User01" -StartTime "12/02/16" -UserName "User_1" `
-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."
请重新格式化,使代码可读-如果在每行开头插入四个空格,就可以了。您正在将字符串读入$StartTime,需要首先将日期字符串解析为datetime类型,只需将
$startime转换为datetime对象,如下所示--
[datetime]$StartTime