Skip to main content

Download & read files via PowerShell

 

<#region PREP
# DIE FOLGENDEN BEIDEN STEPS SIND NUR VORBEREITUNG UND MÜSSEN NICHT JEDES MAL DURCHGEFÜHRT WERDEN.



# STEP 1: App Reg erstellen mit den folgenden Permissions
Delegated   - User.Read
Delegated   - Files.Read.Selected
Delegated   - Sites.Read.All
Application - Sites.Selected



# STEP 2: App auf SharePoint Site berechtigen
$siteUrl = "" # URL der Seite, worauf der Zugriff vergeben werden soll
$appId = "" # ID der App mit Sites.Selected Permission

# Verbinden auf SP Site (PNPOnline Modul benötigt)
Connect-PnPOnline -Url $siteUrl -Interactive # Anmeldung als Site Owner

# Der App die Schreibberechtigung auf der SP Site und allen Files darauf vergeben
Grant-PnPAzureADAppSitePermission -Permissions "Write" -Site $siteUrl -AppId $appId -DisplayName "$AppID - Write Access"
#endregion PREP#>



# DIE SITE ID KANN ÜBER DEN FOLGENDEN URL ABGEHOLT WERDEN: https://bkwfmbenergie.sharepoint.com/sites/T_BKH_MWP3_Files/_api/site/id
$SiteID = "f57476be-a118-4369-ba26-e7c1aac95847"


# NAME DER BIBLIOTHEK, IN DER SICH DIE DATEIEN BEFINDEN
$LibraryName = "Dokumente"


# DOWNLOAD VARIABLES
$OutPathRoot = "C:\Users\RUTRA\OneDrive - BKW\Desktop\GraphDownload"

if (-not $(Test-Path -LiteralPath $OutPathRoot)) {
    New-Item $OutPathRoot -Force -ItemType Directory | Out-Null
}


#region AUTHENTICATION
# AUTHENTICATION VARIABLES
$tenantId = "376e33ca-d656-4e46-8bcb-7d5ba898ae66"
$ClientId = ""
$ClientSecret = ""



$Body = @{
    "tenant"        = $TenantId
    "client_id"     = $ClientId
    "scope"         = "https://graph.microsoft.com/.default"
    "client_secret" = $ClientSecret
    "grant_type"    = "client_credentials"
}



$Params = @{
    "Uri"         = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
    "Method"      = "Post"
    "Body"        = $Body
    "ContentType" = "application/x-www-form-urlencoded"
}
$AuthResponse = Invoke-RestMethod @Params



$Header = @{
    "Authorization" = "Bearer $($AuthResponse.access_token)"
}
#endregion AUTHENTICATION






# GET DRIVE ID
$Params = @{
    "Method"      = "Get"
    "Uri"         = "https://graph.microsoft.com/v1.0/sites/$SiteID/drive"
    "Headers"     = $Header
    "ContentType" = "application/json"
}
$DriveID = $(Invoke-RestMethod @Params | where { $_.name -eq $LibraryName }).id



# HOLE ALLE FILES AUS DEM SHAREPOINT ORDNER
$Params = @{
    "Method"      = "Get"
    "Uri"         = "https://graph.microsoft.com/v1.0/sites/$SiteID/drives/$DriveID/root/children"
    "Headers"     = $Header
    "ContentType" = "application/json"
}
$SPFiles = $(Invoke-RestMethod @Params).Value



# ALLE FILES HERUNTERLADEN
foreach ($File in $SPFiles) {
    if ($File.'@microsoft.graph.downloadUrl') { # Schliesst Ordner aus
        $Params = @{
            "Method"      = "Get"
            "Uri"         = $File.'@microsoft.graph.downloadUrl'
            "ContentType" = "application/json"
            "OutFile"     = "$OutPathRoot\$($File.name)"
        }
        Invoke-RestMethod @Params
    }
}



# EINZELNES FILE HERUNTERLADEN
$File = $SPFiles[1]
$Params = @{
    "Method"      = "Get"
    "Uri"         = $File.'@microsoft.graph.downloadUrl'
    "ContentType" = "application/json"
    "OutFile"     = "$OutPathRoot\$($File.name)"
}
Invoke-RestMethod @Params



# NUR DEN INHALT EINES FILES HOLEN
$File = $SPFiles[1]
$Params = @{
    "Method"      = "Get"
    "Uri"         = $File.'@microsoft.graph.downloadUrl'
    "ContentType" = "application/json"
}
$Content = Invoke-RestMethod @Params
$Content