Hello,
I don’t get an error message but my msi file isn’t launching on my remote computer…
# 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
# Lancement de l'installation
Invoke-Command -ComputerName $Script:RemoteComputer -ScriptBlock {
# 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
$msiPath = "C:\temp\" + $NomFichier
$msiWithArguments = $msiPath + ' ' + $Script:argumentsMSI
# Lancer l'installation du fichier MSI
Start-Process -FilePath "msiexec.exe" -ArgumentList "/i $msiWithArguments" -Wait
}
# 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
If I replace the variable $msiWithArguments with the link of the msi file it works.
Thank you in advance for your help.