I have a random Date Generator, but I am now trying to add this date to the desctiption of each user on the domain. As of now the only other thing in the Description is their age which is just a number. So I want it to be: Description: age, date hired
When I run this code it asks for a filter I then put in a user name and I get an error with Get-ADUser -Prop description.
Is this going through all users? or Am I using the wrong filter?
syntax error: Get-ADUser -Prop Description
$StartDate = Get-Date -Date 2015-01-01
$EndDate = Get-Date -Date 2022-11-11
$RangeInDays = 0..(($EndDate - $StartDate).Days)
$DaysToAdd = Get-Random -InputObjects $RangeInDays
$RandDate = $Startdate.AddDays($DaysToAdd)
Get-ADUser -Prop Description | ForEach {
$desc = $._description $RandDate Set-ADUser $_.sAMAccountName -Description $desc
}
CodePudding user response:
Continuing from my comments,
- your code does not include a
-Filter
on the Get-ADUser cmdlet $._description
is wrong syntax and should be$_.Description
Get-Random -InputObjects $RangeInDays
is wrong syntax and should be
Get-Random -InputObject $RangeInDays
- if you want the description to be 'age, randomdate' you need to format it that way
- if you want a different random date on each user, you need to recreate that random date inside the loop
- Set-ADUser should be on its own line
- your example dates do not show whether you want the date as 'yyyy-MM-dd' or 'yyyy-dd-MM'
Try
$StartDate = Get-Date -Date '2015/01/01' # format yyyy/MM/dd
$EndDate = Get-Date -Date '2022/11/11' # format yyyy/MM/dd
$RangeInDays = 0..(($EndDate - $StartDate).Days - 1)
Get-ADUser -Filter * -Properties Description | ForEach-Object {
$DaysToAdd = Get-Random -InputObject $RangeInDays
$RandDate = $Startdate.AddDays($DaysToAdd)
$desc = '{0}, {1}' -f $_.Description, $RandDate.ToString("yyyy-MM-dd")
$_ | Set-ADUser -Description $desc
}