In: Computer Science
Windows PowerShell
1) Write a PowerShell Script to monitor a file for changes.
2) Write a PowerShell Script to create a user account in a specific OU.
I HAVE ADDED BOTH THE SCRIPT PLEASE FIND THE CODE
ANSWER 1>
Function Register-Watcher {
param ($folder)
$filter = "*.*" #all files
$watcher = New-Object IO.FileSystemWatcher $folder, $filter
-Property @{
IncludeSubdirectories = $false
EnableRaisingEvents = $true
}
$changeAction = [scriptblock]::Create('
# This is the code which will be executed every time a file change
is detected
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file $name was $changeType at $timeStamp"
')
Register-ObjectEvent $Watcher -EventName "Changed" -Action
$changeAction
}
Register-Watcher "c:\temp"
ANSWER 2>
Import-module activedirectory
#Import the list from the user
$Users = Import-Csv -Path C:\Users\vbedi\Desktop\Userlist.csv
foreach ($User in $Users)
{ $Office
$Displayname = $User.Lastname + ", " + $User.Firstname
$UserFirstname = $User.Firstname
$UserFirstIntial = $UserFirstname.Substring(1,1)
$Usermiddlename = $User.Middlename
$UserLastname = $User.Lastname
$OU = $User.OU
$SAM = $User.SAM
$UPN = $UserFirstname.Substring(1,1) + $User.Lastname + "@" +
$User.Maildomain
$Description = $User.Description
$Password = $User.Password
#Creation of the account with the requested formatting.
New-ADUser -Name "$Displayname" -DisplayName "$Displayname"
-SamAccountName $SAM -Office "Office" -UserPrincipalName $UPN
-GivenName "$UserFirstname" -Surname "$UserLastname" -Description
"$Description" -AccountPassword (ConvertTo-SecureString $Password
-AsPlainText -Force) -Enabled $true -Path "$OU"
-ChangePasswordAtLogon $True –PasswordNeverExpires $false -server
server.domain.local
$Displayname
}