2

An array returned from a function has an unexpected element appended to it, and I cannot understand how to fix it. A help will be so much appreciated.

The array in function A, for some unknown reason is different to the array in function B that calls the function A.

function functionA(...)
{
[...]
    $status =  (,@("reserved",$proj,$id));
    Write-Host "Status in FunctionA to FunctionB: "$status
[...]
return $status
}

I get from the Write-Host above: Status in FunctionA to FunctionB: reserved B hfuhfkhec8u8b7hf4smeu43gn4

function functionB(...)
{
[...]
    $funcA = functionA
    Write-Host "Status in FunctionB from FunctionA: "$funcA
[...]
}

I get from the Write-Host above: Status in FunctionB from FunctionA: 1 reserved B hfuhfkhec8u8b7hf4smeu43gn4. You can observe the $status in functionA has not the value 1, so why I get the value 1 appended in the array? What may I do to fix it?

Please check the complete code bellow, where FunctionA is CheckReservation and FunctionB is PeriodicCheckIn

PS: I noted that if I comment the function InformationMsgBox to do not call if in CheckReservation, then the code works.

function CheckReservation($headers){
$events = GetCalendarEvents($headers)
$users = Get-IniFile ..\ini\Users.ini
$user = $users.$env:UserName.email

$error = "There is not any confirmed reservation for this machine."; $status = (,@("NoReservations",$error));

$calendarId = Get-IniFile ..\ini\Computers.ini
$calendarId = $calendarId.$env:Computername.calendarId

foreach($item in $events.items)
{ 
    if($item.status -match "confirmed")
    {
        $attender = $item.attendees.email |
            Where-Object {$_ -contains $user}
        $calendar = $item.attendees |
            Where-Object {$_.email -contains $calendarId}
        $calendarEmail = $calendar.email            
        $calendarStatus = $calendar.responseStatus

        $organizer = $item.organizer.email | Where-Object {$_ -contains $user}

        if(($attender -match $user) -or ($item.organizer.email -match $user))
        {
            if(($calendarStatus -match "accepted") -or ($item.organizer.email -match $calendarId))
            {
                $current = [Xml.XmlConvert]::ToString((get-date),[Xml.XmlDateTimeSerializationMode]::Local)
                if(((get-date $current) -ge (get-date $item.start.dateTime)) -and ((get-date 

$current) -lt (get-date $item.end.dateTime)))
                    {
                        $timespan = NEW-TIMESPAN –Start (get-date $current) –End (get-date $item.end.dateTime);
                        $timeexpire = [int]$timespan.TotalMinutes;
                        Write-Host "Minutes to expire reservation: "$timeexpire;
                        $Timers = Get-IniFile ..\ini\Timers.ini;
                        $WarningBeforeExp = $Timers.Timers.WarningBeforeExp_min;   
                        if($timeexpire -le $WarningBeforeExp)
                        {
                            $msg = message(21);
                            Write-Host $msg;
                            InformationMsgBox $msg 10;
                        }
                        $proj=$item.summary
                        $id=$item.id
                        $status =  (,@("reserved",$proj,$id));
                        Write-Host "Status in FunctionA to FunctionB: "$status
                    } else { $msg = "Reservation expired or schedule time mismatch."; $status = (,@("outOfTime",$msg)); }
                }
            } else { $msg = "You are not an attender for the current reservation for this machine."; $status = (,@("IsNotAttender",$msg));}
        } else { $msg = "There is not any confirmed reservation for this machine. You can use it until someone makes a reservation."; $status = (,@("NoReservations",$msg));}
        }
        return $status
    }


