2016-06-08 15 views
0

這是我來回答最接近:新ADUser便有在PowerShell中,帳戶過期加一天

Set-ADAccountExpiration in PowerShell sets AD account -1 day. Why?

然而,這並不在我的情況下工作。我使用Active Directory中的PowerShell創建新用戶。帳戶過期從一個CSV文件拉到這樣的:

$ExpireDate = $Line.'Contract Expiration' 
    if ($ExpireDate -eq "") {$AccountExpiryDate = [DateTime]::Now.AddYears(1)} 
    else {$AccountExpiryDate = $ExpireDate} 

然後加入到新-ADUser便有:

New-ADUser -Path $TestOU -SamAccountName $SamAccountName -AccountExpirationDate $AccountExpiryDate ` 

它拉就好了日期,但需要爲下一個一天(我明白上午12點的套頭衫)。當我嘗試這樣:

else {$AccountExpiryDate = ([DateTime]($Line.'Contract Expiration')).AddDays(1)} 

它拉動原來的日期,就好像AddDays(1)沒有附加。有任何想法嗎?


發現的解決方案(感謝奧斯汀法國)

$ExpireDate = $Line.'Contract Expiration' 
    $nDate = [DateTime]::Parse($ExpireDate) 
    if ($ExpireDate -eq "") {$AccountExpiryDate = [DateTime]::Now.AddYears(1)} 
    else {$AccountExpiryDate = $nDate.AddDays(1)} 
+0

試着解析日期?例如:'$ nDate = [DateTime] :: Parse($ string); $ nDate.AddDays(1)' –

+0

甜,另一個說明:一個嘗試/捕獲將是一個偉大的想法,以防日期非常畸形(因此整個腳本不會死!) –

回答

1

我懷疑是在列中的日期不被正確轉換爲DateTime對象。使用DateTime.Parse方法將有助於驗證對象並正確地轉換字符串。

在你的情況下,嘗試以下操作:

$ newDateTime = [DATETIME] ::解析($ Line.'Contract過期') $ AccountExpiryDate = $ newDate.AddDays(1)

+0

這就是它。添加工作解決方案到原始帖子。 –

相關問題