Schedules are not working

Product: PowerShell Universal
Version: 1.4.6

Hi,
I just start working with PowerShell Universal and have some problems. I add simple script (read childitem from path provided in param and export that data to csv). When I start that script in Script it’s working correctly, but when I schedule it in Schedules it return Error “Data at the root level is invalid. Line 1, position 1.” . Any idea what casue that?

Thanks in advance

I’m also having this problem when attempting to schedule a script that otherwise runs fine when I run it from the script. If I try to run it from the schedule, I get the same error.

Data at the root level is invalid. Line 1, position 1.

Fairly clean install on 3.5.1

Did you ever figure this out?

Can either of you post a sanitized version of the script? Or at least the first few lines?

So, for testing I wrote a Test script with just Write-Output "Testing..." and that works in a schedule.

Otherwise, here is most of my script, I have the bit commented out that does the sftp upload at the moment, was just trying to get this part of it scheduled and working first.

Again, when running the script, it completes without any issues. When running from the schedule, it fails with that error.

#Requires -Module AeriesApi, Posh-SSH

[CmdletBinding()]
param (
    [Parameter (Mandatory = $true)] [pscredential]$AeriesCreds,
    [Parameter (Mandatory = $true)] [string]$SchoolCode
)

$AeriesApi = @{
    URL = $AeriesCreds.UserName
    Certificate = $AeriesCreds.GetNetworkCredential().Password
}

Initialize-AeriesApi @AeriesApi

# Get StudentIDs with blank InactiveStatusCode
$Students = Get-AeriesStudent -SchoolCode $SchoolCode | where-object {[string]::IsNullOrEmpty($_.InactiveStatusCode)} | select-object -Property StudentID

# Get Contacts associated with active students
$Contacts = Get-AeriesContact -SchoolCode $SchoolCode | Where-Object {$Students.StudentID -contains $_.StudentID.ToString()}

$users = @{}

foreach ($contact in $contacts) {
    $email = $contact.EmailAddress.ToLower()
    if ($contact.MiscCode -match 'P[1-2]' -and
    ![string]::IsNullOrEmpty($email) -and
    !$users.ContainsKey($email)) {
        $users[$email] = [PSCustomObject]@{
            user_id = $email
            login_id = $email
            first_name = $contact.FirstName
            last_name = $contact.LastName
            email = $email
            declared_user_type = 'observer'
            status = 'active'
            Students = @("STU_$($contact.StudentID)")
        }
    } elseif ($users.ContainsKey($email)) {
        if ($users[$email].Students -notcontains "STU_$($contact.StudentID)") {
            $users[$email].Students += "STU_$($contact.StudentID)"
        }
    }
}

$userObservers = @()
foreach ($user in $users.Values) {
    foreach ($student in $user.Students) {
        $userObservers += [PSCustomObject]@{
            observer_id = $user.email
            student_id = $student
            status = 'active'
        }
    }
}


$users.Values | Select-Object -Property user_id, login_id, first_name, last_name, email, declared_user_type, status | export-csv -Path .\WorkingTemp\users.csv -Force
$userObservers | export-csv -Path .\WorkingTemp\user_observers.csv -Force

If I comment out the param block, it appears to run and fail later due to the missing variables. Maybe it’s having an issue grabbing the pscreds secret variable or something?

Ah, ha!

image

I’ll open an issue for this.

1 Like

Any news?

This was resolved in 3.5.2. Sorry about not following up.

Do you mean 3.5.3? I was having the issue in 3.5.2 last week.

Hmmm. Now I’m not really sure. I really thought it was 3.5.2 but I must have been mistaken. I just retested this on 3.5.4 and it is working for me. I used the same parameter block and passed in the credentials.

After seeing your earlier post, I can confirm it was working for me on 3.5.3. So regardless, I think we’re good now!