返回的数组在Powershell中具有意外元素

时间:2020-05-12 03:34:45

标签: arrays powershell debugging

从函数返回的数组附加了一个意外元素,我不知道如何修复它。非常感谢您的帮助。

由于未知原因,函数A中的数组与调用函数A的函数B中的数组不同。

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

我来自上面的Write-HostStatus in FunctionA to FunctionB: reserved B hfuhfkhec8u8b7hf4smeu43gn4

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

我来自上面的Write-HostStatus in FunctionB from FunctionA: 1 reserved B hfuhfkhec8u8b7hf4smeu43gn4。您可以观察到$status中的functionA没有值1,那么为什么我将值1附加在数组中呢?我该怎么解决?

请检查以下完整代码,其中FunctionACheckReservation,而FunctionBPeriodicCheckIn

PS:我注意到,如果我将函数InformationMsgBox注释为在CheckReservation中不调用,则代码可以正常工作。

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 个答案:

答案 0 :(得分:1)

您已经注意到自己,应该InformationMsgBox来指责。每个返回输出的命令都会添加到管道中。

return $sh.Popup($msg,$msg_timeout,$global:AppTitle,1+64)返回您看到添加到数组中的1。您可以通过将其发送到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
}
相关问题