Some articles are posted as Images, Please use Computers to go through them for best experience. For phone users, switch to Web Version

Windows Script - Test if Path input by User exist or not on Remote Servers

 <#Created By - Abhishek Bansal

Use - Script that runs on Servers mentioned in Input.txt file, checks if path input by User while executing this script exists or not on the server.

Prerquistes - Copy entire code, save it in a file with .ps1 extension. On same location, create another file named "Input.txt" and mention all the servers on which this script will run.

Output - TestaccessPath.csv file containing details. Column name Status shows if Path exist or not.

#>


$servers = Get-Content .\Input.txt

$line = 0 #

$linecount = $servers.Count #

$pct = 0 #

$path = Read-Host "Enter a path = "

$remotepath = $path -replace '^([A-Z]):', '$1$'

foreach($server in $servers)

{

$Error.Clear()

$line = $line + 1 #

$pct = $line/$linecount*100 #

Write-Progress -Activity "Checking on $server" -Status "$line out of $linecount" -PercentComplete $pct #

Write-Host -ForegroundColor Cyan "Traversing to $server."#

$result = Test-Path "\\$server\$remotepath" -ErrorAction Stop

    if($result -eq "True")

    {

    Write-Host -ForegroundColor Green "$($path) exists on $server`n"

    $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Path";e={$path}},@{n="Status";e={"Yes"}} | Export-Csv ./TestaccessPath.csv -NoTypeInformation -Append

    }

    else

    {

    Write-Host -ForegroundColor Red "$($path) doesn't exist on $server`n"

    $server | Select-Object -Property @{n="Hostname";e={$server}},@{n="Path";e={$path}},@{n="Status";e={"No"}} | Export-Csv ./TestaccessPath.csv -NoTypeInformation -Append

    }

}

No comments:

Post a Comment