I am trying to add some logic to my function that will check to see if a folder exists and copies the data. If the folder does not exist, it will create the folder using the $destinationfolder variable. When I run the below code, the script will not add the $destinationfolder directory to the remote computer. Any guidance would be appreciated and thanks for your time.
I verified that my script works if I take out the If Construct.
function Copy-RCSSFile {<#
.SYNOPSIS
Copy files from a given path to a remote computer.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)]
[string]$computername,
[Parameter(Mandatory=$True,
ValueFromPipelineByPropertyName=$True)]
[string]$path,
[Parameter(Mandatory=$True,
ValueFromPipelineByPropertyName=$True)]
[string]$destination,
[Parameter(ValueFromPipelineByPropertyName=$True)]
[string]$destinationfolder
) # param
BEGIN {
Setting up DestinationFolder
Write-Verbose"Setting $destinationfolder variable"
$destinationfolder=“C:_Code”
} #BEGIN
PROCESS {
foreach ($computerin$ComputerName) {
Setting up Session to remote computer
Write-Verbose"Connecting to $computer to copy items"
$session=New-PSSession-ComputerName $computer
Write-Verbose"Checking to see if $destinationfolder exists on $computer"
If (-not (Test-Path-LiteralPath $destinationfolder)) {
Try {
Write-Verbose"Creating $destinationfolder on $computer"
New-Item-Path $destinationfolder-ItemType Directory -ErrorAction Stop
}
Catch {
Write-Error-Message “Unable to create directory ‘$destinationfolder’. Error was: $_”-ErrorAction Stop
}
} Else {
Write-Verbose"Directory already exists"
} # If
Write-Verbose"Copying files to $computer"
Copy-Item-Path $path-Destination $destination-ToSession $session
Write-Verbose"Closing $computer session"
Remove-PSSession-Session $session
} # foreach
} #PROCESS
END {
} #END
} #function