[英]How to convert date to type system.datetime in my PowerShells cript?
I need to set -accountexpiration
for multiple users in Active Directory using data from my CSV table with PowerShell.我需要使用来自我的 CSV 表和 PowerShell 的数据为 Active Directory 中的多个用户设置-accountexpiration
。 I have so far tried to do it with the command for a single user:到目前为止,我已尝试使用单个用户的命令来执行此操作:
set-aduser -identity 'ABC' -accountexpirationdate 01/01/2020
which works fine, but when I import the csv and use the assigned variable with foreach, it says:这工作正常,但是当我导入 csv 并使用 foreach 分配的变量时,它说:
Cannot convert value '01/01/2020' to type "system.datetime. Error: Sting was not recognized as a valid DateTime."无法将值“01/01/2020”转换为类型“system.datetime。错误:Sting 未被识别为有效的 DateTime。”
Suppose my table is as follows:假设我的表如下:
samaccountname,edate ABCD,01/01/2019 EFGH,02/02/2020 samaccountname,edate ABCD,01/01/2019 EFGH,02/02/2020
And I use the following script:我使用以下脚本:
import-module activedirectory
$expirytable = import-csv 'c:\expire.csv'
foreach($user in $expirytable)
{
set-aduser -identity $expirytable.samaccountname -accountexpiration $expirytable.edate
}
I get:我得到:
Cannot convert value "01/01/2020" to type "system.Datetime".无法将值“01/01/2020”转换为类型“system.Datetime”。 Error: "String was not recognized as a valid DateTime."错误:“字符串未被识别为有效的日期时间。”
So here is what I was missing I figured out please compare my codes to find mistake.所以这就是我遗漏的我发现请比较我的代码以找出错误。
import-module activedirectory
$expirytable = import-csv 'c:\expire.csv'
foreach($user in $expirytable)
{
$username = $user.samaccountname
$edate = $user.edate
set-aduser -identity $username -accountexpiration $edate
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.