Showing posts with label technical. Show all posts
Showing posts with label technical. Show all posts

Thursday, January 07, 2021

Techie Post - Deleting emails from multiple mailboxes in Office 365

Occasionally I'm asked at work to delete an email that has been accidentally sent out to the wrong people.  It doesn't happen very often, but inevitably when it does happen, I forget the process, so I am including it here for personal reference.

Create a content search rule within the Microsoft 365 Compliance area - https://compliance.microsoft.com/homepage  (take note of the name you call it - you'll need it in the New-ComplianceSearchAction command.

Run Powershell as administrator and enter the following commands...

set-executionpolicy remotesigned
(only use this if you get an error about not being able to run scripts via Powershell)

$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session

New-ComplianceSearchAction -SearchName "Rule Name used in content search" -Purge -PurgeType SoftDelete

If you would like to see the progress of the search, use the following command.

Get-ComplianceSearchAction


Monday, April 20, 2020

Technical Post - List Users of Active Directory Group

A small PowerShell script to list users in a specific Active Directory group

get-adgroup groupname | get-adgroupmember | sort name | % {$_.name}

groupname = The name of the AD group you wish to look at.

Saturday, January 04, 2020

Technical Post - Netlogon and SYSVOL not creating when promoting server to Domain Controller

Here's one that's been bugging me for a few months now..

At one of the schools we support, we needed to add an additional domain controller to an existing domain.  Sounds simple right? 

During the promotion process, the server creates the SYSVOL and Netlogon folders, but they wouldn't share.

The new server is running Microsoft Windows Server 2019, whereas the older server is still running 2012.

After doing some Googling, it appears this is a more common issue than I had originally thought, which is surprising since I've promoted and demoted many domain controllers over the years and have never had an issue like this.

Anyway, I managed to solve it in the end with a bit of searching.

Firstly, on the newer server, open Regedit and browse to "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters"

Change SysVolReady from 0 to 1

This will create the SYSVOL share.

To create the Netlogon share, I then had to manually create a folder called 'scripts' in the following location - c:\windows\sysvol\domain\

Once created, restart the Netlogon service.

That's the folders created and ready, but alas replication is not working between the servers.  Onwards to the next step...

On the old domain controller, look in Event Viewer for an error regarding DFS Replication (ID 2213).  The error will read "The DFS Replication service stopped replication on C:  This occurs when a DFSR JET database is not shut down cleanly and Auto Recovery is disabled."

Beneath the error will be a GUID.  Copy this long number.

Open an elevated command prompt and enter

wmic /namespace:\\root\microsoftdfs path dfsrVolumeConfig where volumeGuid="00000000-0000-0000-0000-000000000000" call ResumeReplication

Where 00000000-0000-0000-0000-000000000000 is, enter the GUID number from the error message (ID 2213)

Now run the following command to see the state of replication and you 'should' see that all servers are now showing State 4 (Normal) and replicating successfully.

For /f %i IN ('dsquery server -o rdn') do @echo %i && @wmic /node:"%i" /namespace:\\root\microsoftdfs path dfsrreplicatedfolderinfo WHERE replicatedfoldername='SYSVOL share' get replicationgroupname,replicatedfoldername,state

For information, the other states are....

0 = Uninitialized
1 = Initialized
2 = Initial Sync
3 = Auto Recovery
4 = Normal
5 = In Error

Now Active Directory and file replication between DCs should be working (you can double-check by running DCDIAG and looking for any errors).

I hope this helps someone!

Sunday, August 25, 2019

Techie Post - Nested If in Excel for converting month to a number

This post is more of a reminder for me, but may help someone else in a similar situation.  Every year in August when I need to create the new user accounts for the students starting in September at school, I need the student date of births to be in the format DDMMYY so that my custom powershell script can create the accounts in Active Directory

Sounds simple, but alas our School Information Management System exports the data in a weird format, so I am always having to modify and fix the exported data in excel before I can import the completed csv file.

Most can be accomplished using the excellent left, mid and right formulas, but the date (more specifically, the month) always causes me issues.

So, where I a have a cell where the month is "Jan", or "Feb", I need it to be "01" for Jan, "02" for Feb and so on.

So.... assuming cell F2 is the cell with the month in it, the following nested If formula will do the job

=IF(F2="Jan","01",IF(F2="Feb","02",IF(F2="Mar","03",IF(F2="Apr","04",IF(F2="May","05",IF(F2="Jun","06",IF(F2="Jul","07",IF(F2="Aug","08",IF(F2="Sep","09",IF(F2="Oct","10",IF(F2="Nov","11",IF(F2="Dec","12",))))))))))))

Once I have the month as a number, I can easily combine the 3 numbers making up the day, month and year (ddmmyy) in to a cell for my script to accept.

I'm sure there's a better way of doing it, but hey ho...  This does the job for now.


Wednesday, April 10, 2019

Technical Post - Creating new Active Directory user accounts on Windows Server 2016 using Powerscript

