IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Scripts/Batch Discussion :

Integration Progressbar


Sujet :

Scripts/Batch

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    62
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 62
    Par défaut Integration Progressbar
    Bonjour,

    J'ai adapté à mon utilisation un script trouvé sur le net

    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    $destination = "C:\Backup"
     
    $folder = "Desktop",
    "Downloads",
    "Favorites",
    "Documents",
    "Music",
    "Pictures",
    "Videos",
    "AppData\Local\Mozilla",
    "AppData\Local\Google",
    "AppData\Roaming\Mozilla"
     
    ###############################################################################################################
     
     
     
    $username = gc env:username
    $userprofile = gc env:userprofile
    $appData = gc env:localAPPDATA
     
     
    ###### Restore data section ######
    if ([IO.Directory]::Exists($destination + "\" + $username + "\")) 
    { 
     
        $OUTPUT= [System.Windows.Forms.MessageBox]::Show("Le dossier backup pour $username existe, on Restaure ?" , "Restauration" , 4)
     
    	if ($OUTPUT -eq "YES") 
    	{
    		write-host -ForegroundColor green "Restauration des donnees de $username"
    		foreach ($f in $folder)
    		{	
    			$currentLocalFolder = $userprofile + "\" + $f
    			$currentRemoteFolder = $destination + "\" + $username + "\" + $f
    			$currentFolderSize = (Get-ChildItem -ErrorAction silentlyContinue $currentLocalFolder -Recurse -Force | Measure-Object -ErrorAction silentlyContinue -Property Length -Sum ).Sum / 1MB
    		    $currentFolderSizeRounded = [System.Math]::Round($currentFolderSize)
    		    write-host -ForegroundColor cyan "  $f... ($currentFolderSizeRounded MB)"
    		    Copy-Item -ErrorAction silentlyContinue -recurse $currentLocalFolder $currentRemoteFolder
     
    			if ($f -eq "AppData\Local\Mozilla") { rename-item $currentLocalFolder "$currentLocalFolder.old" }
    			if ($f -eq "AppData\Roaming\Mozilla") { rename-item $currentLocalFolder "$currentLocalFolder.old" }
    			if ($f -eq "AppData\Local\Google") { rename-item $currentLocalFolder "$currentLocalFolder.old" }
     
    		}
    		rename-item "$destination\$username" "$destination\$username.restore"
    		[System.Windows.Forms.MessageBox]::Show("Restauration complete!" , "KABA") 
    	}
     
    	else
    	{
    		write-host -ForegroundColor yellow "Annulation"
    		exit
    	}
     
     
    }
     
    ###### Backup Data section ########
    else 
    { 
     
    	$OUTPUT= [System.Windows.Forms.MessageBox]::Show("Pret pour le backup de $username ?" , "Backup" , 4)
     
    	if ($OUTPUT -eq "YES")
        {
     
    	Get-Process | Where { $_.Name -Eq "OUTLOOK" } | Kill
     
    	write-host -ForegroundColor green "Backup des donnees de $username"
     
    	foreach ($f in $folder)
    	{	
    		$currentLocalFolder = $userprofile + "\" + $f
    		$currentRemoteFolder = $destination + "\" + $username + "\" + $f
    		$currentFolderSize = (Get-ChildItem -ErrorAction silentlyContinue $currentLocalFolder -Recurse -Force | Measure-Object -ErrorAction silentlyContinue -Property Length -Sum ).Sum / 1MB
    		$currentFolderSizeRounded = [System.Math]::Round($currentFolderSize)
    		write-host -ForegroundColor cyan "  $f... ($currentFolderSizeRounded MB)"
    		Copy-Item -ErrorAction silentlyContinue -recurse $currentLocalFolder $currentRemoteFolder
    	}
     
     
     
    	$oldStylePST = [IO.Directory]::GetFiles($appData + "\Microsoft\Outlook", "*.pst") 
    	foreach($pst in $oldStylePST)	
    	{ 
    		if ((test-path -path ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle")) -eq 0){new-item -type directory -path ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle") | out-null}
    		write-host -ForegroundColor yellow "  $pst..."
    		Copy-Item $pst ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle")
    	}    
     
    	[System.Windows.Forms.MessageBox]::Show("Backup complet!" , "KABA") 
     
        else
    	{
    		write-host -ForegroundColor yellow "Annulation"
    		exit
    	}
    	}
     }

    J'aimerais pouvoir afficher lors de son exécution une barre de progression, mais je n'arrive pas à trouver sur le net ou exactement placer la ligne de code de la barre dans mon script et si la syntaxe est à conserver dans l'état

    Voici la ligne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    for ($i = 1; $i -lt 101; $i++ ){for ($j=0;$j -lt 10000;$j++) {} write-progress -activity "Search in Progress" -status "% Complete:" -percentcomplete $i;}
    Pourriez vous me renseigner ? Sachant que je découvre PS

    Merci d'avance, à demain

  2. #2
    Rédacteur


    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    7 171
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 7 171
    Billets dans le blog
    1
    Par défaut
    Salut,
    il existe l'aide en ligne :
    Code Powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    Get-Help Write-Progress -Full
     
    Get-Help Write-Progress -Examples|more
    La contrainte est qu'on doit connaitre le nombre d'élément avant d'exécuter la progression. Ce qui ne semble pas être le cas dans ton script.

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    62
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 62
    Par défaut
    Salut et merci pour ton aide

    En effet c'est tout à fait différent sur chaque machine sur lesquelles le script va être exécuté

    Tu connais un moyen différent de le faire sachant que c'est mon premier script PS ?

    Merci encore

  4. #4
    Rédacteur


    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    7 171
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 7 171
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par goro_830 Voir le message
    Tu connais un moyen différent de le faire sachant que c'est mon premier script PS ?
    Essaie de l'adapter avec qq chose comme :
    Code Powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
     #nombre de fichiers à traiter, contient aussi les répertoires
    $Files= Get-ChildItem -ErrorAction silentlyContinue $currentLocalFolder -Recurse -Force
      $Files.Count
     
       #taille totale des fichiers à traiter   
    $currentFolderSize = ($Files| Measure-Object -ErrorAction silentlyContinue -Property Length -Sum ).Sum / 1MB
    Ou simplement afficher la progression sur la collection folders, sans le détail, voir utiliser le paramètre -Verbose en mode brut
    Le pb est que le script copie des répertoires la progression ne peut donc se faire sur chaque fichier de chaque répertoire.

    Pour tes tests tu peux utiliser la paramètres -Whatif sur les cmdlets copy-item,rename-item...

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    62
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 62
    Par défaut
    Tu peux me corriger stp ?

    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    $destination = "C:\FLS2"
     
    $folder = "Desktop",
    "Downloads",
    "Favorites",
    "Documents",
    "Music",
    "Pictures",
    "Videos",
    "AppData\Local\Mozilla",
    "AppData\Local\Google",
    "AppData\Roaming\Mozilla"
     
    #####################################################################################
     
    $username = gc env:username
    $userprofile = gc env:userprofile
    $appData = gc env:localAPPDATA
     
    Add-Type -AssemblyName System.Windows.Forms
     
    #nombre de fichiers à traiter, contient aussi les répertoires
    $Files= Get-ChildItem -ErrorAction silentlyContinue $currentLocalFolder -Recurse -Force
      $Files.Count
    #taille totale des fichiers à traiter   
    $currentFolderSize = ($Files| Measure-Object -ErrorAction silentlyContinue -Property Length -Sum ).Sum / 1MB
     
    for ($i = 1; $i -lt 101; $i++ ){for ($j=0;$j -lt 10000;$j++) {} write-progress -activity "Copie en Cours..." -status "% Complete:" -percentcomplete $i;}
     
    ###### Restore data section ######
     
    if ([IO.Directory]::Exists($destination + "\" + $username + "\")) 
    {
        $OUTPUT= [System.Windows.Forms.MessageBox]::Show("Le dossier backup pour $username existe, on Restaure ?" , "Restauration" , 4)
     
    	if ($OUTPUT -eq "YES") 
    	{
    		write-host -ForegroundColor green "Restauration des donnees de $username"
    		foreach ($f in $folder)
    		{	
    			$currentLocalFolder = $userprofile + "\" + $f
    			$currentRemoteFolder = $destination + "\" + $username + "\" + $f
    			$currentFolderSize = (Get-ChildItem -ErrorAction silentlyContinue $currentLocalFolder -Recurse -Force | Measure-Object -ErrorAction silentlyContinue -Property Length -Sum ).Sum / 1MB
    		    $currentFolderSizeRounded = [System.Math]::Round($currentFolderSize)
    		    write-host -ForegroundColor cyan "  $f... ($currentFolderSizeRounded MB)"
    		    Copy-Item -ErrorAction silentlyContinue -recurse $currentLocalFolder $currentRemoteFolder
     
    			if ($f -eq "AppData\Local\Mozilla") { rename-item $currentLocalFolder "$currentLocalFolder.old" }
    			if ($f -eq "AppData\Roaming\Mozilla") { rename-item $currentLocalFolder "$currentLocalFolder.old" }
    			if ($f -eq "AppData\Local\Google") { rename-item $currentLocalFolder "$currentLocalFolder.old" }
     
    		}
    		rename-item "$destination\$username" "$destination\$username.restore"
    		[System.Windows.Forms.MessageBox]::Show("Restauration complete!" , "Restauration") 
    	}
     
    	else
    	{
    		write-host -ForegroundColor yellow "Annulation"
    		exit
    	}	
    }
     
    ###### Backup Data section ########
     
    else 
    { 
    	$OUTPUT= [System.Windows.Forms.MessageBox]::Show("Pret pour le backup de $username ?" , "Backup" , 4)
     
    	if ($OUTPUT -eq "YES")
        {
     
    	Get-Process | Where { $_.Name -Eq "OUTLOOK" } | Kill
     
    	write-host -ForegroundColor green "Backup des donnees de $username"
     
    	foreach ($f in $folder)
    	{	
    		$currentLocalFolder = $userprofile + "\" + $f
    		$currentRemoteFolder = $destination + "\" + $username + "\" + $f
    		$currentFolderSize = (Get-ChildItem -ErrorAction silentlyContinue $currentLocalFolder -Recurse -Force | Measure-Object -ErrorAction silentlyContinue -Property Length -Sum ).Sum / 1MB
    		$currentFolderSizeRounded = [System.Math]::Round($currentFolderSize)
    		write-host -ForegroundColor cyan "  $f... ($currentFolderSizeRounded MB)"
    		Copy-Item -ErrorAction silentlyContinue -recurse $currentLocalFolder $currentRemoteFolder
    	}
     
     
     
    	$oldStylePST = [IO.Directory]::GetFiles($appData + "\Microsoft\Outlook", "*.pst") 
    	foreach($pst in $oldStylePST)	
    	{ 
    		if ((test-path -path ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle")) -eq 0){new-item -type directory -path ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle") | out-null}
    		write-host -ForegroundColor yellow "  $pst..."
    		Copy-Item $pst ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle")
    	}    
     
    	    [System.Windows.Forms.MessageBox]::Show("Backup complet!" , "Sauvegarde") 
        }
     
        else
    	{
    		write-host -ForegroundColor yellow "Annulation"
    		exit
    	}
    }

  6. #6
    Rédacteur


    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    7 171
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 7 171
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par goro_830 Voir le message
    Tu peux me corriger stp ?
    Pas le temps, désolé.

  7. #7
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    62
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 62
    Par défaut
    Ok merci

  8. #8
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    62
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 62
    Par défaut
    Alors je reviens à vous pour ce problème d’intégration de progress bar, j'ai avance un peu mais je reste bloqué :

    J'ai trouvé un script de fonction qui m'a l'air de parfaitement correspondre à mes attentes https://keithga.wordpress.com/2014/0...mwithprogress/

    J'ai récupéré le script :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    <#
    .SYNOPSIS
    RoboCopy with PowerShell progress.
    
    .DESCRIPTION
    Performs file copy with RoboCopy. Output from RoboCopy is captured,
    parsed, and returned as Powershell native status and progress.
    
    .PARAMETER RobocopyArgs
    List of arguments passed directly to Robocopy.
    Must not conflict with defaults: /ndl /TEE /Bytes /NC /nfl /Log
    
    .OUTPUTS
    Returns an object with the status of final copy.
    REMINDER: Any error level below 8 can be considered a success by RoboCopy.
    
    .EXAMPLE
    C:\PS> .\Copy-ItemWithProgress c:\Src d:\Dest
    
    Copy the contents of the c:\Src directory to a directory d:\Dest
    Without the /e or /mir switch, only files from the root of c:\src are copied.
    
    .EXAMPLE
    C:\PS> .\Copy-ItemWithProgress '"c:\Src Files"' d:\Dest /mir /xf *.log -Verbose
    
    Copy the contents of the 'c:\Name with Space' directory to a directory d:\Dest
    /mir and /XF parameters are passed to robocopy, and script is run verbose
    
    .LINK
    https://keithga.wordpress.com/2014/06/23/copy-itemwithprogress
    
    .NOTES
    By Keith S. Garner (KeithGa@KeithGa.com) - 6/23/2014
    With inspiration by Trevor Sullivan @pcgeek86
    
    #>
    
    [CmdletBinding()]
    param(
    	[Parameter(Mandatory = $true,ValueFromRemainingArguments=$true)] 
    	[string[]] $RobocopyArgs
    )
    
    $ScanLog  = [IO.Path]::GetTempFileName()
    $RoboLog  = [IO.Path]::GetTempFileName()
    $ScanArgs = $RobocopyArgs + "/ndl /TEE /bytes /Log:$ScanLog /nfl /L".Split(" ")
    $RoboArgs = $RobocopyArgs + "/ndl /TEE /bytes /Log:$RoboLog /NC".Split(" ")
    
    # Launch Robocopy Processes
    write-verbose ("Robocopy Scan:`n" + ($ScanArgs -join " "))
    write-verbose ("Robocopy Full:`n" + ($RoboArgs -join " "))
    $ScanRun = start-process robocopy -PassThru -WindowStyle Hidden -ArgumentList $ScanArgs
    $RoboRun = start-process robocopy -PassThru -WindowStyle Hidden -ArgumentList $RoboArgs
    
    # Parse Robocopy "Scan" pass
    $ScanRun.WaitForExit()
    $LogData = get-content $ScanLog
    if ($ScanRun.ExitCode -ge 8)
    {
    	$LogData|out-string|Write-Error
    	throw "Robocopy $($ScanRun.ExitCode)"
    }
    $FileSize = [regex]::Match($LogData[-4],".+:\s+(\d+)\s+(\d+)").Groups[2].Value
    write-verbose ("Robocopy Bytes: $FileSize `n" +($LogData -join "`n"))
    
    # Monitor Full RoboCopy
    while (!$RoboRun.HasExited)
    {
    	$LogData = get-content $RoboLog
    	$Files = $LogData -match "^\s*(\d+)\s+(\S+)"
        if ($Files -ne $Null )
        {
    	    $copied = ($Files[0..($Files.Length-2)] | %{$_.Split("`t")[-2]} | Measure -sum).Sum
    	    if ($LogData[-1] -match "(100|\d?\d\.\d)\%")
    	    {
    		    write-progress Copy -ParentID $RoboRun.ID -percentComplete $LogData[-1].Trim("% `t") $LogData[-1]
    		    $Copied += $Files[-1].Split("`t")[-2] /100 * ($LogData[-1].Trim("% `t"))
    	    }
    	    else
    	    {
    		    write-progress Copy -ParentID $RoboRun.ID -Complete
    	    }
    	    write-progress ROBOCOPY -ID $RoboRun.ID -PercentComplete ($Copied/$FileSize*100) $Files[-1].Split("`t")[-1]
        }
    }
    
    # Parse full RoboCopy pass results, and cleanup
    (get-content $RoboLog)[-11..-2] | out-string | Write-Verbose
    [PSCustomObject]@{ ExitCode = $RoboRun.ExitCode }
    remove-item $RoboLog, $ScanLog
    Je voudrais m'en servir pour faire ma copie (1 dossier contenant plusieurs sous-dossiers contenant tous des fichiers

    J'ai donc créé un second .ps1 qui fait appel au précédent script dont voici le code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    C:\Users\David\Desktop\Copy-ItemWithProgress.ps1 "E:\source" "C:\destination\" -verbose
    Mais à l'execution j'ai ces messages d'erreur :

    Nom : Sans titre.jpg
Affichages : 1787
Taille : 96,7 Ko

    J'ai eu beau chercher toute la journée, je ne vois pas à quoi cela peut être du

    Pouvez vous me dire si ma façon de faire est la bonne svp sachant que je decouvre le scripting

    Et surtout comment corriger ces erreurs

    Je vous remercie par avance

    David

Discussions similaires

  1. calcul integral en c
    Par Anonymous dans le forum C
    Réponses: 3
    Dernier message: 11/01/2003, 11h32
  2. comment programmer une progressbar
    Par Choucas dans le forum Paradox
    Réponses: 3
    Dernier message: 13/11/2002, 11h07
  3. ProgressBar avec plusieurs procédures
    Par elifqaoui dans le forum VB 6 et antérieur
    Réponses: 8
    Dernier message: 08/09/2002, 18h03
  4. Réponses: 10
    Dernier message: 27/08/2002, 23h24
  5. Couleur de ProgressBar
    Par benj63 dans le forum C++Builder
    Réponses: 7
    Dernier message: 04/07/2002, 17h33

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo