I’m new to PS script and need your urgent help.
My requirement is to copy the data files from my internal server to a remote third party file-share server on a daily basis.(site to site VPN is configured)
Please see the below script I used.
Source = "\\localserver\Y\Test*"
$Dest = “\220.127.116.11\TestData\”
$Username = “remotedomain\test.user”
$Password = ConvertTo-SecureString ‘password’ -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PsCredential ($Username,$Password)
New-PSDrive -Name ‘W’ -PSProvider FileSystem -Root $Dest -Credential $Credential -Persist
Copy-Item -Path $Source -Destination “W:” -Recurse -passthru
The issue I’m facing is, If I add the third party credentials to Windows “Credential manager” and then if I use the below, it will work.
New-PSDrive -Name ‘W’ -PSProvider FileSystem -Root $Dest -Persist
But I feel, that is not a best practice and need to pass the credential within New-PSDrive command.
Please help me to resolve the issue.
PS version : 4
OS: windows 2008R2 standard