Copie et récupération de fichiers sur une VM puis exécution d'une commande
Comment en Powershell copier ou récupérer un fichier sur une VM depuis votre poste de travail local et lancer des commandes sur la VM
Syntaxe à utiliser pour lancer le script
Powershell -command "c:\NomScriptPowerShell.ps1" -v NomVm
Lien vers le fichier : cliquez ici
Clear-Host
param
(
[string]$VIserver,
[string]$VILogin,
[string]$VIMotdepasse,
[string]$VMName,
[string]$varErrorCount,
[switch]$verbose,
[switch]$debug
)
Clear-Host
#Definition de 2 variables globales
$global:MyVar01 = ""
$global:MyVar02 = ""
$CheminFichierACopier = "C:\Test.txt"
$VIserver = "Nom vCenter"
$VILogin = "domaine\login" #Domaine\login avec droits de connexion au vCenter
$VIMotdepasse = "YourPassword" #Mot de passe pour connexion au vCenter
$VMLogin = "LoginCompteLocalDeLaVM"
$VMMotdepasse = "MotDePasseLocalSurVM"
function main()
{
asnp vmware* #Import modules VMWares
$ErrorActionPreference = "Continue"
if ($verbose) {$VerbosePreference = "Continue"}
if ($debug) {$DebugPreference = "Continue"}
$DebugPreference = "SilentlyContinue"
CheckVIToolKit
Connect-VIServer -Server $VIserver -User $VILogin -Password $VIMotdepasse
Write-Host "Traitement de $VMName"
$UneVM = Get-VM -Name $VMName
Write-Host $UneVM.PowerState #Juste pour afficher si la VM est allumée ou non
$Error.Clear()
#Copie d'un fichier de votre poste de travail vers la VM
Copy-VMGuestFile -Source $CheminFichierACopier -Destination c:\ -VM $UneVM -LocalToGuest -GuestUser $VMLogin -GuestPassword $VMMotdepasse
if($Error.Count -ne 0) { Write-Host "Erreur : $error[0]" }
#Récupération d'un fichier de la VM vers votre poste
Copy-VMGuestFile -Source $CheminFichierACopier -Destination c:\ -VM $UneVM -GuestToLocal -GuestUser $VMLogin -GuestPassword $VMMotdepasse
if($Error.Count -ne 0) { Write-Host "Erreur : $error[0]" }
#Exécution d'une commande sur la VM
Invoke-VMScript -ScriptText "dir *.* > c:\aeff.txt" -VM $UneVM -ScriptType Bat -GuestUser $VMLogin -GuestPassword $VMMotdepasse
if($Error.Count -ne 0) { Write-Host "Erreur : $error[0]" }
}
function CheckVIToolKit()
{
## Before we do anything we must check to see if the user has the VI toolkit installed.
## If user does not then we prompt the user and exit.
$Error.Clear()
Get-PSSnapin vmware*
if($Error.Count -ne 0)
{
Clear-Host
Write-Host "`n`n`t`t ERROR - To run this script, the VI Toolkit must be installed and registered with Powershell. If the VI Tollkit is installed," -foregroundcolor red -backgroundColor yellow
Write-Host "`t`t go to the Settings menu in Powershell Plus and click on Manage Snapins." -foregroundcolor red -backgroundColor yellow
# Read-Host "`n`n`t Press <Enter> to continue."
break
}
}## EOF: CheckVIToolKit()
Function ParseCommand($oArgs){
#Parses the command line and fills the script variables
#with the appropriate values.
# $test = "-n,test"
# $oArgs = $test.split(",")
$ArgCount = 0
if (!$oArgs.length -gt 0){
write-host "No arguments specified."
}
While ($ArgCount -lt $oArgs.length){
switch ($oArgs[$ArgCount].ToLower()){
"-v"{
$global:MyVar01 = $oArgs[($ArgCount+1)]
$ArgCount = ($ArgCount + 2)
write-host "-v : $MyVar01"
}
default{
write-host "Invalid command."
# Help
exit
}
}
}
}
#Parse commands
ParseCommand($args)
$VMName = $MyVar01
write-host "VMName : $VMName"
## Run Main
main
Lien vers le fichier : cliquez ici
Publicité
Pages Web
| Site Web | Description |
|---|---|
| VMWare support | Aide de la commande Copy-VMGuestFile |
| VMWare support | Aide de de la commande Invoke-VMScript |