cancel
Showing results for 
Search instead for 
Did you mean: 
pamedmoston
Surveyor II

Powershell Script Issue

Jump to solution

Hello all,

I am trying to use the offered Powershell script (FullBatch.ps1) to upload our SIS data to Canvas. I have manually uploaded the files with no problem. I am using the same files, and keep getting the same error: Unable to start an SIS import for this term. Please ensure a term with this SIS ID has already been created in Canvas. (SIS ID's are case sensative)

(sensative should be spelled sensitive by the way!). I know that my regex is fine, I know that the term in the terms file has the same SIS ID as the term that has already been uploaded. Has anyone else had this issue and how did you resolve it? Any suggestions?

Thank you!

Pam

1 Solution

Accepted Solutions

Thanks, Alex, that's good to know. The issue is that I had version 2 of Powershell, not version 3! The script is using commands that are new in version 3. With version 3, it is running!

New question: The current script is looking for a zip file. Does anyone know how it could be modified to work directly with .csv files? My extract process creates the .csv files but doesn't zip them.

Thanks for your help, everyone!

View solution in original post

8 Replies
MattHanes
Adventurer III

Can you post the code with the changes you've made? You can just erase your API Key so we can't do anything with it on accident.

Hi Matt,

Yes see below. The only part I edited (that I left in), is the regex. I removed our domain and our token. The term is 2016SP. This is also the name of the zip file, and the SIS ID of the term that has already been uploaded. It gets past the regex matching so that isn't it. I'm really stumped. I bolded the text of the regex and the error message I keep getting.

Thanks,

Pam

# This script allows full batch imports. To use the script you need to name the input .zip

# file with the SIS ID of the destination term in Canvas. The SIS ID is case sensitive!

$sourcePath = "C:\Canvas\zip_upload\" #this is source directory literal path

$archivePath = "C:\Canvas\zip_upload_archive\" #output path for the archive file creation

$logPath = "C:\Canvas\zip_upload_log\" #output path for the zip file creation

$token = "" # access_token

$domain = ".instructure.com"

$termMatchRegex = "^\d{4}(FA|SP|SU)$" # Regex To validate the zip filenames match your Term SIS ID pattern. This example allows SIS IDs in the format of Spring

$checkTimeInterval = 60 # Time interval (in seconds) to pause between import status checks

#################################################

###### Don't edit anything after this line ######

#################################################

# Just in caseset paths don't end with a \, add it.

if(!($sourcePath.EndsWith('\'))){

    $sourcePath += "\"

    Write-Host "Your sourceDir didn't end with a \ so I added one.  It really is important"

}

if(!($archivePath.EndsWith('\'))){

    $archivePath += "\"

    Write-Host "Your archivePath didn't end with a \ so I added one.  It really is important"

}

if(!($logPath.EndsWith('\'))){

    $logPath += "\"

    Write-Host "Your logPath didn't end with a \ so I added one.  It really is important"

}

$contentType = "application/zip" # don't change

$t = get-date -uformat %Y%m%d-%H%M

$file_list = dir $sourcePath*.zip

if($file_list.length -gt 0){

    foreach($file in $file_list){

        Write-Host "Processing $file..."

        $term = $file.name.Substring(0, $file.name.length - 4)

        if($term -cnotmatch $termMatchRegex){

            Write-Host "   Invalid SIS ID in file name ($term)! Does not match expected format!"

            continue

        }

        $inFile = $file.FullName

        $url = "https://$domain/api/v1/accounts/self/sis_imports.json?import_type=instructure_csv&batch_mode=1&batch..."+$term

        $headers = @{"Authorization"="Bearer "+$token}

        ###### Some functions

        $status_log_path = $logPath+$term+"-"+$t+"-status.log"

        write-host $status_log_path

        Try {

            $results1 = (Invoke-WebRequest -Headers $headers -InFile $inFile -Method POST -ContentType $contentType -Uri $url) #-PassThru -OutFile $outputPath$t"-status.log"

            $results1.Content | Out-File $status_log_path

            $results = ($results1.Content | ConvertFrom-Json)

            Write-Host $results

        } Catch [system.exception]{

            $results = $null

        }

       

        $results.id | Out-String

       

        if($results -eq $null){

            write-host "   Unable to start an SIS import for this term. Please ensure a term with this SIS ID has already been created in Canvas. (SIS ID's are case sensative)"

            continue

        }

        $status_msg = "Import created with ID "+$results.id

        write-host "   $status_msg"

        do{

          Start-Sleep -s $checkTimeInterval

          Try {

              $status_url = "https://$domain/api/v1/accounts/self/sis_imports/"+$results.id

              $results1 = (Invoke-WebRequest -Headers $headers -Method GET -Uri $status_url) #-PassThru -OutFile $outputPath$t"-status.log"

              $results1.Content | Out-File -Append $status_log_path

              $results = ($results1.Content | ConvertFrom-Json)

              if($results.workflow_state -eq "Importing"){

                $status_msg = "Importing: "+$results.progress+"%"

                write-host "   $status_msg"

              }

          } Catch [system.exception] {

              $results = $null

          }

          ####Add workflow status output and error

          #$results.id | Out-String

         if($results -eq $null){

            write-host "   Unable to retreive results for the SIS import."

            break

         }

        }

        while($results.progress -lt 100 -and $results.workflow_state -ne "failed_with_messages")

        $results1.Content | Out-File -Append $status_log_path

        $status_msg = "   Results:`n      Workflow State: "+$results.workflow_state+"`n      Progress: "+$results.progress+"%`n`n"

        write-host "$status_msg"

        # The sis import is done, you might do something else here like trigger course copies

        $moveTo = $archivePath+$t+"-"+$file.name

        Move-Item -Force $inFile $moveTo

    }

}

First of all, I'm not a programmer but I do like little puzzles like this if no one else is chiming in. So I pulled yours up and compared it to mine (because we also use powershell to to our imports) and I noticed that your url is different than ours. Obviously our sis_term_ids don't match, but your url has "self" where mine specifies a specific account ID. I don't think that matters because they both return the same object...

Did you try hard-coding the "2016SP" value into the URL to see if that worked? That would narrow the problem down to something weird with the term variable.

It appears that the error message you are getting happens if results is null so that error message might not actually have anything to do with your terms because it's going to give that message if anything goes wrong. At least, that is how I'm reading it. Are you using an older or newer version of this file than the one on the Github? My version has some of the code that you are using commented out so I'm wondering if you are using an older version that might do some things differently. Here a link to the Github version: canvas-contrib/FullBatch.ps1 at master · kajigga/canvas-contrib · GitHub

Hi Matt,

I used the exact same script that another college has used successfully, with my token. I agree that it might be something other than the term itself but don't have a clue how to troubleshoot it. I'll check the version you linked to but that is where I got mine. I did try it before using "1" instead of self but it got the same error.

Thanks,

Pam

akkaufmann
Learner II

Hi Pam,

 @MattHanes ​ is right in that the code will display that error message if something goes wrong when during the POST request.

In order to get a more meaningful error message, add something like this (changes in bold😞

        ###### Some functions

        $status_log_path = $logPath+$term+"-"+$t+"-status.log"

        write-host $status_log_path

        Try {

            $results1 = (Invoke-WebRequest -Headers $headers -InFile $inFile -Method POST -ContentType $contentType -Uri $url) #-PassThru -OutFile $outputPath$t"-status.log"

            $results1.Content | Out-File $status_log_path

            $results = ($results1.Content | ConvertFrom-Json)

            Write-Host $results

        } Catch [system.exception]{

            $results = $null

            Write-Host $_.Exception

        }

I should note that I don't use PowerShell very often, so I'm not 100% sure this is correct, but based on some research, this should output the error encountered.

Alex

Thanks, Alex, that's good to know. The issue is that I had version 2 of Powershell, not version 3! The script is using commands that are new in version 3. With version 3, it is running!

New question: The current script is looking for a zip file. Does anyone know how it could be modified to work directly with .csv files? My extract process creates the .csv files but doesn't zip them.

Thanks for your help, everyone!

View solution in original post

I just noticed that you had added a new question to your last post. To get more visibility (and hopefully a response), you would want to create a new question for this, "New question: The current script is looking for a zip file. Does anyone know how it could be modified to work directly with .csv files? My extract process creates the .csv files but doesn't zip them."

Hi Kona,

I realized that after I did it, and I did create a new question for it - and thereafter answered it myself!

Thanks,

Pam