Overview
In this post / guide we will demonstrate, listing files on a remote server’s in a specific directory.- If the file doesn’t exist a message will be displayed.
- If the user doesn’t key any file, an error message will be displayed.
Applies To
Tested on Windows 10, Windows 2008 R2, Windows 2012.Pre-Requisites
To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or “Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.Policy | Purpose |
---|---|
Restricted | No scripts can be run. Windows PowerShell can be used only in interactive mode. |
AllSigned | Only scripts signed by a trusted publisher can be run. |
RemoteSigned | Downloaded scripts must be signed by a trusted publisher before they can be run. |
Unrestricted | No restrictions; all Windows PowerShell scripts can be run. |
Current Execution Policy
To know the current run the PowerShell cmdlet; Get-ExecutionPolicyTo list execution policies that can be configured run the PowerShell cmdlet; Get-ExecutionPolicy -List
PowerShell Script – List File – Remote Server
This script will read drive and folder from user input, we will list files on a remote server in a specific folder and drive based on user input. In this script we will limit the listing only to first files only.Code Snippet – List Files
The code snippet is for demonstrating “List Files on a remote server on a specific drive”.Powershell Code Snippet – List Files
Clear-Host Set-Location $env:USERPROFILE Push-Location $env:USERPROFILE # # Set Output file Location # $OutPutFilename=$env:USERPROFILE+"\Desktop\FileLists.txt" $ServersFilename=$env:USERPROFILE+"\Desktop\ServerNames.txt" $ReadDrive=Read-Host "Enter the Drive" # # Remove string :\ # $DriveName=($ReadDrive).Contains(":\") if ($DriveName=$true) { $ReadDrive=$ReadDrive.Remove(1,2) } $ReadDirectory=Read-Host "Enter the Directory to List (exclude drive)" # # Remove Output file if file already exists. # if (Test-Path $OutPutFilename) { Remove-Item $OutPutFilename } Get-Content $ServersFilename | ForEach-Object { if (-Not (Test-Connection -ComputerName $_ -Count 1 -Quiet)) { Write-Host Server - $_ `t Is currently down or Unreachable -ForegroundColor White -BackgroundColor Red return } else { Write-Host `nServer - $_ `t Is Reachable -ForegroundColor Green -BackgroundColor Black Write-host \\$_\$ReadDrive$\$ReadDirectory if (Test-Path -Path \\$_\$ReadDrive$\$ReadDirectory) { $GCCount=(Get-Childitem "\\$_\$ReadDrive$\$ReadDirectory" -Filter *.txt).count if ($GCCount -ge 1) { Write-Host Searching .... "\\$_\$ReadDrive$\$ReadDirectory" Write-Host File listing count is $GCCount -ForegroundColor Yellow -BackgroundColor Black Get-Childitem "\\$_\$ReadDrive$\$ReadDirectory" -Filter *.txt | select @{n='ComputerName'; e={_$}}, FullName, LastAccessTime | select -first 5 | Format-Table -AutoSize | Out-File -Append $OutPutFilename } } else { Write-Host "`n$_ - Path Not Found `n" -BackgroundColor Red -ForegroundColor Yellow } return } } Invoke-Item $OutPutFilename
No comments:
Post a Comment