function PeriodicCheckIn ($OAuth2){
    $checkin = $false

    $Timers = Get-IniFile ..\ini\Timers.ini
    $CheckInPeriodicity = $Timers.Timers.CheckInPeriodicity_min

    for($i=1;;$i++)
    {
       $timeout = IdleTimeout
       if(-not $timeout)
           {
               $funcA = CheckReservation $OAuth2 
               Write-Host "Status in FunctionB from FunctionA: "$funcA

               $reservation = $funcA 
                if ($reservation[0] -match "reserved")
                {
                    $project = $reservation[1]
                    $id = $reservation[2]
                    Write-Host "Reservation found"
                    Write-Host "Project: "$project
                    Write-Host "Id: "$id

                    if(-not $checkin)
                    {
                        storageData "CheckInOut" $OAuth2 "CheckIn" $project "" $false
                        $checkin = $true                 
                        $msg = message(15)
                        Write-Host $msg
                        InformationMsgBox $msg -1
                    }
                    else
                    {
                        storageData "updatetime" $OAuth2 "LastUpdate" $project "" $false
                    }                
                }
                elseif($i -eq 1) 
                {
                    Write-Host "Reservation not found"
                    $Availability = CheckAvailability $OAuth2 "10"
                    Write-Host "Availability for now: "$Availability

                    if($Availability -match "Yes")
                    {
                        $msg = message(16)
                        $msgBoxInput =  QuestionMsgBox $msg 30
                        if($msgBoxInput -eq 6)
                        {
                            $project = GetProject "FromUser"
                            CreateReservation $OAuth2 $project "10"
                            storageData "CheckInOut" $OAuth2 "CheckIn" $project "" $false
                            $checkin = $true 

                            $msg = message(15)
                            Write-Host $msg
                            InformationMsgBox $msg -1

                        }
                        else
                        {
                            $leave = $true;
                        }
                    } else
                    {
                        $leave = $true;
                    }
                }
                else
                {
                    Write-Host "O pau é aqui?1 $reservation[1]"
                    $msg = message(18, $reservation[1]);
                    Write-Host "O pau é aqui?2 $reservation[1]"
                    StopMsgBox $msg -60
                    $leave = $true;   
                }
            } else {$leave = $true;}

            if($leave -eq $true){ return $true}

            Write-Host "CheckIn $i"
            start-sleep -Seconds ([double]$CheckInPeriodicity*60)
        }    
    }
function message($index,$txt){
    $LastWarning_min = Get-IniFile ..\ini\Timers.ini
    $LastWarning_min = $LastWarning_min.Timers.LastWarning_min/60 
    $ManualAuthTimeout_min = $LastWarning_min.Timers.ManualAuthTimeout_min

    Write-Host "Next message index: $index"

    $arrMessage = 
        "[MSG000] It is not possible to identify if this current PC is Workstation or Buildstation machine.",
        "[MSG001] Shutting down the system.",
        "[MSG002] You do not have approriate access to this function. Get in contact with lab support.",
        "[MSG003] An error occurred. Verify if there are at least two not empty .txt file, if the path is correct or if you have write permission to the path $txt.",
        "[MSG004] Attempt to delete a folder that does not exist: $txt.",
        "[MSG005] Mapping failed. Make sure your connection can reach the networking that remote path ""$txt"" belongs.",
        "[MSG006] Team not specified for the user $env:UserName. Please make sure the user is registered.",
        "[MSG007] Connection failed. Make sure the PC can reach FIATAUTO networking then try again.",
        "[MSG008] Error while Import-Module.",
        "[MSG009] Error on OAuth 2.0 for Google APIs Authorization.",
        "[MSG010] Error on Method: spreadsheets.values.get (writing) for Google Sheets API request.",
        "[MSG011] Error on Method: spreadsheets.values.get (reading) for Google Sheets API request.",
        "[MSG012] Error on Method: scripts.run for Google App Script API request.",
        "[MSG013] Error on Events: get for Google Calendar API request.",
        "[MSG014] Error on Events: list for Google Calendar API request.",
        "[MSG015] Your access has been granted to work on the project: $global:project.",
        "[MSG016] No reservation was found. Would you like to make a short reservation?",
        "[MSG017] Permission to shared drives mismatch. Select OK to proceed for manual authorization within $ManualAuthTimeout_min minutes or select CANCEL for Windows logoff now.",
        "[MSG018] No valid reservation found. $txt",
        "[MSG019] Inactive time exceeded the inactivity limit. Select OK within 60 seconds to avoid the current Windows session logoff.",
        "[MSG020] Save your files now. Shutting down the system within $LastWarning_min seconds.",
        "[MSG021] Your reservation is close to expire!"

    return $arrMessage[$index]
}

function InformationMsgBox($msg, $msg_timeout){
    $sh = New-Object -ComObject "Wscript.Shell"
    return $sh.Popup($msg,$msg_timeout,$global:AppTitle,1+64)
}

1 Answer 1

2

As you have noted yourself, the InformationMsgBox is to blame. Every command returning output gets added to the pipeline.

return $sh.Popup($msg,$msg_timeout,$global:AppTitle,1+64) returns the 1 you see added to your array. You can suppress it's output by sending it to Out-Null.

function InformationMsgBox($msg, $msg_timeout){
    $sh = New-Object -ComObject "Wscript.Shell"
    return $sh.Popup($msg,$msg_timeout,$global:AppTitle,1+64) | Out-Null
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.