SHORT ANSWER
if(-Not(Test-Path "filesystem::\\$server\c$")) {Write-Error "Server not found: $server"; continue} if(-Not(Test-Path "filesystem::\\$server\c$")) {Write-Error "Server not found: $server"; continue} if(-Not(Test-Path "filesystem::\\$server\c$")) {Write-Error "Server not found: $server"; continue} if(-Not(Test-Path "filesystem::\\$server\c$")) {Write-Error "Server not found: $server"; continue} If Test-Path failed unexpectedly, make sure that SMB2 = 1 (or another SMB parameter) is set on both the client and the destination server.
MORE INFORMATION
IMPORTANT NOTE SMB: both the current system and the target system must have at least a common SMB protocol that is allowed to pass the Test-Path successfully. (It is strongly recommended that you use SMB2 or later.) For example, if SMB1 + is disabled for the target and SMB2 is disabled and only SMB2 is enabled on the client, the above logic will return the message "Server not found ...". This baffled me until I finally checked my target server (Win7) and found that it had SMB2 = 0 (disabled) and there was no entry for SMB1 (enabled by default). I fixed it by setting SMB2 = 1 for the article below.
SMB OS details and script details: https://support.microsoft.com/en-us/help/2696547/detect-enable-disable-smbv1-smbv2-smbv3-in-windows-and-windows-server
Exposure: Win8 / Win20012
Detect: Get-SmbServerConfiguration | Select EnableSMB1Protocol Disable: Set-SmbServerConfiguration -EnableSMB1Protocol $false Enable: Set-SmbServerConfiguration -EnableSMB1Protocol $true
Exposure: Win7 / Win2008R2Server
Detect: Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters | ForEach-Object {Get-ItemProperty $_.pspath} Default configuration = Enabled (No registry key is created), so no SMB1 value will be returned Disable: Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" SMB1 -Type DWORD -Value 0 –Force Enable: Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" SMB1 -Type DWORD -Value 1 –Force
Code example: Copy-Item folder (recursive), only if the target server exists
$scriptRootPath="." $scriptToolsPath="$scriptRootPath\Tools" $targetServerList="$scriptToolsPath\DeployServerList-INT-POC.txt" #DeployServerList-INT.txt, DeployServerList-QA.txt, DeployServerList-PROD.txt $stageTargetDrive="c" $stageFolderPath="$stageTargetDrive$\staging" $VerbosePreference="Continue" #"SilentlyContinue" (default), "Continue", "Stop", "Inquire" $InformationPreference="Continue" Write-Host "Getting list of servers from $targetServerList to stage deployment files to..." $serverList = (get-content "$targetServerList") Write-Verbose "scriptToolsPath=$scriptToolsPath" Write-Verbose "serverlist=$serverList" Write-Verbose "stageFolderPath=$StageFolderPath" Write-Host -Separator "-" Read-Host -Prompt "READY TO STAGE FILES: Check info above, then press Enter to continue (or Ctrl+C to exit)." Write-Host "-------------------------------------------------" Write-Host "Staging files to $stageFolderPath on each target server..." foreach ($server in $serverlist) { # Input validation if([string]::IsNullOrWhiteSpace($server)) {continue} if($server.StartsWith("#")) {Write-Verbose "Comment skipped: $server"; continue} # Skip line if line begins with hashtag comment char Write-Verbose "Testing filesystem access to $server..." if(-Not(Test-Path "filesystem::\\$server\$stageTargetDrive$")) {Write-Error "Server not found: $server"; continue} # TIP: If Test-Path returns false unexpectedly then check if SMB2 is enabled on target server, check SMB1 disabled for both src and target servers. Write-Verbose "Staging files to $server..." Copy-Item ".\" -Destination "\\$server\$stageFolderPath" -Recurse -Force -ErrorAction Continue Write-Information "Files staged on $server." }
source share