I just wanted to say thank you everyone for your support with 365AutomatedLab! This has been a side project for me that I’ve enjoyed doing and glad others are getting the benefits that I have from it. I’ve never been great with celebrating wins, but it really is cool to already have 50+ stars on GitHub for this project. The module is still in it’s early days and can’t wait to see what it turns into as I have a lot of ideas for it, just need to find the time to do it! Again, thank you all for your support and input on this project!
PowerShell
365AutomatedLab Update: Now Supports MacOS
You read that write v0.1.4 now supports MacOs! As long as you are using PowerShell 7.x you are good to go. Also in v0.1.4, you are now able to export your users from your main tenant into a worksheet that is easily imported into your dev tenant. You will only have to add the licensing(Please let me know if you want me to default the developer licenses). I’ve cleaned up some of the documentation and help as well. I’m really excited about this release and can’t wait to add more features! Let me know what you would like to see next.
For easy installation:
Install-Module -Name 365AutomatedLab
As always any PR, Issue, or question is always welcome. Have a great day!
Detailed Changelog:
https://github.com/DevClate/365AutomatedLab/blob/main/CHANGELOG.md
GitHub:
https://github.com/DevClate/365AutomatedLab
PowerShell Gallery:
https://www.powershellgallery.com/packages/365Automatedlab/0.1.4
Getting Started with 365AutomatedLab Part 3
Adding/Removing a User from Groups by User Role
I have to say that this function is one of my favorites. The reason why is how many companies can use only dynamic groups for adding users to their required groups? Using this function, you can create any job role name, then add the groups that job role requires. If you need help mass created groups check out Creating Groups with Excel. It can become the source of truth for standard groups per job role so you are only updating one location. Please test on your dev tenant first, but with most of the other cmdlets in this module they can be used in production, after proper testing.
Adding a User to Groups by User Role
The quickest way to add groups to a user is to use the 365DataEnvironment Excel file in the LabSources folder. I’ll show you that now, and how to customize it for your environment.
New-CT365GroupByUserRole -FilePath "C:\\365AutomatedLab\\LabSources\\365DataEnvironment.xlsx" -UserEmail mwhite@yourdomain.onmicrosoft.com -Domain yourdomain.onmicrosoft.com -UserRole NY-IT
This one line of PowerShell will add Mary White to all the groups associated with the user role “NY-IT,” which are “IT 365 Group, IT, IT NY, Printer – NY – 1, and Printer – NY – 2.”
FilePath: Is the location of the Excel workbook
UserEmail: The full email of the user you want to add groups to
Domain: The domain of your tenant
UserRole: This be the name of the worksheet for that user role.
If you want to create your own user roles, all you have to do is create a new worksheet and label it with the user role you want. Then you will need 4 columns which will be “DisplayName, PrimarySMTP, Description, and Type.” For type, the 4 different options are “365Group, 365MailEnabledSecurity, 365Distribution, and 365Security” depending on which groups you want to add them to. See image below for the template:
You can create as many user roles as you want. I used NY-IT naming convention as an example, but you can use whichever you want, as I know there are too many variations on how companies handle this and didn’t want to do a validateset on it.
That’s it, that is how you create the different job roles then how you add those groups to a user!
Removing a User from Groups by Job Role
I have a feeling after reading how to add, you already know how to remove groups from a user.
Remove-CT365GroupByUserRole -FilePath "C:\\365AutomatedLab\\LabSources\\365DataEnvironment.xlsx" -UserEmail mwhite@yourdomain.onmicrosoft.com -Domain yourdomain.onmicrosoft.com -UserRole NY-IT
Was I right? I really try to make these as simple as possible as I know we all are busy enough, and don’t want to make your life any busier.
Summary
You made it! Now you can easily add and remove user groups by job role with an Excel workbook. I know this is in 365AutomatedLab, but once you have tested, you could use this in production as it is great for onboarding and offboarding users.
As always please feel free to reach out if you have any questions, comments, concerns on this project or any others! If you can please star the GitHub repository as it will help others see it. Have a great day!
GitHub: https://github.com/DevClate/365AutomatedLab
Part 1 – Creating Users with Excel
Part 2 – Creating Groups with Excel
Getting Started with 365AutomatedLab Part 2
Adding/Removing Groups
Adding and removing groups with 365AutomatedLab is as easy as it is for adding and removing users(If you missed part one, check out Getting Started with 365AutomatedLab Part 1) It can add and remove the 4 different groups(Group, Mail Enabled Security, Distribution, and Security) that 365 has from one excel sheet. As of right now it will fill in the Display Name, Primary SMTP, and Description. I’ll be adding more parameters, but wanted to get at least these available. What are some other parameters you want to see first?
Adding Groups
Now the fun part, adding groups with one line of code. But, first we need to have a worksheet named “Groups” with the column headers
- DisplayName
- PrimarySMTP
- Description
- Type
After you have created those, you can input the data that you want in those fields. Remember for type you will use 365Group, 365MailEnabledSecurity, 365Distribution, or 365Security, which will tell 365 what type of group you want created. If you don’t have test data feel free to go to the already premade Excel workbook in the LabSources section in the repository. Below is a quick snapshot of the Excel workbook.
Data all set? Let’s run this little One-Liner to import all these groups into 365!
New-CT365Group -FilePath C:\\Scripts\\LabSources\\365DataEnvironment3.xlsx -UserPrincipalName admin@yourdomainname.onmicrosoft.com -Domain yourdomainname.onmicrosoft.com
Depending on when you last logged in, you’ll have to confirm access twice, once for Exchange Online and once for Graph. If you don’t allow these, the script will look like it froze on you. Trying to save you some of the headaches that I went through!
Your 365 Portal should now look like this
All set! That’s really how easy it is!
Removing Groups
The beauty of this setup, is now you only have to one PowerShell command, and you are all set.
Remove-CT365Group -FilePath C:\\Scripts\\LabSources\\365DataEnvironment3.xlsx -UserPrincipalName admin@yourdomainname.onmicrosoft.com
All done, now all your groups in your Excel workbook are removed!
Summary
Wasn’t it easy? Testing larger data sets will be a breeze or setting up and removing tests. You should never have to test in a production environment, as you can copy those groups to the Excel workbook and import them into your Dev tenant. I hope you found this helpful and makes your life easier as it has mine. If you have any recommendations or questions, please feel free to reach out and/or create a Github Issue.
Getting Started with 365AutomatedLab Part 1
Adding/Removing Users
Adding Users
With 365AutomatedLab, you can add as many users with their primary licensing as you need at one time. Currently, the Microsoft Dev Environment only allows 25 licensed users, and one is your primary account, so the max I’d recommend creating is 24 users if you’ll only be using this in a dev environment.
Quickest way to get started is by opening the 365DataEnvironment Excel Workbook in the LabSources folder. Here you can see the formatting for the cells so that a user can be added. (Let me know if you would like more attributes added, as these are required and ones I felt would be most useful to start)
View of 365DataEnvinronment.xlsx
FirstName | LastName | UserName | Title | Department | StreetAddress | City | State | PostalCode | Country | PhoneNumber | MobilePhone | UsageLocation | License |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Alice | Johnson | ajohnson | IT Specialist | IT | 1818 Maple St | Albany | NY | 12207 | USA | 212-555-1234 | 212-555-5678 | US | DEVELOPERPACK_E5 |
If you want to use “real” data, I’d recommend exporting your users from your production environment that you want to test, which you can use “Export-CT365ProdUserToExcel” to do this if you don’t already have a script to do so. You will have to copy the information into the 365DataEnvironment.xlsx, as the export as of right now is using Microsoft default column headers and I need to change them to match the excel sheets.
Now that you have your data all set, it’s time to add them to your Dev tenant.
New-CT365User -FilePath "C:\\Path\\to\\file.xlsx" -domain "contoso.com"
The file path is exactly what it says and you are required to type out the .xlsx or it will fail. The domain will be the domain for your tenant, if its a dev tenant it should be domainname.onmicrosoft.com. Then it will ask you to type in a password that you will user for all users.
That’s it, now you have created all your users.
Deleting Users
Now say you are done testing with those users and you want to remove them, or something you were testing changed their information too much from their original state? All you need is this line of code, and they will all be removed.
Remove-CT365User -FilePath "C:\\Path\\to\\file.xlsx" -Domain "contoso.com"
After the magic of PowerShell happens, all those users will be gone. Nice and easy, right?
Summary
Hope you found this useful, as it has saved me many times/hours when testing different scripts and changing the source data too much, then having to remember the users information to delete. I was surprised when I hadn’t seen anything like this as I know there are so many people testing in 365, and why not make it easier? Please look out for the next part of this series where you’ll be able to add and remove all the different types of groups in 365.
As always feel free to reach out anytime, and let me know what you think as well! Have a great day!
Quick Use of OpenAI – End Processes
Here is a quick use of OpenAI / ChatGPT to remove Windows processes. If it doesn’t get it right the first time, it usually is a good enough base to only need an edit or 2 for simpler scripts. Not bad for going 2/2 on no edits needed!
PROMPT 1:
Me:
You are a powershell expert, can you write me a script to find all processes running then show me them in a window and I can select which ones I want to delete?
ChatGPT:
Certainly! Here’s a PowerShell script that will retrieve all running processes, display them in a GUI window using Out-GridView
, and then terminate the selected processes:
# Get all running processes
$processes = Get-Process | Select-Object Id, Name, CPU, Path
# Display processes in a GridView for selection
$selectedProcesses = $processes | Out-GridView -Title "Select processes to terminate" -PassThru
# Terminate selected processes
$selectedProcesses | ForEach-Object {
try {
Stop-Process -Id $_.Id -Force
Write-Host "Terminated process: $($_.Name) with ID: $($_.Id)"
} catch {
Write-Warning "Failed to terminate process: $($_.Name) with ID: $($_.Id)"
}
}
Steps:
- The script fetches all running processes and selects a few useful properties like Id, Name, CPU, and Path.
- The processes are then presented in a GridView window using
Out-GridView
. You can select multiple processes by holding down the Ctrl key and clicking on them. - After you click “OK”, the script will attempt to terminate the selected processes.
Note: Ensure you have the required permissions to terminate processes. Also, be cautious about which processes you terminate, as some are essential for the proper functioning of your system.
Review 1:
You can see that it created the first prompt perfectly, and no edits were necessary.
Selected the processes and gave me a GUI to delete the ones I don’t need anymore
PROMPT 2:
ME:
Can you make it so I can select which one I want see beween ram and cpu?
ChatGPT:
Of course! We can add an initial prompt to let you select between viewing processes based on RAM or CPU usage.
Here’s the updated script:
# Get all running processes
$processes = Get-Process | Select-Object Id, Name, CPU, WS
# Prompt the user to choose between CPU and RAM
$choice = $host.ui.PromptForChoice("Selection", "Choose the metric to display top 5 processes:", @("CPU", "RAM"), 0)
# Display the top 5 processes based on the selected metric
if ($choice -eq 0) {
$topProcesses = $processes | Sort-Object CPU -Descending | Select-Object -First 5
} else {
$topProcesses = $processes | Sort-Object WS -Descending | Select-Object -First 5
}
# Display processes in a GridView for selection
$selectedProcesses = $topProcesses | Out-GridView -Title "Select processes to terminate" -PassThru
# Terminate selected processes
$selectedProcesses | ForEach-Object {
try {
Stop-Process -Id $_.Id -Force
Write-Host "Terminated process: $($_.Name) with ID: $($_.Id)"
} catch {
Write-Warning "Failed to terminate process: $($_.Name) with ID: $($_.Id)"
}
}
Now, when you run the script, it will first prompt you to choose between CPU and RAM. After you make your selection, it will display the top 5 processes based on that metric. You can then select which processes you want to terminate.
Review 2:
As you can see this worked exactly how I wanted it to and gave me the option to pick the top 5 highest of ram or cpu usage, then allowed me to delete.