JustinGrote / ModuleFast

A "fast and loose" way to install modules from Powershell Gallery quickly. Meant for CICD, not production
Other
77 stars 5 forks source link

PowerShell 5.1 #6

Closed BohrenAn closed 1 year ago

BohrenAn commented 1 year ago

Hi There

I've tested it with PowerShell 5.1 and got an error

iwr bit.ly/modulefast | iex

iex : At line:21 char:36

image

Regards Andres

o-l-a-v commented 1 year ago

That's expected, this is for PowerShell 7+.

See:

The specific error you see is caused by the ternary operator not being implemented in Windows PowerShell:

BohrenAn commented 1 year ago

Thanks for the explanation and the Links.

LaurentDardenne commented 1 year ago

That's expected, this is for PowerShell 7+.

In this case the module should not load under Powershell v5.1.

vandre commented 9 months ago

Is it possible to backport this to Powershell v5.1? My use case is use this with a Bicep template to deploy a VM. The Microsoft image does not come with Powershell 7 pre-installed

BohrenAn commented 9 months ago

@vandre Did you try the Microsoft.PowerShell.PSResourceGet Module? https://blog.icewolf.ch/archive/2023/10/15/sucessor-of-powershellget-called-psresourceget-is-ga/ That has improved the Installation of PowerShell Modules a lot - not quite as fast as ModuleFast but helps a lot. Regards Andres

vandre commented 9 months ago

@BohrenAn Thanks Andres. It is indeed faster. One thing of note I had to Install the Nuget Package Provider manually because it is not available OOB with the Windows Server 2019 Azure VM image

I ended up with the following script:

#Requires -RunAsAdministrator
[CmdletBinding()]
param(
    [Parameter (Mandatory = $true)]
    [string] $pat
)

$IsVerbose = $VerbosePreference -eq "Continue"
$ErrorActionPreference = "Stop"; 
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 

if (-Not (Get-Module -ListAvailable -Name Microsoft.Powershell.PsResourceGet)) { 
    Write-Verbose "Installing PSResourceGet"
    Get-PackageProvider -Name NuGet -ForceBootstrap
    Install-Module -Name Microsoft.PowerShell.PSResourceGet -Repository PsGallery -Force -AllowClobber
}

$patToken = $pat | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential("PAT", $patToken)
Register-PSResourceRepository -Trusted -Name AzRepo -Uri 'https://pkgs.dev.azure.com/MyOrg/MyProject/_packaging/MyFeed/nuget/v3/index.json' -Force
Install-PSResource MyPackage -Repository AzRepo -Credential $credential -Scope AllUsers -Verbose:$IsVerbose