0

PowerShell script with credentials to copy file from one server to another server.

I have written the code but it's not working. What is wrong with my code? I get the error as

New-PSSession : myComp1 Connecting to remote server myComp1 failed with the following error message : The WS-Management service cannot process the request. Cannot find the Microsoft.PowerShell session configuration in the WSMan: drive on the myComp1 computer. For more information, see the about_Remote_Troubleshooting Help topic.

$pw = ConvertTo-SecureString -AsPlainText -Force -String password
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "domain\UserName", $pw
$trgtSession = New-PSSession -ComputerName myComp1 -Credential $creds 
Copy-Item -ToSession $trgtSession -Path "C:\Users\" -Destination "C:\Users\desktop\" -Recurse

Expected result: code to move the file from one server to another server with credentials.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
MaheshMohan
  • 49
  • 4
  • 11
  • I don't think this is the whole error message. Usually, there is more explanation to it. PLease post the complete error – Theo May 21 '19 at 08:59
  • Check if Powershell Remoting (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/enable-psremoting?view=powershell-6) is enabled on the target machine. – Moerwald May 21 '19 at 09:04
  • @Theo Error Message it This - > New-PSSession : myComp1 Connecting to remote server myComp1 failed with the following error message : The WS-Management service cannot process the request. Cannot find the Microsoft.PowerShell session configuration in the WSMan: drive on the myComp1 computer. For more information, see the about_Remote_Troubleshooting Help topic. – MaheshMohan May 21 '19 at 09:06
  • Is there any other way I can copy file from one server to another server using PS script with Credentials? – MaheshMohan May 21 '19 at 09:09
  • Aha. By googling on that complete message, the first hit I got was [this blog](https://sysadminplus.blogspot.com/2016/11/the-ws-management-service-cannot.html). It explains the error and also tells you what you can do to resolve it. – Theo May 21 '19 at 09:12
  • @Theo The link was useful one. so i need Powershell remote endpoints enabled to use New-PSSession or Invoke-Command. Got it. Is there any other way without using New-PSSession. some hints on it would be useful. I googled but nothing worked out. – MaheshMohan May 21 '19 at 09:21
  • I guess you can run this as domain administrator using UNC paths: `Copy-Item -Path 'UNCPATH OF SOURCE FILES' -Destination 'UNCPATH OF EXISTING TARGET FOLDER' -Recurse`. Otherwise, have a look at the answers given [here](https://stackoverflow.com/questions/612015/copy-item-with-alternate-credentials) – Theo May 21 '19 at 09:29
  • Try to troubleshoot : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_troubleshooting?view=powershell-6 – Nirav Mistry May 21 '19 at 09:57

0 Answers0