Nazaudy, a spark in your curious mind

Useful powershell scripts

To work with Powershell vs Exchange, first of all install Visual Studio Code on your computer, I recommend it to save the scripts, then do:

  1. First of all install PS 7.4.4 on your desire computer by visiting this link: https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.4#msi
  2. Run "pwsh.exe", the execute:
Execute: Install-Module -Name ExchangeOnlineManagement 
Connect-ExchangeOnline -UserPrincipalName This email address is being protected from spambots. You need JavaScript enabled to view it.

Edge, Firefox or similar will open, and once you login you can go back in again to Powershell and start executing some code with admin privileges 

 

Add AD-Group to a License subscription using Admin Center

Install-Module -Name "Microsoft.Graph"
Connect-MgGraph

 

These Useful powershell scripts can help you manage your environment:

 

  1. Robocopy to copy contents from A to B
  2. Script to send mail from printer using Office 365
  3. How to get list of members in an Office 365 Distribution List
  4. Room Resource calendar shows asets "busy" instead of details
  5. Configure a SharedMailbox to keep a copy of the sent emails
  6. Force Azure-AD synchronisation
  7. Assign a phone number to user in Microsoft Teams Phone

 

1. Robocopy to copy contents from A to B

To copy all the contents from a folder A to another folder B, using the mirror mode of Robocopy, execute this powershell script:

$Title = "Robocopy Results: Copy Share from FOLDERA to FOLDERB" + (Get-Date)
#Share From the files need to be copied
$SourceFolder = "X:\FOLDERA"
#Share where to copy all the files 
$DestinationFolder = "\\storage\FOLDERB"
$Logfile = "C:\temp\" + (date -f yyyy-MM-dd) + "-EXHIB.log"
$Subject = "$Title"
# Change robocopy options as needed. ( http://ss64.com/nt/robocopy.html )
Robocopy $SourceFolder $DestinationFolder /MIR /DCOPY:DAT /copy:dat /r:1 /w:1 /LOG:$Logfile

 

If you want to do the Robocopy using only specific folders, you can use this other different script:

#*=============================================
#* Base variables
#*=============================================

$Title = "Robocopy Results: Backup of DES-DATA LIVE to ONSITE"
$Body = "C:\TEMP\Robocopy_Logs\"  + "_DES-DATA LIVE to ONSITE.log"
$to = "<This email address is being protected from spambots. You need JavaScript enabled to view it.>"
$Folders = @("CollectionsArchive","CommsFilmBank","Data","EXHIB","FINANCE","Human Resources", "Management","newbuild","Payroll","Project","Public","READONLY","shared$","Templates","userarch","Users")


#*=============================================
#* EMAIL variables
#*=============================================
$SMTPServer = "designmuseum-org.mail.protection.outlook.com"
$SMTPPort = "25"
$Username = "This email address is being protected from spambots. You need JavaScript enabled to view it."
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
#$smtp.EnableSSL = $true
#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);


#*=============================================
#* Robocopy
#*=============================================
Foreach ($folder in $folders)
{
$SourceFolder = "\\172.16.30.35\" + $folder
$DestinationFolder = "G:\DES-DATA_BACKUP_ONSITE\" + $folder
$Logfile = "C:\TEMP\Robocopy_Logs\" + "_DES-DATA_" + $folder + ".log"
Robocopy $SourceFolder $DestinationFolder /e /mir /DCOPY:DAT /copy:dat /r:1 /w:5 /LOG:$Logfile /NP /FP /V
Add-Content $Body $SourceFolder
Add-Content $Body (Get-Content $LogFile | select-string -pattern "ERROR't" -CaseSensitive)
}


#*=============================================
#* MAIL variables
#*=============================================
#
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $Title
$message.to.add($to)
$message.from = $username
$message.body = (Get-Content $Body | Out-String)
$smtp.send($message)

 

2. Receive an email when a User Account gets locked

There are 2 scripts that you can use for this, each one related as to whether you're using Gmail Workspace or Microsoft Office 365 to send the notification email

#Send Mail via G-mail Workspace
Import-Module activedirectory
$users = Search-ADAccount -LockedOut | Select-Object -ExpandProperty SamAccountName 
ForEach ($user in $users)
{
$test = Get-AdUser $user -Properties * 
$Body = $test.CN + " " + "account was locked out. It is now unlocked"
Send-MailMessage -To $test.Manager -Subject "Below Users LAN ID is locked" -SmtpServer "smtp.gmail.com" -From "This email address is being protected from spambots. You need JavaScript enabled to view it."  -Body $Body
}
Search-ADAccount -LockedOut | Unlock-ADAccount

 

3. Script to send mail from printer using Office 365

On the printer, configure the following:

  • Server name = yourdomain-com.mail.protection.outlook.com
  • Port number - 25

For the PowerShell script, you can modify the following::

 

#*=============================================
#* EMAIL variables
#*=============================================
$SMTPServer = "designmuseum-org.mail.protection.outlook.com"
$SMTPPort = "25"
$Username = "This email address is being protected from spambots. You need JavaScript enabled to view it."
#$Password = "kljjyiwgdebedtid"
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
#$smtp.EnableSSL = $true
#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);



#*=============================================
#* MAIL variables
#*=============================================
#
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $Title
$message.to.add($to)
$message.from = $username
$message.body = (Get-Content $Body | Out-String)
$smtp.send($message)

 

4. How to get list of members in an Office 365 Distribution List

  1. Execute: 
    Get-DistributionGroup -Identity "This email address is being protected from spambots. You need JavaScript enabled to view it." -ResultSize Unlimited | Select Name, PrimarySMTPAddress, RecipientType | Export-CSV "C:\Temp\Distribution-List-Members.csv" -NoTypeInformation -Encoding UTF8

Get list of members from a Distribution Group

Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName This email address is being protected from spambots. You need JavaScript enabled to view it.
Get-AdGroupMember -identity "MFA-AD-GROUP" | select name > list.txt

Get-DistributionGroupMember -identity "Exchange Distribution List" | format-list
Add-DistributionGroupMember -identity "Exchange Distribution List" -member "New User 1"

get-adgroup -filter * | sort name | select Name

 

5. Room Resource calendar shows as "busy" instead of details

  • Get-CalendarProcessing -Identity Dessau | fl
  • Set-CalendarProcessing -Identity Kirby.Laing -DeleteComments $true-DeleteSubject $true
  • The key command to run, is this thou:
  • Set-MailboxFolderPermission -Identity Kirby.Laing:\calendar -User default -AccessRights LimitedDetails

 

6. Configure a SharedMailbox to keep a copy of the sent emails

  • set-mailbox finance -MessageCopyForSentAsEnabled $true
  • set-mailbox finance -MessageCopyForSendOnBehalfEnabled $true
  • set-mailbox finance -UseDatabaseQuotaDefaults $false

 

7. Force Azure-AD synchronisation

  • Get-ADSyncScheduler
  • Start-ADSyncSyncCycle -PolicyType Delta

 

8. Assign a phone number to user in Microsoft Teams Phone

Instead of opening MS Teams Admin Center and doing it manually, you can run this script to auto-magically assign a number to a user's profile. 

#Run PS as an Administrator!
Install-Module MicrosoftTeams #for me, that only worked when using Windows PowerShell ISE under admin credentials
Get-Module -ListAvailable -Name MicrosoftTeams #verify the installation
Connect-MicrosoftTeams -UserPrincipalName This email address is being protected from spambots. You need JavaScript enabled to view it.
Set-CsPhoneNumberAssignment -Identity This email address is being protected from spambots. You need JavaScript enabled to view it. -PhoneNumber 44203xxxxxxx -PhoneNumberType DirectRouting
Set-CsPhoneNumberAssignment -Identity This email address is being protected from spambots. You need JavaScript enabled to view it. -PhoneNumber 44203xxxxxxx -PhoneNumberType DirectRouting
Set-CsPhoneNumberAssignment -Identity This email address is being protected from spambots. You need JavaScript enabled to view it. -PhoneNumber 44203xxxxxxx -PhoneNumberType DirectRouting
Set-CsPhoneNumberAssignment -Identity This email address is being protected from spambots. You need JavaScript enabled to view it. -PhoneNumber 44203xxxxxxx -PhoneNumberType DirectRouting
Set-CsPhoneNumberAssignment -Identity This email address is being protected from spambots. You need JavaScript enabled to view it. -PhoneNumber 44203xxxxxxx -PhoneNumberType DirectRouting

 

 

References

 

Comments powered by CComment