PowerShell script to search List of files from different server and copied it on your given location
I am going to explain below powershell scrip that how it work to copied files in your location from different-different server, Please follow below 7 steps to achieve result-
1. Open Window PowerShell ISE and go to file menu then click on New or Ctrl+N.
2. Copy below script then paste it and finally save as GetFile.ps1.
3. You have full access of server path and location that in script.
4. Create A folder in C drive and name as Jitendra and within this folder two more folder name as CopyFiles and CopiedData respectively.
5. Copy your GetFile.ps1 in CopyFiles folder.
6. Some of description of below script variables:
$FileList = copy list of file in line by line that you want to search and copied to your own location
$ServerList = Name of server with full path from where you want to search your list of files
$destination = Folder where all searched files being copied
$ResultLog = all copied files will be marked here
Result_NonCopiedFile.txt = marked those files that are not searched from your given server
$FileList='C:\Jitendra\CopyFiles\FileList.txt' #In FileList.txt, copy list of file in line by line that you want to search and copied to your own location#
$ServerList='C:\Jitendra\CopyFiles\ServerList.txt' #Name of server with full path where you want to search your list of files#
$destination='C:\Jitendra\CopiedData' #CopiedData is folder where all searched files being copied#
$ResultLog='C:\Jitendra\CopyFiles\Result.txt' #In result.txt, all copied files will be marked#
$ResultLog_NonCopiedData='C:\Jitendra\CopyFiles\Result_NonCopiedFile.txt' #In Result_NonCopiedFile.txt, marked those files that are not searched from your given server#
$ListOfFiles=Get-Content $FileList
$ServerPath= Get-Content $ServerList
$TotalCount= Get-Content $FileList | Measure-Object | Select-Object -ExpandProperty count
$count=0
$log
foreach($file in $ListOfFiles)
{
$result=$false
foreach($URL in $ServerPath)
{
try
{
if(Test-Path( $URL +$file))
{
Copy-Item $URL$file $Destination
$count++
$result=$true;
$log=$file +' successfully copied.'
Add-Content $ResultLog $log
break;
}
}
catch [system.exception]
{
Write-Host 'Not found in '$URL
}
}
if($result -eq $false)
{
$log=$file +' does not exists from given location.'
Add-Content $ResultLog_NonCopiedData $log
}
}
$log = [string]$count + '/' + [string]$TotalCount + ' File has Copied.'
Add-Content $ResultLog $log
Add-Content $ResultLog_NonCopiedData $log
7. Now run Window powerShell as administrator and follow below PowerShell command
> Set-ExecutionPolicy
> Unrestricted
> Y
> CD C:\Jitendra\CopyFiles\
> .\GetFile.ps1
Comments
Post a Comment
Please write comment only that belongs to this blog