[英]Powershell string not recognized as valid DateTime
I got a code to convert Date as below:我得到了一个转换日期的代码,如下所示:
$DatePart = get-aduser -identity $AcountName -Properties * | Select-Object whenCreated
$DatePartSubs =$DatePart.whenCreated
$DatePartSubstring = $DatePartSubs.ToShortDateString()
$FinalDate = [datetime]::ParseExact($DatePartSubstring,'dd/MM/yyyy',$null)
But when i tried to format date i got below error但是当我尝试格式化日期时出现以下错误
Where variable "$DatePartSubstring" equals:其中变量“$DatePartSubstring”等于:
What i did wrong and how to fix it?我做错了什么以及如何解决?
It looks like you are trying to get the date part, without the time, as a date-time object, where the time has been set to midnight.看起来您正在尝试获取没有时间的日期部分作为日期时间 object,其中时间已设置为午夜。 Converting to a sting and back again is the long way around.转换成刺针然后再转回来是很长的路要走。 You might be better off to do the following:您最好执行以下操作:
$DatePart = get-aduser -identity $AcountName -Properties * | Select-Object whenCreated
$FinalDate = $DatePart.whencreated.Date
This should set final date to midnight of the day in question.这应该将最终日期设置为相关日期的午夜。 Note, however, that if you attempt to display $FinalDate on the console, Powershell will implicitly convert it to a string before displaying it.但是请注意,如果您尝试在控制台上显示 $FinalDate,Powershell 会在显示之前将其隐式转换为字符串。 In order to find out what it really is, do为了找出它到底是什么,做
$FinalDate.GetType()
PowerShell conveniently maps quite a lot of LDAP attributes to more friendly names and on some of the attributes it converts them to more usable object types. PowerShell 方便地将大量 LDAP 属性映射到更友好的名称,并且在某些属性上它将它们转换为更有用的 object 类型。 Especially on attributes that we would like to use as DateTime objects and that are stored in AD as Generalized-Time strings like this comes in handy.特别是对于我们希望用作 DateTime 对象并作为通用时间字符串存储在 AD 中的属性,这样会派上用场。
So in your case, I would do所以在你的情况下,我会做
$CreatedDate = Get-ADUser -Identity $AcountName -Properties Created | Select-Object -ExpandProperty Created
Below a table that shows you the most commonly used attributes on users and how PowerShell maps them:下面的表格显示了用户最常用的属性以及 PowerShell 如何映射它们:
Property财产 | Syntax句法 | R/RW读写 | LDAPDisplayName LDA显示名称 |
---|---|---|---|
AccountLockoutTime帐户锁定时间 | DateTime约会时间 | RW读写器 | lockoutTime, converted to local time lockoutTime,转换为本地时间 |
AccountNotDelegated账户未授权 | Boolean Boolean | RW读写器 | userAccountControl (bit mask 1048576) userAccountControl(位掩码 1048576) |
AllowReversiblePasswordEncryption允许可逆密码加密 | Boolean Boolean | RW读写器 | userAccountControl (bit mask 128) userAccountControl(位掩码 128) |
BadLogonCount登录次数错误 | Int32整数32 | R R | badPwdCount错误密码计数 |
CannotChangePassword无法更改密码 | Boolean Boolean | RW读写器 | nTSecurityDescriptor nT安全描述符 |
CanonicalName规范名称 | String细绳 | R R | canonicalName规范名称 |
Certificates证书 | ADCollection收藏 | RW读写器 | userCertificate用户证书 |
ChangePasswordAtLogon登录时更改密码 | Boolean Boolean | W W | If pwdLastSet = 0如果 pwdLastSet = 0 |
City城市 | String细绳 | RW读写器 | l升 |
CN中国 | String细绳 | R R | cn cn |
Company公司 | String细绳 | RW读写器 | company公司 |
Country国家 | String细绳 | RW读写器 | c (2 character abbreviation) c(2字缩写) |
Created已创建 | DateTime约会时间 | R R | whenCreated创建时间 |
Deleted已删除 | Boolean Boolean | R R | isDeleted已删除 |
Department部门 | String细绳 | RW读写器 | department部 |
Description描述 | String细绳 | RW读写器 | description描述 |
DisplayName显示名称 | String细绳 | RW读写器 | displayName显示名称 |
DistinguishedName专有名称 | String (DN)字符串 (DN) | R R | distinguishedName专有名称 |
Division分配 | String细绳 | RW读写器 | division分配 |
DoesNotRequirePreAuth不需要预授权 | Boolean Boolean | RW读写器 | userAccountControl (bit mask 4194304) userAccountControl(位掩码 4194304) |
EmailAddress电子邮件地址 | String细绳 | RW读写器 | mail邮件 |
EmployeeID员工ID | String细绳 | RW读写器 | employeeID员工ID |
EmployeeNumber员工编号 | String细绳 | RW读写器 | employeeNumber员工编号 |
Enabled启用 | Boolean Boolean | RW读写器 | userAccountControl (bit mask not 2) userAccountControl(位掩码不是 2) |
Fax传真 | String细绳 | RW读写器 | facsimileTelephoneNumber传真电话号码 |
GivenName给定的名称 | String细绳 | RW读写器 | givenName给定的名称 |
HomeDirectory主目录 | String细绳 | RW读写器 | homeDirectory主目录 |
HomedirRequired主目录必填 | Boolean Boolean | RW读写器 | userAccountControl (bit mask 8) userAccountControl(位掩码 8) |
HomeDrive家庭驱动器 | String细绳 | RW读写器 | homeDrive家庭驱动器 |
HomePage主页 | String细绳 | RW读写器 | wWWHomePage wWW主页 |
HomePhone家庭电话 | String细绳 | RW读写器 | homePhone家庭电话 |
Initials缩写 | String细绳 | RW读写器 | initials缩写 |
LastBadPasswordAttempt上次密码错误尝试 | DateTime约会时间 | R R | badPasswordTime, converted to local time badPasswordTime,转换为本地时间 |
LastKnownParent最后的亲人 | String (DN)字符串 (DN) | R R | lastKnownParent最后一个已知的父母 |
LastLogonDate最后登录日期 | DateTime约会时间 | R R | lastLogonTimeStamp, converted to local time lastLogonTimeStamp,转换为本地时间 |
LockedOut被锁在外面 | Boolean Boolean | RW读写器 | msDS-User-Account-Control-Computed (bit mask 16) msDS-用户-帐户-控制-计算(位掩码 16) |
LogonWorkstations登录工作站 | String细绳 | RW读写器 | userWorkstations用户工作站 |
Manager经理 | String (DN)字符串 (DN) | RW读写器 | manager经理 |
MemberOf成员 | ADCollection收藏 | R R | memberOf成员 |
MNSLogonAccount MNS登录帐号 | Boolean Boolean | RW读写器 | userAccountControl (bit mask 131072) userAccountControl(位掩码 131072) |
MobilePhone手机 | String细绳 | RW读写器 | mobile移动的 |
Modified修改的 | DateTime约会时间 | R R | whenChanged何时改变 |
Name姓名 | String细绳 | R R | cn (Relative Distinguished Name) cn(相对专有名称) |
ObjectCategory对象类别 | String细绳 | R R | objectCategory对象类别 |
ObjectClass对象类 | String细绳 | R R | objectClass, most specific value objectClass,最具体的值 |
ObjectGUID对象GUID | Guid向导 | R R | objectGUID converted to string objectGUID 转换为字符串 |
Office办公室 | String细绳 | RW读写器 | physicalDeliveryOfficeName物理交付办公室名称 |
OfficePhone办公室电话 | String细绳 | RW读写器 | telephoneNumber电话号码 |
Organization组织 | String细绳 | RW读写器 | o o |
OtherName其他名字 | String细绳 | RW读写器 | middleName中间名字 |
PasswordExpired密码过期 | Boolean Boolean | RW读写器 | msDS-User-Account-Control-Computed (bit mask 8388608) (see Note 1) msDS-User-Account-Control-Computed(位掩码 8388608)(见注释 1) |
PasswordLastSet密码上次设置 | DateTime约会时间 | RW读写器 | pwdLastSet, local time pwdLastSet,当地时间 |
PasswordNeverExpires密码永不过期 | Boolean Boolean | RW读写器 | userAccountControl (bit mask 65536) userAccountControl(位掩码 65536) |
PasswordNotRequired密码不需要 | Boolean Boolean | RW读写器 | userAccountControl (bit mask 32) userAccountControl(位掩码 32) |
POBox邮政信箱 | String细绳 | RW读写器 | postOfficeBox邮局信箱 |
PostalCode邮政编码 | String细绳 | RW读写器 | postalCode邮政编码 |
PrimaryGroup小学组 | String细绳 | R R | Group with primaryGroupToken使用 primaryGroupToken 分组 |
ProfilePath配置文件路径 | String细绳 | RW读写器 | profilePath配置文件路径 |
ProtectedFromAccidentalDeletion防止意外删除 | Boolean Boolean | RW读写器 | nTSecurityDescriptor nT安全描述符 |
SamAccountName山姆账户名 | String细绳 | RW读写器 | sAMAccountName sAM账户名 |
ScriptPath脚本路径 | String细绳 | RW读写器 | scriptPath脚本路径 |
ServicePrincipalNames服务主体名称 | ADCollection收藏 | RW读写器 | servicePrincipalName服务主体名称 |
SID标识符 | Sid席德 | R R | objectSID converted to string objectSID 转换为字符串 |
SIDHistory SID历史 | ADCollection收藏 | R R | sIDHistory sID历史 |
SmartcardLogonRequired智能卡登录需要 | Boolean Boolean | RW读写器 | userAccountControl (bit mask 262144) userAccountControl(位掩码 262144) |
State State | String细绳 | RW读写器 | st英石 |
StreetAddress街道地址 | String细绳 | RW读写器 | streetAddress街道地址 |
Surname姓 | String细绳 | RW读写器 | sn锡 |
Title标题 | String细绳 | RW读写器 | title标题 |
TrustedForDelegation信任委托 | Boolean Boolean | RW读写器 | userAccountControl (bit mask 524288) userAccountControl(位掩码 524288) |
TrustedToAuthForDelegation TrustedToAuthForDelegation | Boolean Boolean | RW读写器 | userAccountControl (bit mask 16777216) userAccountControl(位掩码 16777216) |
UseDESKeyOnly仅使用 DES 密钥 | Boolean Boolean | RW读写器 | userAccountControl (bit mask 2097152) userAccountControl(位掩码 2097152) |
UserPrincipalName用户主体名称 | String细绳 | RW读写器 | userPrincipalName用户主体名称 |
Get-ADUser by default returns objects with these properties: DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
.默认情况下,Get-ADUser 返回具有以下属性的对象: DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
。 If you want more, then you need to specify the extra's with the -Properties
parameter.如果您想要更多,则需要使用-Properties
参数指定额外的。 Best not use -Properties *
最好不要使用-Properties *
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.