A useful little Powerscript which I have written for student user creation in a Windows Active Directory.  Some features may not be useful to all since I wrote this for our needs in our school, but it's pretty straight forward to use and I'm sure it could easily be modified to fit someones needs if they wanted the same.  The script creates the user object in Active Directory, adds them to groups, creates their user folder (on a different server), and sets the correct permissions on that folder.

I've posted this here more of a reminder to me if I ever need to revisit it, and I'm sure there is probably a far better and efficient way of doing this, but it may help someone, somewhere!

A couple of things to bear in mind..  Our student username format begins with two digits, then the first three letters of their surname, and then the first three letters of their forename (ie 19WilStu).   AND obviously, any references to server names and folder locations will need to be updated with your details

Please feel free to comment, and let me know if you have a better way of doing it :D

Write-Host ""
Write-Host "  STUDENT USER CREATION SCRIPT (Powerscript V1)"
Write-Host "  ---------------------------------------------"
Write-Host "             By Stu -  09/04/2019"
Write-Host ""

# ---------------------------------------------------------------------
# -     LETS GET SOME VARIABLES AND CREATE THE USERNAME FORMAT        -
# ---------------------------------------------------------------------
#
$forename = read-host -prompt 'Input users forename'
$surname = read-host -prompt 'Input users Surname'
$year = read-host -prompt 'Input users year - (This is the number in their username eg. 15, 06, 03)'
$forename_Three = ($forename.SubString(0,3))
$Surname_Three = ($surname.SubString(0,3))
$username = $Year + $Surname_Three + $Forename_Three
Write-Host "Hello $username"

# ---------------------------------------------------------------------
# -              CHECK TO SEE IF USER EXISTS OR NOT                   -
# ---------------------------------------------------------------------
#
if (dsquery user -samid $username)
{
Write-Host "Found user - PLEASE RESTART AND START AGAIN"
Pause
exit
}
    else {"Did not find user - Let's continue to setup the user"}

$password = read-host -prompt 'Enter default password'
Write-Host ""
Write-Host " We have everything we need to create the user account. Please wait"
Write-Host ""


# ---------------------------------------------------------------------
# -             CREATE USER OBJECT IN ACTIVE DIRECTORY                -
# ---------------------------------------------------------------------
#
New-ADUser $username -AccountPassword (ConvertTo-SecureString -AsPlainText $password -Force) -ChangePasswordAtLogon $True -path "OU=Year$Year,OU=Pupils,OU=Domain Users,DC=yourdomain,DC=local" -Company "Year$Year" -DisplayName "$forename $surname" -Description "Initial Password is $password" -HomeDrive U -HomeDirectory "\\Yourfileserver\folder\students\userareas\Year$Year\$username" -HomePage "www.yourwebsite.com" -UserPrincipalName "$username@domain" -EmailAddress "$username@youremaildomain.com" -Enabled $True -SamAccountName $username -Surname $Surname -GivenName $Forename -ea Stop

Add-ADGroupMember -Identity "Pupils" -Members $username
Add-ADGroupMember -Identity Year"$Year"Group -Members $username

# ---------------------------------------------------------------------
# -           USER AREA FOLDER CREATION AND PERMISSIONS               -
# ---------------------------------------------------------------------
#
$samAccountName = $username
$fullPath = "\\Yourfileserver\folder\students\userareas\Year$Year\{0}" -f $samAccountName
$driveLetter = "U:"

$User = Get-ADUser -Identity $samAccountName

if($User -ne $Null) {
    Set-ADUser $User -HomeDrive $driveLetter -HomeDirectory $fullPath -ea Stop
    $homeShare = New-Item -path $fullPath -ItemType Directory -force -ea Stop
    $acl = Get-Acl $homeShare
    $FileSystemRights = [System.Security.AccessControl.FileSystemRights]"Modify"
    $AccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
    $InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
    $PropagationFlags = [System.Security.AccessControl.PropagationFlags]"InheritOnly"
    $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($samAccountName, $FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType)
    $acl.AddAccessRule($AccessRule)
    Set-Acl -Path $homeShare -AclObject $acl -ea Stop
    Write-Host ("HomeDirectory created at {0}" -f $fullPath)


# ---------------------------------------------------------------------
# -This bit creates the default subject folders in the users user area-          -
# ---------------------------------------------------------------------

new-item \\Yourfileserver\folder\students\userareas\Year$Year\$username\Art -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\Business -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\Design -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\Drama -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\English -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\Food -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\Geography -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\History -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\ICT -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\Languages -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\Maths -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\Music -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\PE -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\Personal -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\RE -itemtype directory
new-item \\Yourfileserver\folder\students\userareas\Year$Year\Science -itemtype directory

Clear-Host
Write-Host ""
Write-Host "User $username has now been created successfully"
Write-Host ""
Write-Host "Username : $username"
Write-Host "Password : $password"
Write-Host ""
Pause