Skip to content

Commit 618e016

Browse files
Add PSGet to PSResourceGet migration tool
Adds a PowerShell-based migration tool that scans .ps1/.psm1/.psd1 files for PowerShellGet v2 cmdlet usage and converts them to PSResourceGet equivalents using AST-based parsing. Features: - 25 cmdlet mappings (Find-Module → Find-PSResource, etc.) - Parameter transformations (version ranges, renames, removals) - Dry-run report mode and in-place apply with backups - 30 Pester tests covering all conversion scenarios Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 6ecc668 commit 618e016

5 files changed

Lines changed: 1073 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
<#
5+
.SYNOPSIS
6+
Converts PowerShellGet v2 cmdlet usage to PSResourceGet equivalents.
7+
8+
.DESCRIPTION
9+
Scans PowerShell files (.ps1, .psm1, .psd1) for PSGet v2 cmdlet usage
10+
(e.g., Install-Module, Find-Module) and converts them to their
11+
PSResourceGet equivalents (e.g., Install-PSResource, Find-PSResource).
12+
13+
By default, runs in WhatIf/report mode. Use -Apply to modify files.
14+
15+
.PARAMETER Path
16+
Path to a file or directory to scan. Supports wildcards.
17+
Defaults to the current directory.
18+
19+
.PARAMETER Recurse
20+
When Path is a directory, scan subdirectories recursively.
21+
22+
.PARAMETER Apply
23+
Apply the changes to files in-place. Without this switch, only a report is generated.
24+
25+
.PARAMETER BackupPath
26+
Directory for backup copies. Defaults to .bak files alongside originals.
27+
28+
.PARAMETER PassThru
29+
Emit structured result objects to the pipeline instead of formatted output.
30+
31+
.EXAMPLE
32+
# Dry-run: scan current directory recursively and show migration report
33+
.\ConvertTo-PSResourceGet.ps1 -Path . -Recurse
34+
35+
.EXAMPLE
36+
# Apply changes with backups
37+
.\ConvertTo-PSResourceGet.ps1 -Path .\scripts -Recurse -Apply -BackupPath .\backups
38+
39+
.EXAMPLE
40+
# Scan a single file and get structured output
41+
.\ConvertTo-PSResourceGet.ps1 -Path .\deploy.ps1 -PassThru
42+
#>
43+
44+
[CmdletBinding(SupportsShouldProcess)]
45+
param(
46+
[Parameter(Position = 0)]
47+
[string] $Path = '.',
48+
49+
[switch] $Recurse,
50+
51+
[switch] $Apply,
52+
53+
[string] $BackupPath,
54+
55+
[switch] $PassThru
56+
)
57+
58+
# Import the migration module
59+
$modulePath = Join-Path $PSScriptRoot 'PSGetMigration.psm1'
60+
Import-Module $modulePath -Force
61+
62+
# Resolve files to scan
63+
$resolvedPath = Resolve-Path -Path $Path -ErrorAction Stop
64+
65+
$filesToScan = if (Test-Path $resolvedPath.Path -PathType Container) {
66+
$gciParams = @{
67+
Path = $resolvedPath.Path
68+
Include = @('*.ps1', '*.psm1', '*.psd1')
69+
File = $true
70+
}
71+
if ($Recurse) {
72+
$gciParams['Recurse'] = $true
73+
}
74+
Get-ChildItem @gciParams
75+
}
76+
else {
77+
Get-Item $resolvedPath.Path
78+
}
79+
80+
if (-not $filesToScan) {
81+
Write-Host "No PowerShell files found at '$Path'." -ForegroundColor Yellow
82+
return
83+
}
84+
85+
Write-Host "Scanning $($filesToScan.Count) file(s)..." -ForegroundColor Cyan
86+
87+
# Process each file
88+
$convertParams = @{}
89+
if ($Apply) { $convertParams['Apply'] = $true }
90+
if ($BackupPath) { $convertParams['BackupPath'] = $BackupPath }
91+
92+
$results = $filesToScan | ForEach-Object {
93+
ConvertTo-PSResourceGetScript -Path $_.FullName @convertParams
94+
}
95+
96+
if ($PassThru) {
97+
$results
98+
}
99+
else {
100+
$results | Format-MigrationReport
101+
}

tool/migration/PSGetMigration.psd1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
@{
5+
RootModule = 'PSGetMigration.psm1'
6+
ModuleVersion = '0.1.0'
7+
GUID = 'a3f7b2c1-4d5e-6f78-9a0b-c1d2e3f4a5b6'
8+
Author = 'Microsoft Corporation'
9+
CompanyName = 'Microsoft Corporation'
10+
Copyright = '(c) Microsoft Corporation. All rights reserved.'
11+
Description = 'Migration tool to convert PowerShellGet v2 (PSGet) cmdlet usage to PSResourceGet equivalents.'
12+
PowerShellVersion = '5.1'
13+
FunctionsToExport = @(
14+
'Get-PSGetCommandMapping',
15+
'Get-PSGetParameterMapping',
16+
'Find-PSGetCommand',
17+
'Convert-PSGetCommand',
18+
'ConvertTo-PSResourceGetScript',
19+
'Format-MigrationReport'
20+
)
21+
CmdletsToExport = @()
22+
VariablesToExport = @()
23+
AliasesToExport = @()
24+
PrivateData = @{
25+
PSData = @{
26+
Tags = @('Migration', 'PSResourceGet', 'PowerShellGet', 'PSGet')
27+
ProjectUri = 'https://github.com/PowerShell/PSResourceGet'
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)