0
votes

I'm trying to license my users in office365 with a powershell script in tassk scheduler.

First : I have a script to create some users in my domain controller. This script add content (userprincipalname) in a txt file. Example : - Create user : John Smith - The script add this in the txt file : [email protected]

Second : Another script try to license this user. This script are connecting to MSOLService with this cmdlets :

$LOGIN = "[email protected]"
$MDP = Get-Content "C:\Script\SVC-365.txt" | ConvertTo-SecureString
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $LOGIN,$MDP
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Connect-MsolService -Credential $UserCredential
Import-PSSession $Session -AllowClobber

$User = Get-Content "C:\Script\CreationMail.txt"

foreach ($i in $User) {
    Set-MsolUser -UserPrincipalName $i -UsageLocation "FR"
    Set-MsolUserLicense -UserPrincipalName $i -AddLicenses "tenantudl:STANDARDWOFFPACK_FACULTY"
}

This script is perfectly working when I manually execute in powershell ISE, but not working in the task scheduler...

If somebody can help me, I will be grateful to him ! :) Thanks for help !

3
"not working in the task scheduler" - what does this actually mean? Please be specific. - Maciej Jureczko
What user is used for running the task? Is this on the SAME machine? Are there any erors to show? You must store the password for this user in the text file while actualy logged-in as this person. - Theo
AdminDomain run the task. Svc-365 (365 admin) add the license. His password is stored in the txt file. - Ithuriel
See Henrik Stanley Mortensen answer. The user running the task must be the one that created the password file. In your case: log in as Svc-365 -> Create the password file -> let user Svc-365 run the task - Theo

3 Answers

0
votes

Which user runs the script in your task?
With credentials that you have saved as a hash value in a text file it can only be decrypted by the user that made the file.

So if you made that file with your user, but the user that is setup to run the task is not the same, say a service account, it cannot decrypt the password and the login will fail.

As a note, I would highly recommend Microsofts new feature to assign licenses by group membership instead of doing it by scripts as Microsoft changes things an scripts break.

It is in preview right now and require an AD Basic license on your account or higher, you can activate a free trial of the EMS license pack in Azure and and this will be enough to activate the feature.

0
votes

For TaskScheduler scripts, you need to set it up correctly.

  1. Set the "Program/Scritp" to Powershell.exe
  2. Set the "Add arguments" to -ExecutionPolicy Bypass C:\Temp\AddLicence.ps1
  3. (Optional) If your script requires any params then you will need to add them and your "Add Arguments" field ends up as -ExecutionPolicy Bypass C:\Temp\AddLicence.ps1 -Users C:\temp\users.csv -LicenceType C:\temp\O365.txt
0
votes

I find the problem ! :D

In my task, it was domain\administrator who run the task but this account havn't the right to connect to office365 and he havn't the admin right on Exchange-online. I was changed this account with another who have the right on 365 and it's WORK !! :D

Thank's for help guys !