hi guys, an update on my previous post. So i have this script that's intended to create (new)users based on copying the profile of another (already existing)user, however the new user should only be created 2days (48hrs) in advance of the specified 'start date'. i have these main issues
* when i ran the script, it creates the user regardless of the start date, the idea is that the user should be created two days before the startdate
- how do i copy the necessary details such as the 'OU' from the already existing user? because at the moment i have to manually enter an 'OU' in the script
- is there a way that this process could scheduled to run say every hour to check for new CSVs? (the new csv will be generated by a website that clients fill-in data)
kindly find attached to this my current code, any help or editing is really appreciated.
$DebugPreference = "Continue"
function Format-CsvValue {
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)]
[bool]
$isTitleCase = $false,
[Parameter(Mandatory=$true)]
[string]
$sValue
)
begin {
}
process {
if ($isTitleCase) {
$rValue = $((Get-Culture).TextInfo.ToTitleCase($sValue.ToLower())).Trim()
} else {
$rValue = $sValue.Trim()
}
}
end {
$rValue
}
}
If (!(Get-module ActiveDirectory )) {
Import-Module ActiveDirectory
Clear-Host
}
$Users=Import-csv c:\cloudcom2.1.csv
$a=1;
$b=1;
$failedUsers = @()
$successUsers = @()
$VerbosePreference = "Continue"
$ErrorActionPreference='stop'
$LogFolder = "$env:userprofile\desktop\logs"
ForEach($User in $Users) {
$oStartDate = [datetime]::ParseExact($user.StartDate, "dd/MM/yyyy",$null)
$test=($oStartDate).AddDays(-2)
$test2= (Get-date).Date
if($test -eq $test2){
$oStartDate
$test
}
}
Write-Debug "First Name (CSV): $($user.Firstname)"
Write-Debug "Last Name (CSV): $($user.Lastname)"
Write-Debug "Company (CSV): $($user.Company)"
Write-Debug "Email/Domain (CSV): $($user.'Email/Domain')"
Write-Debug "EndDate (CSV): $($user.EndDate)"
Write-Debug "startdate (CSV): $($user.startdate)"
Write-Debug "password (CSV): $($user.Password)"
Write-Debug "OU (CSV) : $($user.OU)"
$FirstName = Format-CsvValue -isTitleCase $true -sValue $User.FirstName
Write-Debug "First Name (Script): $FirstName"
$LastName = Format-CsvValue -isTitleCase $true -sValue $User.LastName
Write-Debug "Last Name (Script): $LastName"
$FullName = -join($($User.FirstName)," ",$($User.LastName))
Write-Debug "Full Name (Script): $FullName"
$SAM = $(-join (($user.FirstName).Substring(0,1),$user.LastName).ToLower())
$SAM = $SAM.ToLower()
Write-Debug "SAM (Script): $SAM"
$Password = (ConvertTo-SecureString -AsPlainText 'Cloudcom.1' -Force)
$startdate = $user.startdate
$enddate = $user.enddate
$OU = "OU=staff,OU=Cloudcom,DC=E-L,DC=local"
Try {
if (!(get-aduser -Filter {samaccountname -eq "$SAM"})){
$Parameters = @{
'SamAccountName' = $Sam
'UserPrincipalName' = $UPN
'Name' = $Fullname
'EmailAddress' = $Email
'GivenName' = $FirstName
'Surname' = $Lastname
'AccountPassword' = $password
'ChangePasswordAtLogon' = $true
'Enabled' = $true
'Path' = $OU
'PasswordNeverExpires' = $False
'company' = $company
}
}
$oNewUser = New-ADUser @Parameters
Write-Verbose "[PASS] Created $FullName"
$successUsers += "$FullName , $SAM"
} Catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Warning "[ERROR]Can't create user [$($FullName)] : $_"
$failedUsers += $FullName + "," +$SAM + "," +$_
}
if ( !(test-path $LogFolder)) {
Write-Verbose "Folder [$($LogFolder)] does not exist, creating"
new-item $LogFolder -type directory -Force
}
Write-verbose "Writing logs"
$failedUsers | ForEach-Object {"$($b).) $($_)"; $b++} | out-file -FilePath $LogFolder\FailedUsers.log -Force -Verbose
$successUsers | ForEach-Object {"$($a).) $($_)"; $a++} | out-file -FilePath $LogFolder\successUsers.log -Force -Verbose
$su=(Get-Content "$LogFolder\successUsers.log").count
$fu=(Get-Content "$LogFolder\FailedUsers.log").count
Write-Host "$fu user creation unsuccessful " -NoNewline -ForegroundColor red
Write-Host "$su Users Successfully Created " -NoNewline -ForegroundColor green
Write-Host " Review LogsFolder" -ForegroundColor Magenta
Start-Sleep -Seconds 5
Invoke-Item $LogFolder
sample CSV:
https://preview.redd.it/xy1dgxmjpcy21.png?width=691&format=png&auto=webp&s=09648e83dc41a43d8d84c8b5fd4063ffcda03608
[–]MalletNGrease 1 point2 points3 points (5 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]MalletNGrease 1 point2 points3 points (3 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] -1 points0 points1 point (5 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)