Shell – Copying a file from a shared folder to a remote machine using PowerShell

powershellscript

I'm a bit stumped and have referenced multiple other entries on both SuperUser and StackOverflow concerning issues similar to mine, but to no avail thus far. I have also read various other websites and such via google search. Still stuck, however. Maybe I'm just dense 🙂

Anyway, I've got a shared folder entitled "WindowsUpdate" on my personal work computer that is available to all other computers (if an Admin) on our LAN here where I work. This folder includes a PowerShell script that automates my update process of which includes finding Windows updates, downloading them and then installing them. I'm using the Windows Update PowerShell Module to accomplish this task. Everything works fine from an update standpoint, except I'd like to also include a small line of code that copies the PowerShell Windows Update Module from my shared folder to the appropriate place on the C:\ of the remote machine. I have been unsuccessful in my numerous trials thus far. Any help will be much appreciated.

I have attempted to use the Copy-Item for this as well as my most recent attempt below in my script.

Echo ""
Echo "Setting Execution policy to BYPASS"
Set-ExecutionPolicy Bypass -Confirm:$false

Echo ""
Echo "Copying Module PSWindowsUpdate to PowerShell Modules Folder"
Invoke-Command -ComputerName AssistDirOffice -ScriptBlock {Copy-Item -Path  \\ASSISTDIROFFICE\WindowsUpdate\PSWindowsUpdate -Destination C:\Windows\System32\WindowsPowerShell\v1.0\Modules}
Pause

Echo""
Echo "Updating Chocolatey Packages"
#Queries Chocolatey.org database for updates to locally installed packages and updates them if necessary
cup all -y -allowemptychecksums

Echo ""
Echo "Importing Powershell Module PSWindowsUpdate"
#Installs Powershell Update module
Import-Module PSWindowsUpdate
refreshenv

Echo ""
Echo "Querying Microsoft Update Server for Windows Updates"
#Queries Microsoft Windows Update servers for updates and install any new  ones found.
Get-WUList

Echo ""
Echo "Hiding Microsoft Security Essentials and Skype Install and Updates"
Hide-WUUpdate -Title "Microsoft Security Essentials*" -Confrim:$false
Hide-WUUpdate -Title "Skpe*" -Confirm:$false
Hide-WUUpdate -Title "Preview*" -Confirm:$false

Echo ""
Echo "Downloading and Installing Windows Updates"
Get-WUInstall -acceptall 

Here is the error message I'm receiving from the above code:

[AssistDirOffice] Connecting to remote server AssistDirOffice failed with the following error message : The WinRM client cannot process the
request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must
be used or the destination machine must be added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that
computers in the TrustedHosts list might not be authenticated. You can get more information about that by running the following command: winrm
help config. For more information, see the about_Remote_Troubleshooting Help topic.

  • CategoryInfo : OpenError: (AssistDirOffice:String) [], PSRemotingTransportException
  • FullyQualifiedErrorId : ServerNotTrusted,PSSessionStateBroken

My questions are as follows:

1) Am I using the correct code and syntax to accomplish this task?
2) There appears to be some sort of security issue involving a trusted host. What's the best way to resolve this?

UPDATE

I've continued fiddling with things and figured out how to make my computer a trusted host on the remote machine; however, I am now receiving this error message:

[AssistDirOffice] Connecting to remote server AssistDirOffice failed with the following error message : The WinRM client cannot process the
request because the server name cannot be resolved. For more information, see the about_Remote_Troubleshooting Help topic.

  • CategoryInfo : OpenError: (AssistDirOffice:String) [], PSRemotingTransportException
  • FullyQualifiedErrorId : ComputerNotFound,PSSessionStateBroken

Other Notes That Might Be Helpful

I'm not trying to connect to an actual server or domain, just my work desktop which is effectively acting as a server I suppose.

Best Answer

Here's the problem(s):

Invoke-Command -ComputerName AssistDirOffice -ScriptBlock {
  Copy-Item -Path \\ASSISTDIROFFICE\WindowsUpdate\update-automation.ps1 
 -Destination C:\Windows\System32\WindowsPowerShell\v1.0\Modules
 }

1) If this is copy/pasted accurately, then your -Destination argument is on it's own line instead of being part of the Copy-Item command.

2) You are invoking Copy-Item on the remote computer (AssistDirOffice), yet you're referencing the paths as if it's running on the local computer.

Try replacing that block with just this:

Copy-Item -Path \\ASSISTDIROFFICE\WindowsUpdate\update-automation.ps1 -Destination C:\Windows\System32\WindowsPowerShell\v1.0\Modules
Related Question