OK, the real problem was that I was getting the path from a registry entry and then cleaning it with Regex. The very start of the path had a space in the front which I did not notice the way I was logging. I fixed this problem by using this:
#outvar is used to build the path and comes from a registry key | Out-String
$outvar = ($outvar -replace '\s','')
$localtruststore = "C:\Users\me\OneDrive\work\scripts\PS\TEST\truststore"
$servers = "SERVER1"
## remotepath is actually set by looking at a registry entry, but I am sure it is coming out like this:
$remotepath = "d$\programname\40\server\Openfire\resources\security"
#### THIS LINE CAUSES THE ERROR - I think just because of the $.
Copy-Item $localtruststore -Destination \\$server\$remotepath -Force
Copy-Item : Illegal characters in path.
At C:\Users\me\OneDrive\work\scripts\PS\TEST\chat_copy_trustore_to_remote.ps1:46 char:11
+ Copy-Item <<<< $localtruststore -Destination \\$server\$remotepath -Force
+ CategoryInfo : NotSpecified: (:) [Copy-Item], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.CopyItemCommand
If I manually put the Destination in, the copy works perfectly, so this must be a simple syntax issue.
I have tried completely building the $destinationpath variable by doing these:
$destinationpath = "\\$server\$remotepath"
$destinationpath = ("\\{0}\{1}" -f $server,$remotepath)
BOTH of these work and when I write-host the variable I get the correct \server\d$\programe…
I still get illegal character in path., char14
I have confirmed that the character referenced in the error is the exact number where the $ symbol is in the destination. it is that char number in that argument.
I ended up just breaking this in to two parts….see below..
This will copy to the admin share for the root of D.
Copy-Item -path $localtruststore -Destination \$server\d$ -Force
Then I use this later to move the file on the remote server..
Invoke-command -ComputerName $server {
Copy-Item -path D:\truststore -Destination D:\Temp -Force
}
What may or may not have complicated this is my Java Keystore files, JKS files, do not have file extensions..
Best Answer
UNC paths start with double backslashes so your command should look like this: