Avatar of Chris Cadey
Chris Cadey
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Copy-item script not working properly

having problems getting my script to work. it fundamentally needs to copy an app folder from the server and drop it to the workstations, so I'm using a script to achieve this. however its throwing up multiple errors of the same type, weirdly some files copy ok.

Heres the script :

$pw = ConvertTo-SecureString -AsPlainText -Force -String xxxxxx
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "xxxx\xxxx",$pw
$Session = New-PSSession -ComputerName "server" -Credential $cred
Copy-item -FromSession $Session -Path "D:\xxxxx\Apps" -Destination "C:\xxxxx" -Recurse

Open in new window


Heres the errors :

Copy-item : Method invocation failed because [System.IO.MemoryStream] does not contain a method named 'new'.
At line:4 char:1
+ Copy-item -FromSession $Session -Path "D:\xxxxx\Apps" -Destination  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], RuntimeException
    + FullyQualifiedErrorId : System.Management.Automation.RuntimeException,WriteException

Open in new window

Powershell

Avatar of undefined
Last Comment
yo_bee

8/22/2022 - Mon
Alex

I don't understand why you're putting in the username and password, your better option would be to use a scheduled task and then implement the username from there.

Thus, only leaving you with

Copy-item -FromSession $Session -Path "D:\xxxxx\Apps" -Destination "C:\xxxxx" -Recurse

Open in new window


Or do a run as on your powershell application.

Regards

Alex
Alex

Oh sorry

Do

$Computers = Get-content c:\temp\computers.txt

Foreach ($Computer in $Computer){Copy-item -Path "D:\xxxxx\Apps" -Destination "\\$($Computer)\c$\xxxxx" -Recurse}

Open in new window


If you let me know where you're getting the destination computer, I.E Active directory or something, I can get it from there.
ASKER CERTIFIED SOLUTION
yo_bee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Chris Cadey

ASKER
I'm launching the PowerShell script from our software deployment platform.  The software determines the workstations to install on, whether its all of them or certain selected ones or even just one.  the script remains the same.

can robocopy be used from within powershell?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
yo_bee

Yes:

Here is a script from the MS Gallery that will help.
In Powershell you can run other commands and use variables from powerhsell cmdlets to be passed to the command..

https://gallery.technet.microsoft.com/scriptcenter/Robocoy-Files-to-Remote-bdfc5154
Chris Cadey

ASKER
OK tried it with Robocopy and its worked a treat. many thanks
yo_bee

Robcopy is the $h!t in my world.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.