How do I check whether directory exists on remote computer/DFS share, and create it if not?

16 Views Asked by At

The script is to be run from a central administration server.

I need to check if a directory (BA Client) exists on multiple remote computers. If it exists, I then need to check if it exists on a DFS share as a sub dir to a directory named with the computer name and if not, then create it.

Finally, I want to copy specific files from the computer to that DFS share \\$server\directory.

This is the script I'm trying to run; however, it fails (hangs) if the directory does not exist on the source computer.

$serverlist = Get-Content ".\serverlist.txt"
foreach ($server in $serverlist)

{
    #If this folder exists in this host
    if (Test-Path "\\$server\C$\progra~1\Tivoli\TSM\Baclient" -eq $true ))
    
    #If this folder exists on DFS share
    if (-not (Test-Path "\\domainname\dfs\Systemer\TSMRepository\servere OPT Files\$server\baclient"))
    {
        Write-Host "Creating new folder...... "
        New-Item -Path "\\domainname\dfs\Systemer\TSMRepository\servere OPT Files\$server\baclient" -ItemType Directory
    }

    sleep -Seconds 4
    Write-Host "Copying the file(s) to destination...."
    Copy-Item -Path "\\$server\C$\progra~1\Tivoli\TSM\Baclient\*.opt" -Destination "\\domainname\dfs\Systemer\TSMRepository\servere OPT Files\$server\baclient\"  -Verbose
}
0

There are 0 best solutions below