locked
Connect-MSOLService : Not connecting as scheduled task. RRS feed

  • Question

  • Hello Everyone!

    Having issues running a script as a scheduled task. Running through powershell directly, there are no issues. 

    When it gets to the point to Connect-MSOLService, it is showing this : 

    Connect-MsolService : The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, 
    or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
    again.

    I am also importing it as a module, but it is having some issues. 

    I have set up the schedule task as Microsoft has recommended on this webpage, specifically with the arguments. 

    https://social.technet.microsoft.com/wiki/contents/articles/32656.office-365-how-to-schedule-a-script-using-task-scheduler.aspx

    Any help or ideas on getting it connected as a scheduled task would be great. Thank you!

    Just to add a bit more details, 

    This is how I am storing my credentials: 

    #Credential
    $username = 'USERNAME'
    $Password = ConvertTo-SecureString 'PASSWORD' -AsPlainText -Force
    $UserCredential = new-object -typename System.Management.Automation.PSCredential `
             -argumentlist $username, $password

    When I am trying to connect to MSOL : 

    Connect-MsolService -Credential $Usercredential



    Friday, January 5, 2018 3:50 PM

Answers

  • Here is a far easier way to schedule a PowerShell task/job.

    $sb = {
        $username = 'userid@domain.net'
        $Password = ConvertTo-SecureString 'MyPswWrd' -AsPlainText -Force
        $creds = new-object System.Management.Automation.PSCredential($username, $password)
        Connect-MsolService -Credential $creds
        Get-MsolUser
    }
    $trigger = @{ Frequency = Weekly; At = '9:00PM'; DaysOfWeek = 'Monday'; Interval = 2 }
    $j = Register-ScheduledJob -ScriptBlock $sb -Trigger $t

    See: https://docs.microsoft.com/en-us/powershell/module/psscheduledjob/about/about_scheduled_jobs?view=powershell-5.1

    This job will run as scheduled under the task manager and will run even if PS is not running or you log off.  It is the preferred way to schedule tasks from  PowerShell.  You can retrieve the current results and status at any PowerShell prompt as the job can be seen from all sessions.

    Get-Job will see the task and Receive-Job will return the output.

    It also works correctly with MsOl



    \_(ツ)_/


    Saturday, January 6, 2018 4:05 PM

All replies

  • Hi,

    Powershell script can be run from Schedule task. Just specify Powershell.exe as application and pass .ps1 (powershell file) as argument.

    Secondly, verify following settings for further troubleshoot

    1) Make sure you are using appropriate credential on task schedular. Change User Or Group if require.

    2) Try to run task with "Run with highest privileges" checked

    3) check task history to get more details if task is failing.

    Thanks

    Saurabh


    Please remember to mark the solution as the answer using "Mark as Answer". If you find a solution to be helpful, please use "Vote as Helpful".

    Saturday, January 6, 2018 3:39 PM
  • Here is a far easier way to schedule a PowerShell task/job.

    $sb = {
        $username = 'userid@domain.net'
        $Password = ConvertTo-SecureString 'MyPswWrd' -AsPlainText -Force
        $creds = new-object System.Management.Automation.PSCredential($username, $password)
        Connect-MsolService -Credential $creds
        Get-MsolUser
    }
    $trigger = @{ Frequency = Weekly; At = '9:00PM'; DaysOfWeek = 'Monday'; Interval = 2 }
    $j = Register-ScheduledJob -ScriptBlock $sb -Trigger $t

    See: https://docs.microsoft.com/en-us/powershell/module/psscheduledjob/about/about_scheduled_jobs?view=powershell-5.1

    This job will run as scheduled under the task manager and will run even if PS is not running or you log off.  It is the preferred way to schedule tasks from  PowerShell.  You can retrieve the current results and status at any PowerShell prompt as the job can be seen from all sessions.

    Get-Job will see the task and Receive-Job will return the output.

    It also works correctly with MsOl



    \_(ツ)_/


    Saturday, January 6, 2018 4:05 PM