cancel
Showing results for 
Search instead for 
Did you mean: 
steve_rowbottom
Learner II

Powershell SIS Import Error

We have successfully used a PowerShell script to zip up a set of csv files and send them to Canvas via the API.  Recently (actually it has been an issue for a while but I have been too busy to investigate!), the script has stopped working and I am receiving the following error:

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At <file>:32 char:14
+ ... results1 = (Invoke-WebRequest -Headers $headers -InFile $InFile -Meth ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

ConvertFrom-Json : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Canvas_SIS\Canvas_Zip_Import.ps1:34 char:33
+ $results = ($results1.Content | ConvertFrom-Json)
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFrom
JsonCommand

A copy of the script I am using, with the token and other identifiers removed is below.  Any suggestions as to how to resolve the issue would be resolved as manually importing the zip file that the script is producing (containing the csv files generated by another process) is getting a little tiresome!

Many thanks

Steve

Script:

$sourcePath = "O:\Canvas\Input\" #this is source directory literal path
$outputPath = "O:\Canvas\Output\" #output path for the zip file creation
$account_id = "1" #root account ID of Canvas, usually the number 1
$token = "<Token REMOVED>" # access_token
$domain = "<School REMOVED>.instructure.com"
$outputZip = "canvas_import.zip" # name of the zip file to create

#################################################
###### Don't edit anything after this line ######
#################################################
$url = "https://henley.instructure.com/api/v1/accounts/1/sis_imports.json?import_type=instructure_csv"
$headers = @{"Authorization"="Bearer "+$token}

# Just in case $sourcePath doesn't end with a \, add it.
if(!($sourcePath.EndsWith('\'))){
$sourcePath += "\"
Write-Host "You sourcePath didn't end with a \ so I added one. It really is important"
}
if($outputZip.Contains('\')){
Write-Host "The outputZip should not contain backslashes. You are warned"
}

###### Some functions

$contentType = "application/zip" # don't change
$InFile = $outputPath+$outputZip # don't change
Write-zip -Path $sourcePath"*.csv" -OutputPath $InFile

$t = get-date -format M_d_y_h
$status_log_path = $outputPath+$t+"-status.log"
$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 $status_line
Start-Sleep -s 60
$status_url = "https://henley.instructure.com/api/v1/accounts/1/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)
$results1.Content | Out-File -Append $status_log_path

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

Move-Item -Force $outputPath$outputZip $outputPath$t-$outputZip
Remove-Item $sourcePath*.csv

Labels (3)
0 Kudos
0 Replies