{"id":680,"date":"2024-01-18T13:13:00","date_gmt":"2024-01-18T13:13:00","guid":{"rendered":"http:\/\/draith.com\/?p=680"},"modified":"2024-03-06T01:20:02","modified_gmt":"2024-03-06T01:20:02","slug":"powershell-github-and-the-gh-cli","status":"publish","type":"post","link":"http:\/\/draith.com\/?p=680","title":{"rendered":"PowerShell, GitHub, and the GH Cli"},"content":{"rendered":"\n<p>Here&#8217;s another quick one &#8211; using the GitHub CLI to copy a repository from GitHub, bring it down to your local machine, and then copy it to a remote server.  Great for quickly setting up a new automation server or re-baseline an existing box.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Before running the script, ensure:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You have permissions to access the remote server and write to the target directory.<\/li>\n\n\n\n<li>PowerShell Remoting is enabled on the remote server if you&#8217;re using a PowerShell session for the transfer.<\/li>\n\n\n\n<li>The <a href=\"https:\/\/cli.github.com\/\" data-type=\"link\" data-id=\"https:\/\/cli.github.com\/\">GitHub CLI <\/a>is installed and configured on your machine.<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s the script!<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\"># Define variables\n$repoUrl = &quot;https:\/\/github.com\/username\/repository&quot; # Replace with your GitHub repository URL\n$tempDir = Join-Path -Path $env:TEMP -ChildPath &quot;github_repo_temp&quot;\n$remoteServer = &quot;\\\\remote-server\\share&quot; # Replace with your remote server share path\n$remotePath = &quot;path\\to\\destination\\folder&quot; # Replace with your remote destination path, relative to the share\n\n# Ensure the temp directory does not already exist\nif (Test-Path -Path $tempDir) {\n    Remove-Item -Path $tempDir -Recurse\n}\n\n# Clone the repository to the temporary directory\ngh repo clone $repoUrl $tempDir\n\n# Assuming the remote server is accessible via a network share\n$fullRemotePath = Join-Path -Path $remoteServer -ChildPath $remotePath\n\n# Ensure the remote directory exists\nif (-not (Test-Path -Path $fullRemotePath)) {\n    New-Item -Path $fullRemotePath -ItemType Directory\n}\n\n# Copy the contents to the remote server\nCopy-Item -Path &quot;$tempDir\\*&quot; -Destination $fullRemotePath -Recurse\n\n# Clean up the temporary directory\nRemove-Item -Path $tempDir -Recurse\n\nWrite-Host &quot;Repository contents have been copied to the remote server.&quot;\n<\/pre><\/div>\n\n\n\n<p>And that&#8217;s it!  Consider putting it in a scheduler to always ensure that your remote automation servers are up to date and using your latest code.<\/p>\n\n\n\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s another quick one &#8211; using the GitHub CLI to copy a repository from GitHub, bring it down to your local machine, and then copy it to a remote server. Great for quickly setting up a new automation server or re-baseline an existing box. Before running the script, ensure: Here&#8217;s the script! And that&#8217;s it! &hellip; <a href=\"http:\/\/draith.com\/?p=680\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PowerShell, GitHub, and the GH Cli&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[28,16],"class_list":["post-680","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-github","tag-powershell"],"_links":{"self":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=680"}],"version-history":[{"count":1,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/680\/revisions"}],"predecessor-version":[{"id":681,"href":"http:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/680\/revisions\/681"}],"wp:attachment":[{"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=680"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}