PowerShell Module

PowerShell Module

by Matt Hamilton -
Number of replies: 8

Hi gang,

Thought I'd post here since if you're using web services you might also be a PowerShell wonk like me.

I've created a Moodle module that uses the web services. We've been using it in production for the better part of this year with no issues. We use it to provision new users and enrol them in courses.

The module is open source and I'm happy to take suggestions (or pull requests) if there's functionality you need! You can find it on GitHub here: https://github.com/WodongaTAFE/PSMoodle and install it from the PowerShell Gallery using Install-Module.

Cheers,
Matt

Average of ratings: -
In reply to Matt Hamilton

Re: PowerShell Module

by Pieterjan Heyse -

Hi Matt, 

I'm a total novice with regards to Powershell and am currently fighting with powershell and the Moodle API to automatically provision a part of our Moodle installation. 

I have researched the API to find all the url's I need to make to do everythin I want, but am now thinking on how to proceed. I think there are 2 options, but maybe you can advise me on how to proceed? 

1) Use Invoke-restmethod to create everything manually

2) Expand on your modules

The second is much harder for me I think, what would your advise be to a novice powershell user? 

Thanks, 

Pieterjan Heyse

In reply to Pieterjan Heyse

Re: PowerShell Module

by Matt Hamilton -
Happy to help! What part of the API that you use is missing from the PowerShell module? Maybe I can build something out. Feel free to log it as an "issue" on GitHub too!
In reply to Matt Hamilton

Re: PowerShell Module

by Pieterjan Heyse -
These are the calls I currently need and hacked some code together for:

core_course_create_categories
core_course_create_courses
core_group_create_groups
core_cohort_search_cohorts
local_ws_enrolcohort_add_instance
enrol_manual_enrol_users
core_course_import_course

By hacking I mean that I'm doing something like this for those calls:

$body['categories[0][name]'] = 'Test'
$body['categories[0][parent]'] = 0
$resp = MoodleApiCall "core_course_create_categories" $body

The main issue is that I had to install and extra plugin for the local_ws_enrolcohort_add_instance call because I could not find a way to add a cohort to a course in the default API.

Am I correct that only these are not supported in your module?

core_group_create_groups
local_ws_enrolcohort_add_instance
core_course_import_course


I'll log my request to GitHub, maybe other people have this kind of question, too?
In reply to Pieterjan Heyse

Re: PowerShell Module

by Matt Hamilton -
Yep you're correct - I haven't covered groups at all, and I've only got courses covered in terms of searching rather than creating. Good excuse to build it out a bit!

The local_ws one I'm not sure about. I kinda want to make sure I'm covering the core endpoints before I worry too much about plugins. Isn't there a built-in way to enrol a cohort into a course (i.e. as soon as a user is added to a cohort they get enrolled in the course)? I'm reasonably new to Moodle but I remember seeing that somewhere.
In reply to Matt Hamilton

Re: PowerShell Module

by Pieterjan Heyse -
I'm afraid not. In the UI you can add a cohort to a course and at that time ask Moodle to create a group in that course with the same name. The API does not provide this functionality and I think I had to add this plugin to be able to do this with the API. It's a good excuse to learn git, more Powershell and use my github account to upload the code I'll try to make to extend your functionality.

I'll get in touch if I have it working to maybe integrate this back in your code? I don't know how this would go, but this seems like a nice idea. Totally new to all this stuff...

Thanks,
Pieterjan
In reply to Pieterjan Heyse

Re: PowerShell Module

by Matt Hamilton -
Ok this morning I've added a bunch of new functions for you.

Get-MoodleGroup
Get-MoodleGroupMember
New-MoodleGroup
Set-MoodleGroup
Remove-MoodleGroup
New-MoodleCourseCategory
Set-MoodleCourseCategory
Remove-MoodleCourseCategory
Copy-MoodleCourse

That'll cover off a bunch of your requirements. I've left some comments on your GitHub issue as well.
In reply to Matt Hamilton

Re: PowerShell Module

by GK Chesterton -

This looks great!  I'm just wondering, is there any way that as an ordinary user I could possible use PowerShell in order to do routine tasks in the command line rather than the GUI e.g. download turnitin assignments, upload files, log in, remove content and so on. Or am I living in a make-believe-dream-land? smile


In reply to GK Chesterton

Re: PowerShell Module

by Matt Hamilton -
Good question! I'm not sure what web service endpoints there are for student activities, but if they exist I'm sure we could make cmdlets for them. The permissions model is pretty robust - I've noticed that if I try to use New- or Set- cmdlets when connected to the Moodle sandbox it won't work, so it's definitely possible to lock a user out from certain functionality.
Average of ratings: Useful (1)