{"id":572,"date":"2021-09-24T09:57:44","date_gmt":"2021-09-24T07:57:44","guid":{"rendered":"https:\/\/cymbeline.ch\/?p=572"},"modified":"2021-09-24T09:57:46","modified_gmt":"2021-09-24T07:57:46","slug":"copy-files-with-powershell-remoting","status":"publish","type":"post","link":"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/","title":{"rendered":"Copy files with PowerShell Remoting"},"content":{"rendered":"\n

Recently, at work, I found myself in the situation where I needed to copy some file from my workstation to a jump box. Now of course, on Linux I’d just use rsync<\/code> or scp<\/code>. But our IT doesn’t like provisioning Linux boxes and therefore uses Windows for jump servers too, so no luck here. Luckily, I could convince them to turn on and allow PowerShell Remoting, so with some simple scripts I can still easily copy files over without using SMB and looking at more hassle with IT.<\/p>\n\n\n

\nfunction Copy-LocalToRemote(\n    [Parameter(Mandatory = $true)] $LocalPath,\n    [Parameter(Mandatory = $true)] $RemotePath,\n    $ComputerName = 'my.default.target.host'\n) {\n    Invoke-Command -ComputerName $ComputerName `\n        {\n            param($path, $content)\n            Set-Content -Path $path -Value $content `\n                -AsByteStream\n        } `\n        -ArgumentList $RemotePath,(\n            Get-Content $LocalPath Raw -AsByteStream)\n}\n\nfunction Copy-RemoteToLocal(\n    [Parameter(Mandatory = $true)] $RemotePath,\n    [Parameter(Mandatory = $true)] $LocalPath,\n    $ComputerName = 'my.default.source.host'\n) {\n    Invoke-Command -ComputerName $ComputerName `\n        {\n            param($path)\n            Get-Content -Path $path -Raw -AsByteStream\n        } `\n        -ArgumentList $RemotePath |\n    Set-Content -Path $LocalPath -AsByteStream\n}\n\nNew-Alias -Name 'ltr' -Value 'Copy-LocalToRemote'\nNew-Alias -Name 'rtl' -Value 'Copy-RemoteToLocal'\n<\/pre><\/div>\n\n\n

As you can see, this is quite simple. Obviously, functions above can only copy one file at a time though. Maybe in the future I’ll build something that can copy entire file structures recursively. I also haven’t spent any time looking at how efficient it is to pass streams this way. In fact, I wouldn’t be surprised at all if this would perform poorly for large files. But then again, I’m mostly pushing around scripts and config files, so this works just fine.<\/p>\n

<\/p>","protected":false},"excerpt":{"rendered":"

Recently, at work, I found myself in the situation where I needed to copy some file from my workstation to a jump box. Now of course, on Linux I’d just use rsync or scp. But our IT doesn’t like provisioning Linux boxes and therefore uses Windows for jump servers too, so no luck here. Luckily, … Continue reading “Copy files with PowerShell Remoting”<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[61],"tags":[33],"yoast_head":"\nCopy files with PowerShell Remoting - Tales of a Code Monkey<\/title>\n<meta name=\"description\" content=\"How to copy files between machines using only PowerShell Remoting. Basic rsync or scp through PowerShell when better tools just aren't there.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Copy files with PowerShell Remoting - Tales of a Code Monkey\" \/>\n<meta property=\"og:description\" content=\"How to copy files between machines using only PowerShell Remoting. Basic rsync or scp through PowerShell when better tools just aren't there.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/\" \/>\n<meta property=\"og:site_name\" content=\"Tales of a Code Monkey\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-24T07:57:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-24T07:57:46+00:00\" \/>\n<meta name=\"author\" content=\"roger\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"roger\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/\",\"url\":\"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/\",\"name\":\"Copy files with PowerShell Remoting - Tales of a Code Monkey\",\"isPartOf\":{\"@id\":\"https:\/\/cymbeline.ch\/#website\"},\"datePublished\":\"2021-09-24T07:57:44+00:00\",\"dateModified\":\"2021-09-24T07:57:46+00:00\",\"author\":{\"@id\":\"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916\"},\"description\":\"How to copy files between machines using only PowerShell Remoting. Basic rsync or scp through PowerShell when better tools just aren't there.\",\"breadcrumb\":{\"@id\":\"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cymbeline.ch\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Copy files with PowerShell Remoting\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cymbeline.ch\/#website\",\"url\":\"https:\/\/cymbeline.ch\/\",\"name\":\"Tales of a Code Monkey\",\"description\":\"... the adventures of a guy making software.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cymbeline.ch\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916\",\"name\":\"roger\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cymbeline.ch\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g\",\"caption\":\"roger\"},\"sameAs\":[\"https:\/\/www.cymbeline.ch\"],\"url\":\"https:\/\/cymbeline.ch\/author\/roger\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Copy files with PowerShell Remoting - Tales of a Code Monkey","description":"How to copy files between machines using only PowerShell Remoting. Basic rsync or scp through PowerShell when better tools just aren't there.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/","og_locale":"en_US","og_type":"article","og_title":"Copy files with PowerShell Remoting - Tales of a Code Monkey","og_description":"How to copy files between machines using only PowerShell Remoting. Basic rsync or scp through PowerShell when better tools just aren't there.","og_url":"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/","og_site_name":"Tales of a Code Monkey","article_published_time":"2021-09-24T07:57:44+00:00","article_modified_time":"2021-09-24T07:57:46+00:00","author":"roger","twitter_misc":{"Written by":"roger","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/","url":"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/","name":"Copy files with PowerShell Remoting - Tales of a Code Monkey","isPartOf":{"@id":"https:\/\/cymbeline.ch\/#website"},"datePublished":"2021-09-24T07:57:44+00:00","dateModified":"2021-09-24T07:57:46+00:00","author":{"@id":"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916"},"description":"How to copy files between machines using only PowerShell Remoting. Basic rsync or scp through PowerShell when better tools just aren't there.","breadcrumb":{"@id":"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cymbeline.ch\/2021\/09\/24\/copy-files-with-powershell-remoting\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cymbeline.ch\/"},{"@type":"ListItem","position":2,"name":"Copy files with PowerShell Remoting"}]},{"@type":"WebSite","@id":"https:\/\/cymbeline.ch\/#website","url":"https:\/\/cymbeline.ch\/","name":"Tales of a Code Monkey","description":"... the adventures of a guy making software.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cymbeline.ch\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916","name":"roger","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cymbeline.ch\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g","caption":"roger"},"sameAs":["https:\/\/www.cymbeline.ch"],"url":"https:\/\/cymbeline.ch\/author\/roger\/"}]}},"_links":{"self":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts\/572"}],"collection":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/comments?post=572"}],"version-history":[{"count":3,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts\/572\/revisions"}],"predecessor-version":[{"id":575,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts\/572\/revisions\/575"}],"wp:attachment":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/media?parent=572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/categories?post=572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/tags?post=572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}