@@ -84,74 +84,159 @@ Catch {
8484# [ CLI for Microsoft 365] ( #tab/cli-m365-ps )
8585
8686``` powershell
87- # SharePoint online site URL
88- $SiteURL = Read-Host -Prompt "Enter your SharePoint site URL (e.g https://contoso.sharepoint.com/sites/Company311)"
87+ function Add-DummySharePointContent {
88+ [CmdletBinding(SupportsShouldProcess)]
89+ param(
90+ [Parameter(Mandatory, HelpMessage = "URL of the SharePoint site (e.g. https://contoso.sharepoint.com/sites/Company311)")]
91+ [ValidateNotNullOrEmpty()]
92+ [string]
93+ $SiteUrl,
94+
95+ [Parameter(Mandatory, HelpMessage = "Site-relative URL of the document library (e.g. /Shared Documents)")]
96+ [ValidateNotNullOrEmpty()]
97+ [string]
98+ $LibraryServerRelativeUrl,
99+
100+ [Parameter(Mandatory, HelpMessage = "Path to the local seed file that will be uploaded")]
101+ [ValidateScript({ Test-Path $_ -PathType Leaf })]
102+ [string]
103+ $LocalSeedFile,
104+
105+ [Parameter(HelpMessage = "Number of folders to create")]
106+ [ValidateRange(1, 2000)]
107+ [int]
108+ $FolderCount = 50,
109+
110+ [Parameter(HelpMessage = "Number of files to create within each folder")]
111+ [ValidateRange(1, 500)]
112+ [int]
113+ $FilesPerFolder = 10,
114+
115+ [Parameter(HelpMessage = "Folder name prefix")]
116+ [ValidateNotNullOrEmpty()]
117+ [string]
118+ $FolderPrefix = "Folder"
119+ )
120+
121+ begin {
122+ Write-Host "Ensuring Microsoft 365 CLI session..." -ForegroundColor Cyan
123+ $loginOutput = m365 login --ensure 2>&1
124+ if ($LASTEXITCODE -ne 0) {
125+ throw "Failed to ensure CLI login. CLI output: $loginOutput"
126+ }
89127
90- # Document library URL where you want to create the dummy folders and files
91- $LibraryName = Read-Host -Prompt "Enter site-relative URL of your Document library (e.g '/Shared Documents')"
128+ $seedFile = Get-Item -LiteralPath $LocalSeedFile -ErrorAction Stop
92129
93- # Location of the dummy file
94- $LocalFile= "D:\dtemp\TestDoc.docx"
130+ $script:Summary = [ordered]@{
131+ FoldersRequested = $FolderCount
132+ FilesRequested = $FolderCount * $FilesPerFolder
133+ FoldersCreated = 0
134+ FoldersSkipped = 0
135+ FilesUploaded = 0
136+ FilesSkipped = 0
137+ Failures = 0
138+ }
95139
96- # Number of files to create within each folder
97- $MaxFilesCount = 20
140+ $script:Report = New-Object System.Collections.Generic.List[object]
141+ $script:ReportPath = Join-Path -Path (Get-Location) -ChildPath ("dummy-content-report-{0}.csv" -f (Get-Date -Format 'yyyyMMdd-HHmmss'))
142+ }
143+
144+ process {
145+ for ($folderIndex = 1; $folderIndex -le $FolderCount; $folderIndex++) {
146+ $folderName = "{0}_{1}" -f $FolderPrefix, $folderIndex
147+ $folderDisplayPath = "$LibraryServerRelativeUrl/$folderName"
148+
149+ if (-not $PSCmdlet.ShouldProcess($folderDisplayPath, "Create folder")) {
150+ $script:Summary.FoldersSkipped++
151+ $script:Report.Add([pscustomobject]@{
152+ ItemType = 'Folder'
153+ Name = $folderName
154+ Path = $folderDisplayPath
155+ Status = 'Skipped'
156+ Note = 'WhatIf'
157+ })
158+ continue
159+ }
98160
99- # Number of folders to create in the libraru
100- $MaxFolderCount = 500
161+ $folderOutput = m365 spo folder add --webUrl $SiteUrl --parentFolderUrl $LibraryServerRelativeUrl --name $folderName --output json 2>&1
162+ if ($LASTEXITCODE -ne 0) {
163+ $script:Summary.Failures++
164+ Write-Warning "Failed to create folder '$folderName'. CLI output: $folderOutput"
165+ }
166+ else {
167+ $script:Summary.FoldersCreated++
168+ }
101169
102- # The name of the folder to be created
103- $FolderName = "Folder"
170+ $script:Report.Add([pscustomobject]@{
171+ ItemType = 'Folder'
172+ Name = $folderName
173+ Path = $folderDisplayPath
174+ Status = if ($LASTEXITCODE -eq 0) { 'Created' } else { 'Failed' }
175+ Note = if ($LASTEXITCODE -eq 0) { '' } else { $folderOutput }
176+ })
177+
178+ for ($fileIndex = 1; $fileIndex -le $FilesPerFolder; $fileIndex++) {
179+ $newFileName = "{0}_{1}{2}" -f $seedFile.BaseName, $fileIndex, $seedFile.Extension
180+ $targetFolder = "$LibraryServerRelativeUrl/$folderName"
181+
182+ if (-not $PSCmdlet.ShouldProcess("$targetFolder/$newFileName", "Upload file")) {
183+ $script:Summary.FilesSkipped++
184+ $script:Report.Add([pscustomobject]@{
185+ ItemType = 'File'
186+ Name = $newFileName
187+ Path = "$targetFolder/$newFileName"
188+ Status = 'Skipped'
189+ Note = 'WhatIf'
190+ })
191+ continue
192+ }
104193
105- # Get Credentials to connect
106- $m365Status = m365 status
107- if ($m365Status -match "Logged Out") {
108- m365 login
109- }
194+ $fileOutput = m365 spo file add --webUrl $SiteUrl --folder $targetFolder --path $seedFile.FullName --fileName $newFileName --output json 2>&1
195+ if ($LASTEXITCODE -ne 0) {
196+ $script:Summary.Failures++
197+ Write-Warning "Failed to upload file '$newFileName'. CLI output: $fileOutput"
198+ }
199+ else {
200+ $script:Summary.FilesUploaded++
201+ }
110202
111- Try {
112- # Get the File from file server
113- $File = Get-ChildItem $LocalFile
203+ $script:Report.Add([pscustomobject]@{
204+ ItemType = 'File'
205+ Name = $newFileName
206+ Path = "$targetFolder/$newFileName"
207+ Status = if ($LASTEXITCODE -eq 0) { 'Uploaded' } else { 'Failed' }
208+ Note = if ($LASTEXITCODE -eq 0) { '' } else { $fileOutput }
209+ })
210+ }
211+ }
212+ }
114213
115- # Initialize folder counter
116- $FolderCounter = 1
117-
118- While($FolderCounter -le $MaxFolderCount)
119- {
120- $newFolderName = $FolderName + "_" + $FolderCounter
121- Try {
122- # Add new folder in the library
123- m365 spo folder add --webUrl $SiteURL --parentFolderUrl $LibraryName --name $newFolderName
124- Write-Host -f Green "New Folder '$newFolderName' Created ($FolderCounter of $MaxFolderCount)!"
125-
126- # Initialize file counter
127- $FileCounter = 1
128-
129- While($FileCounter -le $MaxFilesCount)
130- {
131- $NewFileName = $File.BaseName + "_" + $FileCounter + ".docx"
132- Try {
133- # Add new file in the folder
134- m365 spo file add --webUrl $SiteURL --folder "$($LibraryName)/$newFolderName" --path $File --FileLeafRef $NewFileName
135- }
136- Catch {
137- Write-Host "Error while creating a new file: $($_.Exception.Message)" -ForegroundColor Red
138- }
139- Write-Host -f Green "New File '$NewFileName' Created ($FileCounter of $MaxFilesCount)!"
140- $FileCounter++
141- }
142- }
143- Catch {
144- Write-Host "Error while creating a new folder: $($_.Exception.Message)" -ForegroundColor Red
145- }
146- $FolderCounter++;
147- }
148- }
149- Catch {
150- write-host -f Red "Error Uploading File:"$_.Exception.Message
214+ end {
215+ try {
216+ $script:Report | Export-Csv -Path $script:ReportPath -NoTypeInformation -Encoding UTF8
217+ Write-Host "Report saved to $($script:ReportPath)." -ForegroundColor Green
218+ }
219+ catch {
220+ $script:Summary.Failures++
221+ Write-Error "Failed to write report: $($_.Exception.Message)"
222+ }
223+
224+ Write-Host "----- Summary -----" -ForegroundColor Cyan
225+ Write-Host "Folders requested : $($script:Summary.FoldersRequested)"
226+ Write-Host "Folders created : $($script:Summary.FoldersCreated)"
227+ Write-Host "Folders skipped : $($script:Summary.FoldersSkipped)"
228+ Write-Host "Files requested : $($script:Summary.FilesRequested)"
229+ Write-Host "Files uploaded : $($script:Summary.FilesUploaded)"
230+ Write-Host "Files skipped : $($script:Summary.FilesSkipped)"
231+ Write-Host "Failures : $($script:Summary.Failures)"
232+
233+ if ($script:Summary.Failures -gt 0) {
234+ Write-Warning "Some operations failed. Review the report for details."
235+ }
236+ }
151237}
152238
153- # Disconnect SharePoint online connection
154- m365 logout
239+ Add-DummySharePointContent -SiteUrl "https://contoso.sharepoint.com/sites/Company311" -LibraryServerRelativeUrl "/Shared Documents" -LocalSeedFile "D:\dtemp\TestDoc.docx" -FolderCount 100 -FilesPerFolder 10
155240```
156241
157242[ !INCLUDE [ More about CLI for Microsoft 365] ( ../../docfx/includes/MORE-CLIM365.md )]
@@ -164,6 +249,7 @@ m365 logout
164249| -----------|
165250| [ Reshmee Auckloo] ( https://github.com/reshmee011 ) |
166251| [ Ganesh Sanap] ( https://ganeshsanapblogs.wordpress.com/about ) |
252+ | Adam Wójcik |
167253
168254[ !INCLUDE [ DISCLAIMER] ( ../../docfx/includes/DISCLAIMER.md )]
169255
0 commit comments