PowerCLi script to Upload files into datastores for multiple hosts in parallel

This post was originally published on this site

Hello Friends,

 

Recently i’ve got a scenario where i have to upload the files into multiple hosts in their local datastores around 2000+ boxes. I’m trying below scripts to upload the files to selected 50 hosts in parallel. but somehow script is not running as per our expectation. Any help would be highly appreciated here.

 

Script object: master script should initiate both file upload scripts in parallel for multiple hosts at a time.

 

Master script:

===========

$hosts= Get-Content “C:HostList.txt”

 

 

foreach ($esx in $hosts) {

 

Write-Host “Running File upload1 script on host: $esx …”  -ForegroundColor Yellow

#.Test_FileUpload.ps1 -HostName $esx -User “XXXX”  -Pswd “XXXX”  

Write-Host “Running File upload2 script on host: $esx …”  -ForegroundColor Yellow

#.Test_FileUpload2.ps1 -HostName $esx -User “XXX”  -Pswd “XXXX”

}

 

script2:Test_FileUpload.ps1

===========

Param(

[string]$HostName,

[string]$User,

[string]$Pswd

)

 

$fileName = “C:UsersNew folderDELL-T330ESXi670-202008001.zip”

#$tgtFolder = ‘ISO’

$StartTime = Get-Date

 

 

Write-Host “************************************************” -ForegroundColor Cyan

Write-Host “Please wait while connecting the host: $HostName ” -ForegroundColor Yellow

Write-Host “************************************************” -ForegroundColor Cyan

 

 

Connect-VIServer -Server $HostName -user $User -Password $Pswd

Write-Host “connected to the server: $HostName” -ForegroundColor Green

 

 

$datastores = Get-VMHost $HostName | Get-Datastore | where{$_.name -like “datastore*”}

 

 

    foreach($ds in $datastores){

   

    New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root “” > $null

    Write-Host “New ISO folder has been created under DS: $ds ” -ForegroundColor Green

     New-Item -Path DS:ISO -ItemType Directory

    

     $tgtFolder =’ISO’

     New-PSDrive -Location $ds -Name DS1 -PSProvider VimDatastore -Root “” > $null

  

    if(!(Test-Path -Path “DS1:/$($tgtFolder)”)){

        New-Item -ItemType Directory -Path “DS1:/$($tgtFolder)” > $null

    }

    Write-Host “DS copy is in-progres.. Pls wait” -ForegroundColor Yellow

    Copy-DatastoreItem -Item $fileName -Destination “DS1:/$($tgtFolder)”

   

    Remove-PSDrive -Name DS -Confirm:$false

    Remove-PSDrive -Name DS1 -Confirm:$false

}

 

Write-Host ” DS copy task has completed for host: $fileName” -ForegroundColor Green

 

$EndTime = Get-Date

$TotalTime = New-TimeSpan –Start $StartTime –End $EndTime #| select -expandproperty Seconds

Write-Host “************************************************” -ForegroundColor Cyan

Write-Host “Total time taken to complete the ISO copy: $TotalTime ” -ForegroundColor Green

Write-Host “************************************************” -ForegroundColor Cyan

 

 

script3:Test_FileUpload2

==========

Param(

[string]$HostName,

[string]$User,

[string]$Pswd

)

 

$fileName = “C:UsersNew folderDELL-T330VMware-VMvisor-Installer-6.7.0.update03-16316930.x86_64-DellEMC_Customized-A06.zip”

#$tgtFolder = ‘ISO’

$StartTime = Get-Date

 

 

Write-Host “************************************************” -ForegroundColor Cyan

Write-Host “Please wait while connecting the host: $HostName ” -ForegroundColor Yellow

Write-Host “************************************************” -ForegroundColor Cyan

 

 

Connect-VIServer -Server $HostName -user $User -Password $Pswd

Write-Host “connected to the server: $HostName” -ForegroundColor Green

 

 

$datastores = Get-VMHost $HostName | Get-Datastore | where{$_.name -like “datastore*”}

 

 

    foreach($ds in $datastores){

   

    New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root “” > $null

    Write-Host “New ISO folder has been created under DS: $ds ” -ForegroundColor Green

     New-Item -Path DS:ISO -ItemType Directory

    

     $tgtFolder =’ISO’

     New-PSDrive -Location $ds -Name DS1 -PSProvider VimDatastore -Root “” > $null

  

    if(!(Test-Path -Path “DS1:/$($tgtFolder)”)){

        New-Item -ItemType Directory -Path “DS1:/$($tgtFolder)” > $null

    }

    Write-Host “DS copy is in-progres.. Pls wait” -ForegroundColor Yellow

    Copy-DatastoreItem -Item $fileName -Destination “DS1:/$($tgtFolder)”

   

    Remove-PSDrive -Name DS -Confirm:$false

    Remove-PSDrive -Name DS1 -Confirm:$false

}

 

Write-Host ” DS copy task has completed for host: $fileName” -ForegroundColor Green

 

$EndTime = Get-Date

$TotalTime = New-TimeSpan –Start $StartTime –End $EndTime #| select -expandproperty Seconds

Write-Host “************************************************” -ForegroundColor Cyan

Write-Host “Total time taken to complete the ISO copy: $TotalTime ” -ForegroundColor Green

Write-Host “************************************************” -ForegroundColor Cyan

 

 

Regards,

Swamy Naveen

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.