Copy-Module.ps1

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
#requires -module PowerShellGet

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 7c4258c9-27b3-40c3-91eb-92c8bf3a7175
 
.AUTHOR Alex Sarafian
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Modules
 
.LICENSEURI https://github.com/Sarafian/PowerShellScripts/blob/master/LICENSE
 
.PROJECTURI https://github.com/Sarafian/PowerShellScripts
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>


<#
 
.DESCRIPTION
 Copy a module from one repisotry to anotther
 
#>
 
[CmdletBinding(SupportsShouldProcess=$true)]
param(
    [Parameter(Mandatory=$true)]
    [string]$ModuleName,
    [Parameter(Mandatory=$false)]
    [string]$RequiredVersion=$null,
    [Parameter(Mandatory=$false)]
    [string]$FromRepository="PSGallery",
    [Parameter(Mandatory=$true)]
    [string]$ApiKey,
    [Parameter(Mandatory=$true)]
    [string]$ToRepository
)
Write-Debug "PSCmdlet.ParameterSetName=$($PSCmdlet.ParameterSetName)"

Write-Debug "ModuleName=$ModuleName"
Write-Debug "RequiredVersion=$RequiredVersion"
Write-Debug "FromRepository=$FromRepository"
Write-Debug "ApiKey=$ApiKey"
Write-Debug "ToRepository=$ToRepository"

$savePath=$env:TEMP


$saveModuleHash=@{
    Name=$ModuleName

}
if($FromRepository)
{
    $saveModuleHash.Repository=$FromRepository
}

if(-not $RequiredVersion)
{
    $RequiredVersion=Find-Module @saveModuleHash|Select-Object -ExpandProperty Version -First 1
}
$saveModuleHash.RequiredVersion=$RequiredVersion
$moduleOnTargetRepository=Find-Module -Name $ModuleName -RequiredVersion $RequiredVersion -Repository $ToRepository -ErrorAction SilentlyContinue
if($moduleOnTargetRepository -eq $null)
{
    Save-Module @saveModuleHash -Path $savePath -ErrorAction Stop
    Write-Host "Saved $ModuleName"

    $savedModulePath=Join-Path $savePath $ModuleName
    Publish-Module -Path $savedModulePath -Repository $ToRepository -NuGetApiKey $ApiKey
    Write-Host "Published $ModuleName"
}
else
{
    Write-Warning "$ModuleName with $RequiredVersion is already available on $ToRepository"
}