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 :

Outil pour copier et lancer un fichier sur un poste


Sujet :

Scripts/Batch

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Intégrateur Web
    Inscrit en
    Septembre 2020
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Septembre 2020
    Messages : 32
    Par défaut Outil pour copier et lancer un fichier sur un poste
    Bonjour,

    Voilà je suis en train de développer un script en PowerShell pour copier un fichier d'installation sur un poste puis lancer l'installation.
    Plus j'avance et plus j'ai l'impression de faire une usine à gaz.

    Mes étapes :
    1. copie du fichier de mon poste vers le poste distant sur c:\
    2. ouverture d'une connexion à distance sur le poste
    3. contrôle que le dossier c:\temp existe. Si pas de dossier, le créer
    4. déplacement du fichier de c: vers c:\temp
    5. exécution du fichier

    Pour le moment, j'ai fait les 4 premiers points. J'ai des erreurs sur le point 4 car je dois relance deux fois le script pour que cela fonctionne comme s'il essayé de copier le fichier avant d'avoir créé le dossier temp. Mais de toute façon, idéalement, j'aurais préféré contrôler si le dossier temp existe sur le poste distant, le créer si ce n'est pas le cas et copier directement le fichier dedans sans devoir passer au préalable par une copie sur le c: mais ça ne semblait pas fonctionner. Je m'y prends sans doute mal... Voilà mon code :

    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
    # Déclaration du chemin du fichier sur le poste local
    $localFilePath = "C:\temp\PROTECT_Installer_x64_fr_FR.exe"
    
    # Déclaration des détails du poste distant
    $remoteComputer = "192.168.254.230"
    $remoteFolderPath = "\\$remoteComputer\c$"
    
    #Copie du fichier à la racine du poste distant
    Copy-Item -Path $localFilePath -Destination $remoteFolderPath
    
    #Connexion au poste distant
    Enter-PSSession -ComputerName $remoteComputer -Credential "IT-CONNECT\Administrateur"
    
    # Spécifier les chemins des fichiers et dossiers
    $sourceFilePath = "C:\PROTECT_Installer_x64_fr_FR.exe"
    $destinationFolderPath = "C:\temp"
    
    # Vérifier si le dossier de destination existe, sinon le créer
    if (-not (Test-Path $destinationFolderPath -PathType Container)) {
        New-Item -Path $destinationFolderPath -ItemType Directory | Out-Null
        Write-Host "Le dossier $destinationFolderPath a été créé."
    } else {
        Write-Host "Le dossier $destinationFolderPath existe déjà."
    }
    
    # Copier le fichier vers le dossier de destination
    Move-Item -Path $sourceFilePath -Destination $destinationFolderPath
    Merci d'avance

  2. #2
    Membre averti
    Homme Profil pro
    Intégrateur Web
    Inscrit en
    Septembre 2020
    Messages
    32
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Intégrateur Web

    Informations forums :
    Inscription : Septembre 2020
    Messages : 32
    Par défaut
    Bonjour,

    J'ai avancé sur mon script. C'est la fin du script qui ne fonctionne pas cela ne lance pas mon fichier msi.

    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
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    # Importation de la librairie
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    # Création de l'objet de formulaire
    $form = New-Object System.Windows.Forms.Form
    $form.Text = "Télédistribution"
    $form.Size = New-Object System.Drawing.Size(600,230)
    $form.StartPosition = "CenterScreen"
    
    # Champ pour saisir le nom de la machine
    $labelMachineName = New-Object System.Windows.Forms.Label
    $labelMachineName.Text = "Nom de la machine ou adresse IP (*) :"
    $labelMachineName.Location = New-Object System.Drawing.Point(10,20)
    $labelMachineName.AutoSize="$true"
    $form.Controls.Add($labelMachineName)
    
    $textboxMachineName = New-Object System.Windows.Forms.TextBox
    $textboxMachineName.Location = New-Object System.Drawing.Point(210,15)
    $textboxMachineName.Width=360
    $form.Controls.Add($textboxMachineName)
    
    # Bouton pour parcourir et sélectionner le fichier MSI
    $labelFilePath = New-Object System.Windows.Forms.Label
    $labelFilePath.Text = "Fichier MSI (*) :"
    $labelFilePath.Location = New-Object System.Drawing.Point(10,50)
    $labelFilePath.AutoSize="$true"
    $form.Controls.Add($labelFilePath)
    
    $buttonBrowse = New-Object System.Windows.Forms.Button
    $buttonBrowse.Text = "Parcourir"
    $buttonBrowse.Location = New-Object System.Drawing.Point(210,45)
    $buttonBrowse.Add_Click({
        $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
        $openFileDialog.Filter = "Fichiers MSI (*.msi)|*.msi|Tous les fichiers (*.*)|*.*"
        $openFileDialog.Title = "Sélectionnez un fichier MSI"
    
        if ($openFileDialog.ShowDialog() -eq 'OK') {
            $textboxFilePath.Text = $openFileDialog.FileName
        }
    })
    $form.Controls.Add($buttonBrowse)
    
    $textboxFilePath = New-Object System.Windows.Forms.TextBox
    $textboxFilePath.Location = New-Object System.Drawing.Point(210,75)
    $textboxFilePath.Width=360
    $textboxFilePath.ReadOnly = $true
    $form.Controls.Add($textboxFilePath)
    
    # Champ pour saisir des arguments du fichier MSI
    $labelArgumentsMSI = New-Object System.Windows.Forms.Label
    $labelArgumentsMSI.Text = "Arguments du fichier MSI:"
    $labelArgumentsMSI.Location = New-Object System.Drawing.Point(10,110)
    $labelArgumentsMSI.AutoSize="$True"
    $form.Controls.Add($labelArgumentsMSI)
    
    $textboxArgumentsMSI = New-Object System.Windows.Forms.TextBox
    $textboxArgumentsMSI.Location = New-Object System.Drawing.Point(210,105)
    $textboxArgumentsMSI.Width=360
    $form.Controls.Add($textboxArgumentsMSI)
    
    
    # Bouton OK pour valider et fermer le formulaire
    $buttonOK = New-Object System.Windows.Forms.Button
    $buttonOK.Text = "Lancer la télédistribution"
    $buttonOK.Location = New-Object System.Drawing.Point(410,145)
    $buttonOK.AutoSize="$true"
    $buttonOK.Add_Click({
        $Script:RemoteComputer = $textboxMachineName.Text
        $Script:FilePath = $textboxFilePath.Text
        $Script:argumentsMSI = $textboxArgumentsMSI.Text
        $form.Close()
    })
    $form.Controls.Add($buttonOK)
    
    
    # Affichage du formulaire
    $form.ShowDialog() | Out-Null
    
    # Vérifier si la liste des TrustedHosts existe
    if (-not (Test-Path WSMan:\localhost\Client\TrustedHosts)) {
        # Si elle n'existe pas, la créer avec la machine $Script:RemoteComputer
        Set-Item WSMan:\localhost\Client\TrustedHosts -Value $Script:RemoteComputer -Force
    } else {
        # Si elle existe, vérifier si elle est vide
        $currentTrustedHosts = (Get-Item WSMan:\localhost\Client\TrustedHosts).Value
        if (-not $currentTrustedHosts) {
            # Si elle est vide, ajouter la machine $Script:RemoteComputer
            Set-Item WSMan:\localhost\Client\TrustedHosts -Value $Script:RemoteComputer -Force
        } else {
            # Si elle n'est pas vide, vérifier si la machine $Script:RemoteComputer existe
            $hostsArray = $currentTrustedHosts -split ','
            if ($Script:RemoteComputer -notin $hostsArray) {
                # Si elle n'existe pas, ajouter la machine $Script:RemoteComputer à la suite des autres machines
                $newTrustedHosts = $currentTrustedHosts + "," + $Script:RemoteComputer
                Set-Item WSMan:\localhost\Client\TrustedHosts -Value $newTrustedHosts -Force
            }
            # Si elle existe, ne rien faire
        }
    }
    
    
    # Déclaration des détails du poste distant
    $RemoteFolderPath = "\\$Script:RemoteComputer\c`$\Temp\"
    
    
    # Vérifier si le dossier de destination existe, sinon le créer
    if (-not (Test-Path $RemoteFolderPath -PathType Container)) {
        New-Item -Path $RemoteFolderPath -ItemType Directory | Out-Null
        Write-Host "Le dossier $RemoteFolderPath a été créé."
    } else {
        Write-Host "Le dossier $RemoteFolderPath existe déjà."
    }
    
    
    # Copie du fichier à l'emplacement du poste distant
    Copy-Item -Path $FilePath -Destination $RemoteFolderPath
    
    
    # Récupéreration du nom du fichier sans le chemin
       $NomFichier = [System.IO.Path]::GetFileName($textboxFilePath.Text)
    # Chemin du fichier MSI sur le serveur distant
       $msiWithPath = "C:\temp\" + $NomFichier
    
    # Lancer l'installation du fichier MSI
    Invoke-Command -ComputerName $Script:RemoteComputer -ScriptBlock {
       Start-Process -FilePath "msiexec.exe" -ArgumentList "/i `"$msiWithPath`" $Script:argumentsMSI" -Wait -PassThru
    }
    
    # Suppression du site dans les TrustedHosts de WinRM
    $listeTrustedHosts = (Get-Item WSMan:\localhost\Client\TrustedHosts).Value
    $listeMiseAJour = $listeTrustedHosts -replace "$Script:RemoteComputer,", "" -replace ",$Script:RemoteComputer", "" -replace "$Script:RemoteComputer", ""
    Set-Item WSMan:\localhost\Client\TrustedHosts -Value $listeMiseAJour -Force

Discussions similaires

  1. outils pour modéliser une application web basé sur UML
    Par sarinia dans le forum Modélisation
    Réponses: 1
    Dernier message: 10/03/2011, 09h11
  2. outils pour tester une structure de fichier edi
    Par Midsou dans le forum EDI/Outils
    Réponses: 0
    Dernier message: 28/10/2009, 14h40
  3. Quels outils pour image de XP et Vista sur le meme disque
    Par neodelphi2007 dans le forum Windows Vista
    Réponses: 3
    Dernier message: 09/09/2008, 07h36
  4. Réponses: 2
    Dernier message: 10/10/2006, 09h33

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