Skip to main content

3 posts tagged with "Scripting"

View All Tags

· 4 min read

Secure Application Model

For this post you'll want a secure application model Azure AD application configured. Use these instructions from Gavin Stone's Gavsto.com blog.

CIPP Inspired

The code in this script was inspired by functionality in the CyberDrain Improved Partner Portal (CIPP). Need to manage M365 tenants - CIPP is a fantastic open-source solution for M365 management for single tenants and Microsoft CSP Partners alike.

Background information

In my quest to stop having to use Windows Sandbox or VMs to access Exchange Online via PowerShell (we have WinRM basic auth disabled) I've been looking at solutions using the Exchange Online REST API to run commands against our customer tenants. The result of my efforts is a pretty complex script that introduces a method to run any PowerShell script in the context of a set of customer tenants.

caution

You need to have configured the Secure Application Model according to the instructions linked above, you will need your SAM tokens etc accessible for this guide.

Securing Credentials in Scripts

Do not store credentials directly in scripts. You should utilise services like Azure KeyVault or other PAM solutions to secure credentials used in scripts, including this one.

· One min read

I'm the author of the NinjaOne PowerShell module so when I was looking for a quick way to identify duplicate devices I turned to that module and the NinjaOne API to find a solution.

About this script

This script is a quick way to identify duplicate devices in your NinjaOne instance. It can return a full listing of all devices grouped by serial number, it can also just return the ID, last contact date and serial number for the device with the oldest last contact date.

The script

Get-NinjaOneDuplicateDevices.ps1
#Requires -Module NinjaOne
#Requires -Version 7
[CmdletBinding()]
param (
# Returns all devices with duplicates.
[Parameter(ParameterSetName = 'All', Mandatory)]
[Switch]$All,
# Returns the duplicate devices only.
[Parameter(ParameterSetName = 'Duplicates', Mandatory)]
[Switch]$Duplicates
)

try {
$DuplicateDevices = Get-NinjaOneDevices -detailed | Group-Object { $_.system.serialNumber } | Where-Object { $_.count -gt 1 -and ($_.name -ne '$(DEFAULT_STRING)' -and $_.name -ne 'Default string') }
if ($All) {
$Output = $DuplicateDevices | ForEach-Object { $_ | Select-Object -ExpandProperty group | Sort-Object $_.lastContact }
} elseif ($Duplicates) {
$Output = $DuplicateDevices | ForEach-Object { $_ | Select-Object -ExpandProperty group | Sort-Object $_.lastContact | Select-Object -First 1 -Property id, lastContact, @{ name = 'serialNumber'; expression = { $_.system.serialNumber } } }
}
if ($Output) {
return $Output
} else {
Write-Warning 'No duplicate devices found or an unanticipated error occurred.'
}
} catch {
Write-Error 'Please ensure the NinjaOne PowerShell module is correctly installed and you have run the "Connect-NinjaOne" command and connected to NinjaOne.'
exit 1
}

At this time it's not possible to remove devices via the NinjaOne API so this script will only return the duplicate devices - you'll have to arrange to remove these devices manually.

If you want all devices with duplicates:

Get-NinjaOneDuplicateDevices -All

or just the older duplicated devices:

Get-NinjaOneDuplicateDevices -Duplicates

· 2 min read

Did you know, when you have a retention policy configured for OneDrive in Microsoft 365 you lose the ability to recursively delete folders. The OneDrive client often handles this by recursing through the folder tree to remove the online copies but sometimes that doesn't work and in those cases you can end up stuck with a folder tree that you would have to manually recurse to remove the offending folders by first emptying them entirely before OneDrive will let you delete them.

Well that's never going to happen ;-)

Enter PowerShell.

Required Modules

You'll need the PnP.PowerShell module for this script. Install with Install-Module 'PnP.PowerShell'

The script

Remove-OneDriveFolderRecursively.ps1
[CmdletBinding()]
param (
# OneDrive host name e.g. 'https://microsoft-my.sharepoint.com'
[Parameter(Mandatory)]
[String]$OneDriveHost,
# Site path e.g. '/personal/satya_nadella_microsoft_com' must start with '/'.
[Parameter(Mandatory)]
[String]$SitePath,
# Folder path e.g. '/Documents/Documents/PowerShell/Modules' must start with '/'.
[String]$FolderPath
)

# Setup some configuration variables.
$SiteURL = $OneDriveHost + $SitePath
$FolderSiteRelativeURL = $SitePath + $FolderPath

# Connect to the site with the PnP.PowerShell module.
Connect-PnPOnline -Url $SiteURL -Interactive
$Web = Get-PnPWeb
$Folder = Get-PnPFolder -Url $FolderSiteRelativeURL

# Function to recursively remove files and folders from the path given.
Function Clear-PnPFolder([Microsoft.SharePoint.Client.Folder]$Folder) {
$InformationPreference = 'Continue'
If ($Web.ServerRelativeURL -eq '/') {
$FolderSiteRelativeURL = $Folder.ServerRelativeUrl
} Else {
$FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($Web.ServerRelativeURL, [string]::Empty)
}
# First remove all files in the folder.
$Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File
ForEach ($File in $Files) {
# Delete the file.
Remove-PnPFile -ServerRelativeUrl $File.ServerRelativeURL -Force -Recycle
Write-Information ("Deleted File: '{0}' at '{1}'" -f $File.Name, $File.ServerRelativeURL)
}
# Second loop through sub folders and remove them - unless they are "special" or "hidden" folders.
$SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder
Foreach ($SubFolder in $SubFolders) {
If (($SubFolder.Name -ne 'Forms') -and (-Not($SubFolder.Name.StartsWith('_')))) {
# Recurse into children.
Clear-PnPFolder -Folder $SubFolder
# Finally delete the now empty folder.
Remove-PnPFolder -Name $SubFolder.Name -Folder $Site + $FolderSiteRelativeURL -Force -Recycle
Write-Information ("Deleted Folder: '{0}' at '{1}'" -f $SubFolder.Name, $SubFolder.ServerRelativeURL)
}
}
$InformationPreference = 'SilentlyContinue'
}
# Call the function to empty folder if it exists.
if ($null -ne $Folder) {
Clear-PnPFolder -Folder $Folder
} Else {
Write-Error ("Folder '{0}' not found" -f $FolderSiteRelativeURL)
}

Invoking the script

The script is looking for three parameters, an example invocation to remove a synced "Documents/PowerShell/Modules" folder in Satya Nadella's OneDrive would be:

Double the Documents

You'll notice below that the /Documents folder in the path is doubled. This is because the first folder in the OneDrive path is always /Documents then your path starts. In this example we're removing the /Documents/PowerShell/Modules folder.

Remove-OneDriveFolderRecursively -OneDriveHost 'https://microsoft-my.sharepoint.com' -SitePath '/personal/satya_natella_microsoft_com' -FolderPath '/Documents/Documents/PowerShell/Modules'

Hope this helps!

This post was inspired by a script posted on Microsoft's TechCommunity here...