Asked by:
Error in Power Shell Script, please help

General discussion
-
While running this power shell i am getting the error.
#import activedirecory module Import-Module ActiveDirectory #import csv file $csvpath = "C:\temp\expdate-list.csv" $users = Import-Csv -Path "$csvpath" -Encoding UTF8 $ou = "OU=DEMO,DC=demo01,DC=it-koehler,DC=com" Write-Host "following users will be changed: " -ForegroundColor Gray foreach($user in $users ) { $surname = ($user).Surname $givenname = ($user).GivenName $expdate = ($user).AccountExpirationDate #check if date is not empty if($expdate -ne "$null"){ get-aduser -Filter {(GivenName -eq $givenname) -and (Surname -eq $surname)} -SearchBase "$ou" | Set-ADUser -AccountExpirationDate $expdate Write-Host "expirationdate for user $givenname,$surname set to $expdate" -ForegroundColor Green } else { get-aduser -Filter {(GivenName -eq $givenname) -and (Surname -eq $surname)} -SearchBase "$ou" | Set-ADUser -AccountExpirationDate $null Write-Host "expirationdate deleted for: $givenname,$surname " -ForegroundColor Yellow } } #show overview Write-Host " " Write-Host "overview:" Get-Aduser -Filter * -SearchBase "$ou" -Properties AccountExpirationDate | Sort-Object surname | Select-Object Surname,Givenname,AccountExpirationDate
Error is : cannot bind parameter account expiration date cannot convert value. String was not recognized as valid date and time.
All replies
-
-
You must not add quotes around variables. Variables only need to be quoted wen they are merged into a string. Adding quotes breaks many things when you don't use them correctly.
The following syntax is wrong.
if($expdate -ne "$null"){
This is how to do this:
if($expdate){
This test the variable for null. If it is not null the result is "true".
\_(ツ)_/
-