OnDemandModule.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
if (!$Global:OnDemandModulePath) { $Global:OnDemandModulePath = "$HOME\OnDemandModules" } function Get-OnDemandModule { <# .SYNOPSIS Gets Ondemand-imported modules .DESCRIPTION Scans imported modules and returns the ones imported OnDemand .PARAMETER Name Module Name .PARAMETER ListAvailable returns available modules as well as already imported modules .EXAMPLE Get-OnDemandModule -Name AzureAD .EXAMPLE Get-OnDemandModule -Name ImportExcel -ListAvailable | Import-OndemandModule #> [CmdletBinding()] param ( [string] $Name, [switch] $ListAvailable ) begin { $CurrentPSModulePath = $env:PSModulePath $env:PSModulePath = $env:PSModulePath + ";$OnDemandModulePath" } process { # try # { switch ($true) { ($ListAvailable -and $Name) { $Modules = Get-Module $Name -ListAvailable } ($ListAvailable -and -not($Name)) { $Modules = Get-Module -ListAvailable } (-not($ListAvailable) -and $Name) { $Modules = Get-Module -Name $Name } (-not($ListAvailable) -and -not($Name)) { $Modules = Get-Module } } $Modules = $Modules | Where-Object { $_.Path -like "$OnDemandModulePath*" } # } # catch # { # throw "$Name Module couldn't be found in OndemandModulePath $OnDemandModulePath" # } } end { $env:PSModulePath = $CurrentPSModulePath $Modules } } function Import-OnDemandModule { <# .SYNOPSIS Allows to import modules on demand. .DESCRIPTION When you install modules with Install-Module, it saves the modules to one of folder in `$env:PSModulePath. Those modules are imported automatically evrytime powershell is started. OnDemandModule allows you to import modules with their name only without specifying path .PARAMETER Name Name of the Module you would like to import .PARAMETER Path Path of the module. it is set to `$OnDemandModulePath by default .EXAMPLE Import-OnDemandModule -Name VMWare.PowerCLI #> [CmdletBinding()] param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $Name, [ValidateScript( { Test-Path $_ })] [string] $Path = $OnDemandModulePath ) begin { $CurrentPSModulePath = $env:PSModulePath $env:PSModulePath = $env:PSModulePath + ";$OnDemandModulePath" } process { if (-not(Get-OnDemandModule -Name $Name -ListAvailable)) { $env:PSModulePath = $CurrentPSModulePath throw "$Name Module could not be found!" break } try { Import-Module "$OndemandModulePath\$Name" } catch { $env:PSModulePath = $CurrentPSModulePath throw $Error[0].Message } } end { $env:PSModulePath = $CurrentPSModulePath } } function Install-OnDemandModule { <# .SYNOPSIS It is a just placeholder. It returs a warning only. .DESCRIPTION It is a just placeholder. It returs a warning only. .PARAMETER Name Name of the Module you would like to install. .EXAMPLE Install-OnDemandModule -Name MSOnline #> [CmdletBinding()] param ( [Parameter(Position = 0)] [string] $Name ) begin { } process { Write-Warning "This cmdlet is just a placeholder. Please use `"Save-Module`" cmdlet with `"-Name $Name -Path `$OnDemandModulePath`" parameters it has more options. For more information, run `"Get-Help Save-Module`"" } end { } } function Set-OnDemandModulePath { <# .SYNOPSIS Sets OnDemandModule path to given path. .DESCRIPTION Import-OnDemandModule uses the path defined in `$OnDemandModulePath variable. This cmdlet will add settings to Profile.ps1 with given path. So `$OnDemandModulePath can be set to this path as a part of profile. .PARAMETER Path Desired Path for modules to be used with OnDemandModule .EXAMPLE Set-OnDemandModule -Path "C:\Users\Musa\OnDemandModules" #> [CmdletBinding()] param ( [Parameter( ValueFromPipelineByPropertyName = $true, ValueFromPipeline = $true, Mandatory = $true )] [ValidateScript( { Test-Path $_ })] [string] $Path ) begin { } process { $Path = Resolve-Path -Path $Path $PSFolder = Split-Path -Path $profile -Parent $ProfilePath = Join-Path -Path $PSFolder -ChildPath "Profile.ps1" $PathVariable = "`$Global:OnDemandModulePath = `'$Path`'" if (-not(Test-Path -Path $PSFolder)) { try { New-Item -Path (Split-Path -Path $PSFolder -Parent) -Name (Split-Path -Path $PSFolder -Leaf) -ItemType Directory | Out-Null } catch { throw "Powershell User ($PSFolder) Folder does not exist!" break } } if (-not(Test-Path -Path $ProfilePath)) { try { New-Item -Path $PSFolder -Name Profile.ps1 -ItemType File | Out-Null } catch { throw "User Profile file ($PSFolder\Profile.ps1) does not exist!" break } } if ($ProfilePath -and (Get-Content $ProfilePath | Where-Object { $_ -like '*Global:OnDemandModulePath*' })) { $ProfileContent = Get-Content -Path $ProfilePath $ProfileContent | ForEach-Object { $_ -replace ".*Global:OnDemandModulePath.*", $PathVariable } | Set-Content -Path $ProfilePath -Encoding utf8 } else { $Content = @" `n ################## OnDemandModule Path ################### $PathVariable ########### Generated by OnDemandModule module ########### "@ Add-Content -Path $ProfilePath -Value $Content } $Global:OnDemandModulePath = $Path } end { } } |