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 |
Partager