1
### ENVIRONMENT SETTINGS
$user = "domain\user"
$pass = "thepassword"
$doclib = "Documents"
$url = "http://localhost/sites/teamsite"

### TOGGLE DEBUG MODE
$debug = $FALSE

### DO NOT MODIFY
$headers = @{accept = "application/json; odata=verbose"}
$formDigest = $null
$secPass = ConvertTo-SecureString $pass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $secPass)

function SetFormDigest() {
    $response = PostRequest ("/_api/contextinfo") $null
    $formDigest = $response.d.GetContextWebInformation.FormDigestValue
    $headers.Add("X-RequestDigest", $formDigest);

    if ($debug) {
        Write-Host "Form Digest: " $formDigest
    }
}

function Request ($endpoint, $body, $method) {
    if ($debug) {
        Write-Host "Endpoint: $endpoint, Method: $method, Cred:" $cred.UserName -BackgroundColor Green -ForegroundColor Black
        Write-Host "Header Keys:" $headers.Keys -BackgroundColor DarkGreen -ForegroundColor Gray
        Write-Host "Header Values:" $headers.Values -BackgroundColor DarkGreen -ForegroundColor Gray
        Write-Host "Body Keys:" $body.Keys -BackgroundColor DarkGreen -ForegroundColor Gray
        Write-Host "Body Values:" $body.Values -BackgroundColor DarkGreen -ForegroundColor Gray
    }
    return Invoke-RestMethod -Uri ($url+$endpoint) -Headers $headers -Method $method -Body $body -Credential $cred -ContentType "application/json;odata=verbose"
}

function GetRequest ($endpoint, $body) {
    return Request $endpoint $body ([Microsoft.PowerShell.Commands.WebRequestMethod]::Get)
}

function PostRequest ($endpoint, $body) {
    return Request $endpoint $body ([Microsoft.PowerShell.Commands.WebRequestMethod]::Post)
}

function CreateFolder($folderPath, $folderName) {
    Write-Host "Creating Folder '$foldername' (at $folderPath)"
    $response = PostRequest ("/_api/web/getFolderByServerRelativeURL('" + $folderPath + "')/folders") (ConvertTo-Json @{ 
        __metadata = @{ type= "SP.Folder" }
        ServerRelativeUrl = $folderName
    })
}


## Script Begin ##

SetFormDigest
CreateFolder $doclib "MyFolder"

I am trying to create a folder in my office 365 account using this code yet I get two errors

Invoke-RestMethod : The remote server returned an error: (403) Forbidden.
At C:\Users\dah\Documents\RobIACASMigration\test.ps1:34 char:12
+     return Invoke-RestMethod -Uri ($url+$endpoint) -Headers $headers -Method 
$me ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWe 
   bRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell. 
   Commands.InvokeRestMethodCommand

Invoke-RestMethod : Object reference not set to an instance of an object.
At C:\Users\dah\Documents\RobIACASMigration\test.ps1:34 char:12
+     return Invoke-RestMethod -Uri ($url+$endpoint) -Headers $headers -Method 
$me ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], NullReferenc 
   eException
    + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell. 
   Commands.InvokeRestMethodCommand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.