@@ -454,6 +454,87 @@ function ConvertTo-PSResourceGetScript {
454454
455455# endregion
456456
457+ # region String Converter
458+
459+ function ConvertTo-PSResourceGetString {
460+ <#
461+ . SYNOPSIS
462+ Converts PSGet v2 cmdlet usage in a script string to PSResourceGet equivalents.
463+ . PARAMETER InputScript
464+ A string containing PowerShell script text with PSGet v2 commands.
465+ . OUTPUTS
466+ PSCustomObject with: ConvertedScript, Conversions (detail array), Warnings
467+ . EXAMPLE
468+ $result = ConvertTo-PSResourceGetString -InputScript 'Install-Module -Name Pester -Force'
469+ $result.ConvertedScript # → 'Install-PSResource -Name Pester -Reinstall'
470+ #>
471+ [CmdletBinding ()]
472+ [OutputType ([PSCustomObject ])]
473+ param (
474+ [Parameter (Mandatory , ValueFromPipeline )]
475+ [string ] $InputScript
476+ )
477+
478+ process {
479+ # Write to a temp file so the AST parser can process it
480+ $tempFile = [System.IO.Path ]::GetTempFileName() -replace ' \.tmp$' , ' .ps1'
481+ try {
482+ [System.IO.File ]::WriteAllText($tempFile , $InputScript )
483+
484+ $commands = @ (Find-PSGetCommand - Path $tempFile )
485+ $allWarnings = [System.Collections.Generic.List [string ]]::new()
486+
487+ if ($commands.Count -eq 0 ) {
488+ return [PSCustomObject ]@ {
489+ ConvertedScript = $InputScript
490+ Conversions = @ ()
491+ Warnings = @ ()
492+ }
493+ }
494+
495+ # Convert each command and collect results
496+ $conversions = foreach ($cmd in $commands ) {
497+ $result = Convert-PSGetCommand - CommandInfo $cmd
498+ $result | Add-Member - NotePropertyName ' StartOffset' - NotePropertyValue $cmd.StartOffset
499+ $result | Add-Member - NotePropertyName ' EndOffset' - NotePropertyValue $cmd.EndOffset
500+ foreach ($w in $result.Warnings ) { $allWarnings.Add ($w ) }
501+ $result
502+ }
503+
504+ # Apply replacements in reverse offset order
505+ $output = $InputScript
506+ $sortedConversions = $conversions |
507+ Where-Object { $_.Status -eq ' Converted' -and $null -ne $_.ConvertedText } |
508+ Sort-Object - Property StartOffset - Descending
509+
510+ foreach ($conv in $sortedConversions ) {
511+ $output = $output.Substring (0 , $conv.StartOffset ) +
512+ $conv.ConvertedText +
513+ $output.Substring ($conv.EndOffset )
514+ }
515+
516+ return [PSCustomObject ]@ {
517+ ConvertedScript = $output
518+ Conversions = @ ($conversions | ForEach-Object {
519+ [PSCustomObject ]@ {
520+ Line = $_.Line
521+ OriginalText = $_.OriginalText
522+ ConvertedText = $_.ConvertedText
523+ Warnings = $_.Warnings
524+ Status = $_.Status
525+ }
526+ })
527+ Warnings = $allWarnings.ToArray ()
528+ }
529+ }
530+ finally {
531+ Remove-Item $tempFile - Force - ErrorAction SilentlyContinue
532+ }
533+ }
534+ }
535+
536+ # endregion
537+
457538# region Formatting
458539
459540function Format-MigrationReport {
@@ -540,5 +621,6 @@ Export-ModuleMember -Function @(
540621 ' Find-PSGetCommand' ,
541622 ' Convert-PSGetCommand' ,
542623 ' ConvertTo-PSResourceGetScript' ,
624+ ' ConvertTo-PSResourceGetString' ,
543625 ' Format-MigrationReport'
544626)
0 commit comments