EnhancedSPOAO.psm1
#Region '.\Private\Clean-ExportsFolder.ps1' -1 function Clean-ExportsFolder { param ( [Parameter(Mandatory = $true)] [string]$FolderPath ) if (Test-Path -Path $FolderPath) { # Get all files in the folder $files = Get-ChildItem -Path "$FolderPath\*" -Recurse # Remove each file and log its name foreach ($file in $files) { try { Remove-Item -Path $file.FullName -Recurse -Force Write-EnhancedLog -Message "Deleted file: $($file.FullName)" -Level "INFO" -ForegroundColor ([ConsoleColor]::Yellow) } catch { Write-EnhancedLog -Message "Failed to delete file: $($file.FullName) - Error: $_" -Level "ERROR" -ForegroundColor ([ConsoleColor]::Red) } } Write-EnhancedLog -Message "Cleaned up existing folder at: $FolderPath" -Level "INFO" -ForegroundColor ([ConsoleColor]::Yellow) } else { # Create the folder if it does not exist New-Item -ItemType Directory -Path $FolderPath | Out-Null Write-EnhancedLog -Message "Created folder at: $FolderPath" -Level "INFO" -ForegroundColor ([ConsoleColor]::Green) } } # Example usage # $folderPath = "C:\path\to\exports" # Clean-ExportsFolder -FolderPath $folderPath -LogFunction Write-EnhancedLog #EndRegion '.\Private\Clean-ExportsFolder.ps1' 32 #Region '.\Private\Validate-FileUpload.ps1' -1 function Validate-FileUpload { param ( [Parameter(Mandatory = $true)] [string]$DocumentDriveId, [Parameter(Mandatory = $true)] [string]$FolderName, [Parameter(Mandatory = $true)] [string]$FileName, [Parameter(Mandatory = $true)] [hashtable]$Headers ) $url = "https://graph.microsoft.com/v1.0/drives/$DocumentDriveId/root:/$FolderName/$FileName" try { $response = Invoke-RestMethod -Headers $Headers -Uri $url -Method GET if ($response) { Write-EnhancedLog -Message "File '$FileName' exists in '$FolderName' after upload." -Level "INFO" -ForegroundColor ([ConsoleColor]::Green) } else { Write-EnhancedLog -Message "File '$FileName' does not exist in '$FolderName' after upload." -Level "ERROR" -ForegroundColor ([ConsoleColor]::Red) throw "File '$FileName' does not exist in '$FolderName' after upload." } } catch { Write-EnhancedLog -Message "Failed to validate file '$FileName' in '$FolderName': $_" -Level "ERROR" -ForegroundColor ([ConsoleColor]::Red) throw $_ } } #EndRegion '.\Private\Validate-FileUpload.ps1' 31 #Region '.\Public\Ensure-ExportsFolder.ps1' -1 function Ensure-ExportsFolder { param ( [Parameter(Mandatory = $true)] [string]$BasePath, [Parameter(Mandatory = $true)] [string]$ExportsFolderName, [Parameter(Mandatory = $true)] [string]$ExportSubFolderName ) # Construct the full path to the exports folder $ExportsBaseFolderPath = Join-Path -Path $BasePath -ChildPath $ExportsFolderName $ExportsFolderPath = Join-Path -Path $ExportsBaseFolderPath -ChildPath $ExportSubFolderName # Check if the base exports folder exists if (-Not (Test-Path -Path $ExportsBaseFolderPath)) { # Create the base exports folder New-Item -ItemType Directory -Path $ExportsBaseFolderPath | Out-Null Write-EnhancedLog -Message "Created base exports folder at: $ExportsBaseFolderPath" -Level "INFO" -ForegroundColor ([ConsoleColor]::Green) } if (-Not (Test-Path -Path $ExportsFolderPath)) { # Create the base exports folder New-Item -ItemType Directory -Path $ExportsFolderPath | Out-Null Write-EnhancedLog -Message "Created base exports folder at: $ExportsFolderPath" -Level "INFO" -ForegroundColor ([ConsoleColor]::Green) } # Ensure the subfolder is clean Clean-ExportsFolder -FolderPath $ExportsFolderPath # Return the full path of the exports folder return $ExportsFolderPath } #EndRegion '.\Public\Ensure-ExportsFolder.ps1' 35 #Region '.\Public\Get-SharePointDocumentDriveId.ps1' -1 function Get-SharePointDocumentDriveId { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string]$SiteObjectId, [Parameter(Mandatory = $true)] [string]$DocumentDriveName, [Parameter(Mandatory = $true)] [hashtable]$Headers ) try { # Get the subsite ID $url = "https://graph.microsoft.com/v1.0/groups/$SiteObjectId/sites/root" $subsiteID = (Invoke-RestMethod -Headers $Headers -Uri $url -Method GET).id Write-EnhancedLog -Message "Retrieved subsite ID: $subsiteID" -Level "INFO" # Get the drives $url = "https://graph.microsoft.com/v1.0/sites/$subsiteID/drives" $drives = Invoke-RestMethod -Headers $Headers -Uri $url -Method GET Write-EnhancedLog -Message "Retrieved drives for subsite ID: $subsiteID" -Level "INFO" # Find the document drive ID $documentDriveId = ($drives.value | Where-Object { $_.name -eq $DocumentDriveName }).id if ($documentDriveId) { Write-EnhancedLog -Message "Found document drive ID: $documentDriveId" -Level "INFO" return $documentDriveId } else { Write-EnhancedLog -Message "Document drive '$DocumentDriveName' not found." -Level "ERROR" -ForegroundColor ([ConsoleColor]::Red) throw "Document drive '$DocumentDriveName' not found." } } catch { Write-EnhancedLog -Message "Failed to get document drive ID: $_" -Level "ERROR" -ForegroundColor ([ConsoleColor]::Red) throw $_ } } # # Example usage # $headers = @{ # "Authorization" = "Bearer YOUR_ACCESS_TOKEN" # "Content-Type" = "application/json" # } # $siteObjectId = "your_site_object_id" # $documentDriveName = "Documents" # Get-SharePointDocumentDriveId -SiteObjectId $siteObjectId -DocumentDriveName $documentDriveName -Headers $headers #EndRegion '.\Public\Get-SharePointDocumentDriveId.ps1' 53 #Region '.\Public\New-SharePointFolder.ps1' -1 function New-SharePointFolder { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string]$DocumentDriveId, [Parameter(Mandatory = $true)] [string]$ParentFolderPath, [Parameter(Mandatory = $true)] [string]$FolderName, [Parameter(Mandatory = $true)] [hashtable]$Headers ) try { # Check if the folder already exists $checkUrl = "https://graph.microsoft.com/v1.0/drives/" + $DocumentDriveId + "/root:/" + $ParentFolderPath + ":/children" $existingFolders = Invoke-RestMethod -Headers $Headers -Uri $checkUrl -Method GET $existingFolder = $existingFolders.value | Where-Object { $_.name -eq $FolderName -and $_.folder } if ($existingFolder) { Write-EnhancedLog -Message "Folder '$FolderName' already exists in '$ParentFolderPath'. Skipping folder creation." -Level "INFO" return $existingFolder } } catch { Write-EnhancedLog -Message "Folder '$FolderName' not found in '$ParentFolderPath'. Proceeding with folder creation." -Level "INFO" } try { # If the folder does not exist, create it $url = "https://graph.microsoft.com/v1.0/drives/" + $DocumentDriveId + "/root:/" + $ParentFolderPath + ":/children" $body = @{ "@microsoft.graph.conflictBehavior" = "fail" "name" = $FolderName "folder" = @{} } Write-EnhancedLog -Message "Creating folder '$FolderName' in '$ParentFolderPath'..." -Level "INFO" $createdFolder = Invoke-RestMethod -Headers $Headers -Uri $url -Body ($body | ConvertTo-Json) -Method POST Write-EnhancedLog -Message "Folder created successfully." -Level "INFO" return $createdFolder } catch { Write-EnhancedLog -Message "Failed to create folder '$FolderName' in '$ParentFolderPath': $_" -Level "ERROR" -ForegroundColor ([ConsoleColor]::Red) throw $_ } } # # Example usage # $headers = @{ # "Authorization" = "Bearer YOUR_ACCESS_TOKEN" # "Content-Type" = "application/json" # } # $documentDriveId = "your_document_drive_id" # $parentFolderPath = "your/parent/folder/path" # $folderName = "NewFolder" # New-SharePointFolder -DocumentDriveId $documentDriveId -ParentFolderPath $parentFolderPath -FolderName $folderName -Headers $headers #EndRegion '.\Public\New-SharePointFolder.ps1' 63 #Region '.\Public\Upload-FileToSharePoint.ps1' -1 function Upload-FileToSharePoint { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string]$DocumentDriveId, [Parameter(Mandatory = $true)] [string]$FilePath, [Parameter(Mandatory = $true)] [string]$FolderName, [Parameter(Mandatory = $true)] [hashtable]$Headers ) try { # Validate file existence before upload Validate-FileExists -FilePath $FilePath # Read the file content $content = Get-Content -Path $FilePath -Raw $filename = (Get-Item -Path $FilePath).Name # Construct the PUT URL $putUrl = "https://graph.microsoft.com/v1.0/drives/$DocumentDriveId/root:/$FolderName/$($filename):/content" # Upload the file Write-EnhancedLog -Message "Uploading file '$filename' to folder '$FolderName'..." -Level "INFO" $uploadResponse = Invoke-RestMethod -Headers $Headers -Uri $putUrl -Body $content -Method PUT Write-EnhancedLog -Message "File '$filename' uploaded successfully." -Level "INFO" # Validate file existence after upload Validate-FileUpload -DocumentDriveId $DocumentDriveId -FolderName $FolderName -FileName $filename -Headers $Headers return $uploadResponse } catch { Write-EnhancedLog -Message "Failed to upload file '$filename' to folder '$FolderName': $_" -Level "ERROR" -ForegroundColor ([ConsoleColor]::Red) throw $_ } } #EndRegion '.\Public\Upload-FileToSharePoint.ps1' 43 |