Search code examples
powershellpowershell-remoting

schtasks output is in different encoding whether it runs locally or in remote session


I'm trying to check the schedule tasks on my servers, and I would like to use background-jobs and remote sessions to speed up the execution of the script. But when i run my command in the remote session, the output is in a different culture ( or encoding ?) than onto my workstation. I tried to use New-PsSessionOption but the result is the same :

  1. locally run schtasks to check a remote server, output is OK :

    PS>Get-Culture
    
    LCID             Name             DisplayName
    ----             ----             -----------
    1036             fr-FR            French (France)
    
    PS>schtasks -s servername /v /query /fo csv |ConvertFrom-Csv |select -first 1
    
    
    Nom de l'hôte                                             : servername
    Nom de la tâche                                           : \BITS_CCM_Incoming_{16
                                                                674A29-EDDD-43C3-9EF2-
                                                                2B2D64EFA6F5}
    Prochaine exécution                                       : 23/11/2012 22:38:00
    Statut                                                    : Prêt
    
  2. Open a remote session an run schtasks locally :

    PS>$pso = New-PSSessionOption -Culture "fr-fr"
    PS>etsn servername -SessionOption $pso
    [servername]: PS C:\> get-culture
    
    LCID             Name             DisplayName
    ----             ----             -----------
    1036             fr-FR            Français (France)
    [servername]: PS C:\> schtasks /query /v /fo csv |ConvertFrom-Csv |select -First 1
    
    
    Nom de l'h"te                                             : servername
    Nom de la tƒche                                           :     \BITS_CCM_Incoming_{16674A29-EDDD-43C3-9EF2-2B2D64EFA6F5}
    Prochaine ex'cution                                       : 23/11/2012 22:38:00
    Statut                                                    : Pr^t
    

as you can see deespite the same culture is used the output seems to be of different encondings. How to deal with that ?


Solution

  • After a while, I've found that I can use the COM object 'Schedule.Service' to retrieve tasks info wich is not language dependant :

    icm -AsJob -JobName getTasks -ComputerName $servers -ScriptBlock{
        $Schedule = new-object -com("Schedule.Service")
        $Schedule.connect($env:computername)
        $Tasks = $Schedule.getfolder("\").gettasks(0)
        $Tasks | Select-Object  Name,Path,State,Enabled,LastRunTime,LastTaskResult
    }
    
    $resu=wait-job getTasks |receive-job
    remove-Job getTasks
    
    $resu|sort PSComputerName