简体   繁体   English

Powershell 帐户到期日期脚本输出格式错误

[英]Powershell script for Account Expiration date outputs wrong format

We have a script which emails managers when an AD account is within 14 days of expiring, but the displayed date format is in mmddyyy and for the life of me i can't get it to display ddmmyyy我们有一个脚本,当 AD 帐户在 14 天内过期时,它会向管理员发送电子邮件,但显示的日期格式是 mmddyyy 并且对于我来说,我无法让它显示 ddmmyyy

$From = "xxxx"
$CC = "xxxx", "xxxx"
#$BCC = "xxxx"
$SMTPServer = "xxxx"
$startDate = Get-Date
$endDate = $startDate.AddDays(14)
$Users = Get-ADUser -Filter {AccountExpirationDate -gt $startDate -and AccountExpirationDate -lt $endDate} -Properties AccountExpirationDate, Manager
Foreach($User in $Users)
{
$Manager = Get-ADUser $User.Manager -Properties EmailAddress
#Set our dynamic variables
$To = $Manager.EmailAddress
$Subject = "Account Expiration Notification for " + $User.Name
$Body = 
"Hi $($Manager.Name),
This notification is to inform you that the account for $($User.Name) will expire on $($User.AccountExpirationDate) 
From this date the account will not be able to access any ICT services and will be deleted one month following expiry.
If you need to extend the account for this member of your team, please raise a Service Desk ticket via xxxx giving details of the extension needed and new date of expiry.
It is important to take action now to avoid any unnecessary disruption for your team member.
Thank you,
ICT Help Desk"
Send-MailMessage -To xxxx -cc xxxx -From $From -Subject $Subject -SmtpServer xxxx -Body $Body
}

You can format any [datetime] value however you like, by either passing an appropriate format string to the ToString() method on the object, or by using the -f string format operator (which will then do the same for you):您可以根据需要格式化任何[datetime]值,方法是将适当的格式字符串传递给 object 上的ToString()方法,或使用-f字符串格式运算符(然后将为您执行相同的操作):

$body = " ... $('{0:ddMMyyy}' -f $User.AccountExpirationDate) ... "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM