Pergunta

I'm trying to copy a file from one server to another server so that it doesn't ask me to enter my login info and I don't have to store the password blatently in the code. The problem is, it's still coming up with a credential request GUI, even though I give the -Credential parameter. This is how I store the password in a file:

read-host -assecurestring | convertfrom-securestring | out-file C:\secString.txt

Then the code looks like this:

function GetSecureLogin() {
   $username = "usa02xxxswg2\20xbackup"
   $password = get-content C:\secString.txt | convertto-securestring
   $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
}

function CopyFileToFolder ([string]$Source, [string]$destination){
   Copy-Item "$Source" -Destination "$destination" -Credential $cred  #this is where the login info box is coming up but shouldn't be
}

######################start here#####################
$cred = ""
GetSecureLogin

$tempSource = "filename.txt"
$ToLocation = "$ToLoc"
CopyFileToFolder $tempSource $ToLoc

Any ideas? I'm relatively new to PowerShell, so I could be doing something silly. I'm not sure how to tell if $cred is getting back to my Copy function ok or if it's out of scope.

I've looked at these examples but I'm having trouble deciding if I need to do something different because they are specifying the server info and I'm trying to avoid that since I just want to use UNC path like I was doing before. The copying was working fine without login because I was using my Active Directory login to save the task. Now we want to use a locally saved login (where the script is run) and then it needs a different local login when it's trying to access the other server.

copy-item with alt cred, powershell without prompt for cred, copy without cred, copy with alt cred, cred without password prompt

Foi útil?

Solução 2

Looks like I figured it out. I did it like this link. It was a little tricky in that I didn't have to use the directory that came after my sharename, but once I got rid of the subdirectory, it worked.

copy with credentials and New-PSDrive

Outras dicas

I'm trying to figure out if returning $cred from my GetSecureLogin function, and assigning it to $cred in the main part of the script will fix the problem where it's asking me for the login info when I do the copy. It looks like my co-worker moved all the directories, so I still have to test this once I figure out where he moved them to.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top