From 807f7fef2b8363eb1616aa863072d441de9fe877 Mon Sep 17 00:00:00 2001 From: HvdMark Date: Mon, 17 Mar 2025 16:38:58 +0100 Subject: [PATCH 1/2] Added checks --- .gitignore | 7 ++ InstallModuleFromGitHub.psd1 | 2 +- InstallModuleFromGitHub.psm1 | 180 +++++++++++++++++++++-------------- README.md | 11 +++ 4 files changed, 128 insertions(+), 72 deletions(-) diff --git a/.gitignore b/.gitignore index cd2946a..932c104 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,10 @@ $RECYCLE.BIN/ Network Trash Folder Temporary Items .apdisk + +test.ps1 +.profile.ps1 +.git +.vscode +launch.json +.gitignore \ No newline at end of file diff --git a/InstallModuleFromGitHub.psd1 b/InstallModuleFromGitHub.psd1 index cb4ae0a..d8a0191 100644 --- a/InstallModuleFromGitHub.psd1 +++ b/InstallModuleFromGitHub.psd1 @@ -4,7 +4,7 @@ RootModule = 'InstallModuleFromGitHub.psm1' # Version number of this module. - ModuleVersion = '1.7.0' + ModuleVersion = '1.8.0' # ID used to uniquely identify this module GUID = '2997e240-5cec-4543-a231-573576b78c88' diff --git a/InstallModuleFromGitHub.psm1 b/InstallModuleFromGitHub.psm1 index dd07c85..600fe71 100644 --- a/InstallModuleFromGitHub.psm1 +++ b/InstallModuleFromGitHub.psm1 @@ -1,4 +1,5 @@ -function Install-ModuleFromGitHub { +function Install-ModuleFromGitHub +{ [CmdletBinding()] param( $GitHubRepo, @@ -8,104 +9,141 @@ function Install-ModuleFromGitHub { $DestinationPath, $SSOToken, $moduleName, - [ValidateSet('CurrentUser','AllUsers')] + [ValidateSet('CurrentUser', 'AllUsers')] [string] $Scope ) - Process { - if($PSBoundParameters.ContainsKey("ProjectUri")) { + Process + { + if ($PSBoundParameters.ContainsKey("ProjectUri")) + { $GitHubRepo = $null - if($ProjectUri.OriginalString.StartsWith("https://github.com")) { + if ($ProjectUri.OriginalString.StartsWith("https://github.com")) + { $GitHubRepo = $ProjectUri.AbsolutePath - } else { - $name=$ProjectUri.LocalPath.split('/')[-1] + } + else + { + $name = $ProjectUri.LocalPath.split('/')[-1] Write-Host -ForegroundColor Red ("Module [{0}]: not installed, it is not hosted on GitHub " -f $name) } } - if($GitHubRepo) { - Write-Verbose ("[$(Get-Date)] Retrieving {0} {1}" -f $GitHubRepo, $Branch) - - $url = "https://api.github.com/repos/{0}/zipball/{1}" -f $GitHubRepo, $Branch + if ($GitHubRepo) + { + $url = "https://api.github.com/repos/{0}/zipball/{1}" -f $GitHubRepo, $Branch + Write-Verbose ("[$(Get-Date)] Retrieving {0} {1}" -f $GitHubRepo, $Branch) + Write-Verbose ("[$(Get-Date)] Download from {0}" -f $url) - if ($moduleName) { - $targetModuleName = $moduleName - } else { - $targetModuleName=$GitHubRepo.split('/')[-1] - } - Write-Debug "targetModuleName: $targetModuleName" + if ($moduleName) + { + $targetModuleName = $moduleName + } + else + { + $targetModuleName = $GitHubRepo.split('/')[-1] + } + Write-Debug "targetModuleName: $targetModuleName" - $tmpDir = [System.IO.Path]::GetTempPath() + $tmpDir = [System.IO.Path]::GetTempPath() - $OutFile = Join-Path -Path $tmpDir -ChildPath "$($targetModuleName).zip" - Write-Debug "OutFile: $OutFile" + $OutFile = Join-Path -Path $tmpDir -ChildPath "$($targetModuleName).zip" + Write-Debug "OutFile: $OutFile" - if ($SSOToken) {$headers = @{"Authorization" = "token $SSOToken" }} + if ($SSOToken) { $headers = @{"Authorization" = "token $SSOToken" } } - #enable TLS1.2 encryption - if (-not ($IsLinux -or $IsMacOS)) { - [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 - } - Invoke-RestMethod $url -OutFile $OutFile -Headers $headers - if (-not ([System.Environment]::OSVersion.Platform -eq "Unix")) { - Unblock-File $OutFile - } + #enable TLS1.2 encryption + if (-not ($IsLinux -or $IsMacOS)) + { + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + } + Invoke-RestMethod $url -OutFile $OutFile -Headers $headers + if (-not ([System.Environment]::OSVersion.Platform -eq "Unix")) + { + Unblock-File $OutFile + } - $fileHash = $(Get-FileHash -Path $OutFile).hash - $tmpDir = "$tmpDir/$fileHash" + $fileHash = $(Get-FileHash -Path $OutFile).hash + $tmpDir = "$tmpDir/$fileHash" - Expand-Archive -Path $OutFile -DestinationPath $tmpDir -Force + if (Test-Path $tmpDir) + { + Write-Warning "Folder $Folder already exists, re-using content" + } + else + { + New-Item $tmpDir -ItemType Directory + } + + Expand-Archive -Path $OutFile -DestinationPath $tmpDir -Force - $unzippedArchive = get-childItem "$tmpDir" - Write-Debug "targetModule: $targetModule" + $unzippedArchive = Get-ChildItem "$tmpDir" + Write-Debug "targetModule: $targetModule" - if ([System.Environment]::OSVersion.Platform -eq "Unix") { - if ($Scope -eq "CurrentUser") { - $dest = Join-Path -Path $HOME -ChildPath ".local/share/powershell/Modules" - } else { - $dest = "/usr/local/share/powershell/Modules" - } + if ([System.Environment]::OSVersion.Platform -eq "Unix") + { + if ($Scope -eq "CurrentUser") + { + $dest = Join-Path -Path $HOME -ChildPath ".local/share/powershell/Modules" } - - else { - if ($Scope -eq "CurrentUser") { - $scopedPath = $HOME - $scopedChildPath = "\Documents\PowerShell\Modules" - } else { - $scopedPath = $env:ProgramFiles - $scopedChildPath = "\PowerShell\Modules" - } - $dest = Join-Path -Path $scopedPath -ChildPath $scopedChildPath + else + { + $dest = "/usr/local/share/powershell/Modules" } + } - if($DestinationPath) { - $dest = $DestinationPath + else + { + if ($Scope -eq "CurrentUser") + { + $scopedPath = $HOME + $scopedChildPath = "\Documents\PowerShell\Modules" } - $dest = Join-Path -Path $dest -ChildPath $targetModuleName - if ([System.Environment]::OSVersion.Platform -eq "Unix") { - $psd1 = Get-ChildItem (Join-Path -Path $unzippedArchive -ChildPath *) -Include *.psd1 -Recurse - } else { - $psd1 = Get-ChildItem (Join-Path -Path $tmpDir -ChildPath $unzippedArchive.Name) -Include *.psd1 -Recurse - } - - $sourcePath = $unzippedArchive.FullName - - if($psd1) { - $ModuleVersion=(Get-Content -Raw $psd1.FullName | Invoke-Expression).ModuleVersion - $dest = Join-Path -Path $dest -ChildPath $ModuleVersion - $null = New-Item -ItemType directory -Path $dest -Force - $sourcePath = $psd1.DirectoryName + else + { + $scopedPath = $env:ProgramFiles + $scopedChildPath = "\PowerShell\Modules" } + $dest = Join-Path -Path $scopedPath -ChildPath $scopedChildPath + } + + if ($DestinationPath) + { + $dest = $DestinationPath + } + $dest = Join-Path -Path $dest -ChildPath $targetModuleName + if ([System.Environment]::OSVersion.Platform -eq "Unix") + { + $psd1 = Get-ChildItem (Join-Path -Path $unzippedArchive -ChildPath *) -Include *.psd1 -Recurse + } + else + { + $psd1 = Get-ChildItem (Join-Path -Path $tmpDir -ChildPath $unzippedArchive.Name) -Include *.psd1 -Recurse + } + + $sourcePath = $unzippedArchive.FullName + + if ($psd1) + { + $ModuleVersion = (Get-Content -Raw $psd1.FullName | Invoke-Expression).ModuleVersion + $dest = Join-Path -Path $dest -ChildPath $ModuleVersion + $null = New-Item -ItemType directory -Path $dest -Force + $sourcePath = $psd1.DirectoryName + } - if ([System.Environment]::OSVersion.Platform -eq "Unix") { - $null = Copy-Item "$(Join-Path -Path $unzippedArchive -ChildPath *)" $dest -Force -Recurse - } else { - $null = Copy-Item "$sourcePath\*" $dest -Force -Recurse - } + if ([System.Environment]::OSVersion.Platform -eq "Unix") + { + $null = Copy-Item "$(Join-Path -Path $unzippedArchive -ChildPath *)" $dest -Force -Recurse + } + else + { + $null = Copy-Item "$sourcePath\*" $dest -Force -Recurse + } } + Write-Host "Installer finished" } } diff --git a/README.md b/README.md index f7983f5..123c214 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,17 @@ Not all PowerShell Modules are published to the PowerShellGallery but are hosted ## Changes +## 1.8.0 +- Add: Create folder first before expanding archive +- Add: Test-Path to see if tmpDir already exist + +## 1.7.0 +https://github.com/dfinke/InstallModuleFromGitHub/pull/32 +- Add tab-completion for -Scope by @cspotcode + +https://github.com/dfinke/InstallModuleFromGitHub/pull/25 +- fix unzippedArchive path when searching for psd1 file by @joshschmitter + ## 1.6.0 via https://github.com/dfinke/InstallModuleFromGitHub/pull/25 From ca617bff038ef014281b198c0b58f5698cbbb698 Mon Sep 17 00:00:00 2001 From: HvdMark Date: Wed, 19 Mar 2025 15:08:00 +0100 Subject: [PATCH 2/2] Added verbose messages and fixed PS5.1/7.0 issue and fixed Documents folder location --- InstallModuleFromGitHub.psd1 | 2 +- InstallModuleFromGitHub.psm1 | 65 ++++++++++++++++++++++++++++-------- README.md | 15 +++++++++ 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/InstallModuleFromGitHub.psd1 b/InstallModuleFromGitHub.psd1 index d8a0191..906eae5 100644 --- a/InstallModuleFromGitHub.psd1 +++ b/InstallModuleFromGitHub.psd1 @@ -4,7 +4,7 @@ RootModule = 'InstallModuleFromGitHub.psm1' # Version number of this module. - ModuleVersion = '1.8.0' + ModuleVersion = '1.8.1' # ID used to uniquely identify this module GUID = '2997e240-5cec-4543-a231-573576b78c88' diff --git a/InstallModuleFromGitHub.psm1 b/InstallModuleFromGitHub.psm1 index 600fe71..9599546 100644 --- a/InstallModuleFromGitHub.psm1 +++ b/InstallModuleFromGitHub.psm1 @@ -11,11 +11,13 @@ function Install-ModuleFromGitHub $moduleName, [ValidateSet('CurrentUser', 'AllUsers')] [string] - $Scope + $Scope = "CurrentUser" ) Process { + Write-Verbose ("[$(Get-Date)] Powershell : {0}.{1}" -f $PSVersionTable.psVersion.Major, $PSVersionTable.psVersion.Minor) + if ($PSBoundParameters.ContainsKey("ProjectUri")) { $GitHubRepo = $null @@ -33,8 +35,10 @@ function Install-ModuleFromGitHub if ($GitHubRepo) { $url = "https://api.github.com/repos/{0}/zipball/{1}" -f $GitHubRepo, $Branch - Write-Verbose ("[$(Get-Date)] Retrieving {0} {1}" -f $GitHubRepo, $Branch) - Write-Verbose ("[$(Get-Date)] Download from {0}" -f $url) + Write-Verbose ("[$(Get-Date)] Repository : {0}" -f "https://api.github.com/repos/$GitHubRepo") + Write-Verbose ("[$(Get-Date)] Branch : {0}" -f "$Branch") + Write-Verbose ("[$(Get-Date)] Retrieving : {0} {1}" -f $GitHubRepo, $Branch) + Write-Verbose ("[$(Get-Date)] Download from : {0}" -f $url) if ($moduleName) { @@ -44,14 +48,24 @@ function Install-ModuleFromGitHub { $targetModuleName = $GitHubRepo.split('/')[-1] } + Write-Verbose ("[$(Get-Date)] targetModuleName : {0}" -f $targetModuleName) Write-Debug "targetModuleName: $targetModuleName" $tmpDir = [System.IO.Path]::GetTempPath() $OutFile = Join-Path -Path $tmpDir -ChildPath "$($targetModuleName).zip" + Write-Verbose ("[$(Get-Date)] Output File : {0}" -f $OutFile) Write-Debug "OutFile: $OutFile" - if ($SSOToken) { $headers = @{"Authorization" = "token $SSOToken" } } + if ($SSOToken) + { + $headers = @{"Authorization" = "token $SSOToken" } + Write-Verbose ("[$(Get-Date)] SSO Token : {0}" -f $SSOToken) + } + else + { + Write-Verbose ("[$(Get-Date)] SSO Token : N/A") + } #enable TLS1.2 encryption if (-not ($IsLinux -or $IsMacOS)) @@ -59,6 +73,7 @@ function Install-ModuleFromGitHub [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } Invoke-RestMethod $url -OutFile $OutFile -Headers $headers + if (-not ([System.Environment]::OSVersion.Platform -eq "Unix")) { Unblock-File $OutFile @@ -69,17 +84,19 @@ function Install-ModuleFromGitHub if (Test-Path $tmpDir) { - Write-Warning "Folder $Folder already exists, re-using content" + Write-Warning ("[$(Get-Date)] Checking Folder : {0} already exists, re-using content" -f $tmpDir) } else { New-Item $tmpDir -ItemType Directory + Write-Verbose ("[$(Get-Date)] Checking Folder : {0} created" -f $tmpDir) } Expand-Archive -Path $OutFile -DestinationPath $tmpDir -Force $unzippedArchive = Get-ChildItem "$tmpDir" Write-Debug "targetModule: $targetModule" + Write-Verbose ("[$(Get-Date)] targetModuleName : {0}" -f $targetModuleName) if ([System.Environment]::OSVersion.Platform -eq "Unix") { @@ -92,19 +109,38 @@ function Install-ModuleFromGitHub $dest = "/usr/local/share/powershell/Modules" } } - else { + Write-Verbose ("[$(Get-Date)] Using Scope : {0}" -f $Scope) if ($Scope -eq "CurrentUser") { - $scopedPath = $HOME - $scopedChildPath = "\Documents\PowerShell\Modules" + # Current User + if ($psVersionTable.PSVersion.Major -eq "5") + { + $scopedPath = [Environment]::GetFolderPath('MyDocuments') + $scopedChildPath = "\WindowsPowerShell\Modules" + } + elseif ($psVersionTable.PSVersion.Major -eq "7") + { + $scopedPath = [Environment]::GetFolderPath('MyDocuments') + $scopedChildPath = "\PowerShell\Modules" + } } else { - $scopedPath = $env:ProgramFiles - $scopedChildPath = "\PowerShell\Modules" + # All Users + if ($psVersionTable.PSVersion.Major -eq "5") + { + $scopedPath = $env:ProgramFiles + $scopedChildPath = "\WindowsPowerShell\Modules" + } + elseif ($psVersionTable.PSVersion.Major -eq "7") + { + $scopedPath = $env:ProgramFiles + $scopedChildPath = "\PowerShell\Modules" + } } + $dest = Join-Path -Path $scopedPath -ChildPath $scopedChildPath } @@ -113,6 +149,7 @@ function Install-ModuleFromGitHub $dest = $DestinationPath } $dest = Join-Path -Path $dest -ChildPath $targetModuleName + if ([System.Environment]::OSVersion.Platform -eq "Unix") { $psd1 = Get-ChildItem (Join-Path -Path $unzippedArchive -ChildPath *) -Include *.psd1 -Recurse @@ -127,13 +164,15 @@ function Install-ModuleFromGitHub if ($psd1) { $ModuleVersion = (Get-Content -Raw $psd1.FullName | Invoke-Expression).ModuleVersion + Write-Verbose ("[$(Get-Date)] Module version : {0}" -f $ModuleVersion) + $dest = Join-Path -Path $dest -ChildPath $ModuleVersion + Write-Verbose ("[$(Get-Date)] Destination : {0}" -f $dest) + $null = New-Item -ItemType directory -Path $dest -Force $sourcePath = $psd1.DirectoryName } - - if ([System.Environment]::OSVersion.Platform -eq "Unix") { $null = Copy-Item "$(Join-Path -Path $unzippedArchive -ChildPath *)" $dest -Force -Recurse @@ -143,7 +182,7 @@ function Install-ModuleFromGitHub $null = Copy-Item "$sourcePath\*" $dest -Force -Recurse } } - Write-Host "Installer finished" + Write-Verbose "[$(Get-Date)] Status : Installer finished" } } diff --git a/README.md b/README.md index 123c214..23d001b 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,21 @@ Not all PowerShell Modules are published to the PowerShellGallery but are hosted ## Changes +## 1.8.1 +- Add: Default $Scope to CurrentUser if $Scope is'nt entered +- Add: More verbose output +- Fix: PSModulePath is different between Windows Powershell 5.1 and Powershell 7.0 + $HOME can't be used when documents folder is moved with folder redirection + + Current User: + 5.1 [Environment]::GetFolderPath('MyDocuments')\WindowsPowerShell\Modules + 7.0 [Environment]::GetFolderPath('MyDocuments')\PowerShell\Modules + + All User: + 5.1 $env:ProgramFiles\WindowsPowerShell\Modules + 7.0 $env:ProgramFiles\PowerShell\Modules + + ## 1.8.0 - Add: Create folder first before expanding archive - Add: Test-Path to see if tmpDir already exist