Another Powershell script


  Check out the following script that is mainly used for copying files from one server to multiple devices over the network. The script reads the names of the servers from the servers.txt file. Here you can add as many entries as you like. I will explain the main components of this script:
the $sourceuat and $sourceprod variables contains the path from which the files will be copied from. Also, the script will select only the most recent file, if there are multiple files. You can modify the path to suit your needs:

$sourceuat = Get-ChildItem -Path D:\”*” | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$sourceprod = Get-ChildItem -Path D:\”*” | Sort-Object LastAccessTime -Descending | Select-Object -First 1
If the server’s name contains a particular patters (in this case *uat*), then the script will run the commands for the UAT environments. In case you don’t know UAT stands for USER ACCEPTANCE TESTING, a testing environment for products.
The $destinationuat and $destinationprod will contain the destination’s file and will be used by the script to check if the file was copied.
Start-Sleep -s 1 puts the script on a hold state for 1 second
The script will create a local “Log.txt” file where the operations will be logged.

Powershell script

I think that’s about it, here is the actual script:

$servers = Get-Content servers.txt 
foreach($j in $servers)  
{
if ($j -like “*uat*”)
    {
    $sourceuat = Get-ChildItem -Path D:\”*” | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    Copy-Item $sourceuat -Destination “$j\destination” -force
    Start-Sleep -s 1
    $destinationuat = Get-ChildItem -Path “$j\destination” | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    $logFile = ‘Log.txt’
    $date = Get-Date
    if ($sourceuat.Name -eq $destinationuat.Name)
      {
      
        Write-output “$date : the copy for $j server was SUCCESSFUL” | Out-File -Append $logFile
      }
      else
      {     
        Write-output “$date : the copy for $j server was NOT successful” | Out-File -Append $logFile
      }
    }
    
    else
    {
    $sourceprod = Get-ChildItem -Path D:\folder1\”*” | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    Copy-Item $sourceprod -Destination “$j\destination” -force
    Start-Sleep -s 1
    $destinationprod = Get-ChildItem -Path “$j\destination” | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    $logFile = ‘Log.txt’
    $date = Get-Date
    if ($sourceprod.Name -eq $destinationprod.Name)
      {
      
        Write-output “$date : the copy for $j server was SUCCESSFUL” | Out-File -Append $logFile
      }
      else
      {     
        Write-output “$date : the copy for $j server was NOT successful” | Out-File -Append $logFile
      }  
   }
}

Best regards folks, stay tuned for the following articles.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s