For this activity, it will be to create a script that meets the following criter
ID: 3766591 • Letter: F
Question
For this activity, it will be to create a script that meets the following criteria in PowerShell. The focus will be to create and utilize a student created "name" algorithm and utilize functions to achieve that purpose. The generated name is part of a Name-Value pair in a Associative Array. •The Function will conform to naming convention used in PowerShell of Verb-Noun such as Write-Host (write is the verb and Host is the noun). The name of the function shall be Create-Basename. •The function should receive one parameter and return to the calling procedure the result of the create name operation. •The create name operation will be left up to the student but should utilize some form of manipulation of the incoming parameter that will return a namebase between 4 and 8 characters. •The main portion of the script (not the function) will ask the user for a Customer Name, Address, City, State, Zip from the user and then store those into a Name- Value pairs in an associative array. (recall that array type on starting on page 162) •The number of Customer Names and Address sets asked of the user will be exactly 6 for this script. This means that the script will ask the user for a name, address, city, state, zip, then store them into the associative array, then ask for the next set. It will store a total of 6 address sets. •After the 6 customer names and addresses have been entered and stored, the script will ask the user for a name. If the name is one of the 6 (ie in the associative array), the script displays back the full address information for that person. If the person is not in the array, it displays an apology to the user. Either way, the script then ends. •To do the search in the associative array, the create namebase function written by the student must be utilized to create a name base from the input name and use that result to check the associative array. A loop may NOT be used to search the elements of the associative array. •First and Last Names are to be entered by the user when asked for a name and then combined into one full name when being stored into the associative array. Your instructions to the user will need to reflect that. •Address, City, State, Zip will be stored into the array into their own elements separately for each address set entered utilizing the namebase value of the name they are associated with. This puts a logical link between the names and address sets. You know the name, you can create a namebase and use that to get the address set that goes with it. •The create namebase Function must be considered substantial. A simple "i use the first four letters of the input value" is not considered substantial. There must be a logic for the formulation of the create namebase function. •The Function in the script must include a short description in the form of comments in the script of How the create namebase function manipulates the input value to create the namebase value. •This version of the script will not have to deal any with collisions (name bases that are the smae) but the student should consider working on making it less likely to have the collisions when creating their logic for making the namebase. •Be sure to include your script that you have written in your dropbox submission in its entirety. •Screen shots must show the entrance of all names and a succesful search. Then an unsuccessful search. You don't have to redo the names screen shots for the 2nd set search unless you use a different set of customer names.
Explanation / Answer
Try
{
Import-Module ActiveDirectory -ErrorAction Stop
}
Catch
{
Write-Host "[ERROR]`t ActiveDirectory Module couldn't be loaded. Script will stop!"
Exit 1
}
#----------------------------------------------------------
#STATIC VARIABLES
#----------------------------------------------------------
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$newpath = $path + "import_create_ad_users.csv"
$log = $path + "create_ad_users.log"
$date = Get-Date
$addn = (Get-ADDomain).DistinguishedName
$dnsroot = (Get-ADDomain).DNSRoot
$i = 1
#----------------------------------------------------------
#START FUNCTIONS
#----------------------------------------------------------
Function Start-Commands
{
Create-Users
}
Function Create-Users
{
"Processing started (on " + $date + "): " | Out-File $log -append
"--------------------------------------------" | Out-File $log -append
Import-CSV $newpath | ForEach-Object {
If (($_.Implement.ToLower()) -eq "yes")
{
If (($_.GivenName -eq "") -Or ($_.LastName -eq "") -Or ($_.Initials -eq ""))
{
Write-Host "[ERROR]`t Please provide valid GivenName, LastName and Initials. Processing skipped for line $($i)`r`n"
"[ERROR]`t Please provide valid GivenName, LastName and Initials. Processing skipped for line $($i)`r`n" | Out-File $log -append
}
Else
{
# Set the target OU
$location = $_.TargetOU + ",$($addn)"
# Set the Enabled and PasswordNeverExpires properties
If (($_.Enabled.ToLower()) -eq "true") { $enabled = $True } Else { $enabled = $False }
If (($_.PasswordNeverExpires.ToLower()) -eq "true") { $expires = $True } Else { $expires = $False }
# A check for the country, because those were full names and need
# to be land codes in order for AD to accept them. I used Netherlands
# as example
If($_.Country -eq "Netherlands")
{
$_.Country = "NL"
}
Else
{
$_.Country = "EN"
}
# Replace dots / points (.) in names, because AD will error when a
# name ends with a dot (and it looks cleaner as well)
$replace = $_.Lastname.Replace(".","")
If($replace.length -lt 4)
{
$lastname = $replace
}
Else
{
$lastname = $replace.substring(0,4)
}
# Create sAMAccountName according to this 'naming convention':
# <FirstLetterInitials><FirstFourLettersLastName> for example
# htehp
$sam = $_.Initials.substring(0,1).ToLower() + $lastname.ToLower()
Try { $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$sam)" }
Catch { }
If(!$exists)
{
# Set all variables according to the table names in the Excel
# sheet / import CSV. The names can differ in every project, but
# if the names change, make sure to change it below as well.
$setpass = ConvertTo-SecureString -AsPlainText $_.Password -force
Try
{
Write-Host "[INFO]`t Creating user : $($sam)"
"[INFO]`t Creating user : $($sam)" | Out-File $log -append
New-ADUser $sam -GivenName $_.GivenName -Initials $_.Initials `
-Surname $_.LastName -DisplayName ($_.LastName + "," + $_.Initials + " " + $_.GivenName) `
-Office $_.OfficeName -Description $_.Description -EmailAddress $_.Mail `
-StreetAddress $_.StreetAddress -City $_.City -State $_.State `
-PostalCode $_.PostalCode -Country $_.Country -UserPrincipalName ($sam + "@" + $dnsroot) `
-Company $_.Company -Department $_.Department -EmployeeID $_.EmployeeID `
-Title $_.Title -OfficePhone $_.Phone -AccountPassword $setpass -Manager $_.Manager
#[SNAP]DOWNLOAD THE SCRIPT FOR THE REST OF THE CODE[/SNAP]
}
"--------------------------------------------" + "`r`n" | Out-File $log -append
}
Write-Host "STARTED SCRIPT`r`n"
Start-Commands
Write-Host "STOPPED SCRIPT"