简体   繁体   English

如何在我的 PowerShell 脚本中将日期转换为 system.datetime 类型?

[英]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.

相关问题 Get-Date无法将null转换为类型“ System.Datetime” - Get-Date cannot convert null to type “System.Datetime” 如何使用 powershell 将 system.string 转换为 system.DateTime 类型 - How to convert system.string to the type system.DateTime using powershell 无法将参数“开始”绑定到目标。 异常设置“开始”:“无法将 null 转换为键入“System.DateTime”。” - Cannot bind parameter 'Start' to the target. Exception setting "Start": "Cannot convert null to type "System.DateTime"." 无法将值“Mon, 02 Nov 2020 06:39:24 PST”转换为类型“System.DateTime” - Cannot convert value "Mon, 02 Nov 2020 06:39:24 PST" to type "System.DateTime" 06/14/2022 09:37:48”键入“System.DateTime”。错误:“字符串未被识别为有效的 DateTime - 06/14/2022 09:37:48" to type "System.DateTime". Error: "String was not recognized as a valid DateTime 在PowerShell中设置System.DateTime对象的时间 - Setting the time of a System.DateTime object in PowerShell 将 UFormat 应用于 System.DateTime 变量 - Apply UFormat to System.DateTime variable 在System.DateTime对象上操纵时间 - Manipulate time on System.DateTime object 在Powershell中datetime与System.DateTime和ConvertTo-Json - datetime vs System.DateTime and ConvertTo-Json in Powershell PowerShell-从System.DateTime到System.ConsoleColor的无效转换 - PowerShell - Invalid cast from System.DateTime to System.ConsoleColor
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM