diff --git a/src/DataMigration/DataMigration.Autorest/custom/Cmdlets/New-AzDataMigrationLoginsMigration.ps1 b/src/DataMigration/DataMigration.Autorest/custom/Cmdlets/New-AzDataMigrationLoginsMigration.ps1 index 06245f2de50a..214d2db8df81 100644 --- a/src/DataMigration/DataMigration.Autorest/custom/Cmdlets/New-AzDataMigrationLoginsMigration.ps1 +++ b/src/DataMigration/DataMigration.Autorest/custom/Cmdlets/New-AzDataMigrationLoginsMigration.ps1 @@ -78,77 +78,66 @@ function New-AzDataMigrationLoginsMigration #Defining Default Output Path $DefaultOutputFolder = Get-DefaultLoginMigrationsOutputFolder - #Defining Base and Exe paths - $BaseFolder = Join-Path -Path $DefaultOutputFolder -ChildPath Downloads; - $ExePath = Join-Path -Path $BaseFolder -ChildPath Logins.Console.csproj\Logins.Console.exe; + #Defining Downloads folder + $DownloadsFolder = Join-Path -Path $DefaultOutputFolder -ChildPath Downloads; - #Checking if BaseFolder Path is valid or not - if(-Not (Test-Path $BaseFolder)) + #Checking if DownloadsFolder Path is valid or not + if(-Not (Test-Path $DownloadsFolder)) { - $null = New-Item -Path $BaseFolder -ItemType "directory" + $null = New-Item -Path $DownloadsFolder -ItemType "directory" + } + else + { + #Delete old Login console app files + Delete-OldLoginConsoleApp $DownloadsFolder; } - #Testing Whether Console App is downloaded or not - $TestExePath = Test-Path -Path $ExePath; - - #Console app download address - $ZipSource = "https://sqlassess.blob.core.windows.net/app/LoginsMigration.zip"; - $ZipDestination = Join-Path -Path $BaseFolder -ChildPath "LoginsMigration.zip"; + #Determine latest version of Login console app + $PackageId = "Microsoft.SqlServer.Migration.LoginsConsoleApp" + $LatestNugetOrgDetails = Get-LatestConsoleAppVersionFromNugetOrg $PackageId - #Downloading and extracting LoginsMigration Zip file - if(-Not $TestExePath) + #Determine local version of Login console app + $ConsoleAppFolders = Get-ChildItem -Path $DownloadsFolder -Filter "$PackageId.*" + $LatestLocalNameAndVersion = "" + if ($ConsoleAppFolders.Length -gt 0) { - #Downloading and extracting LoginMigration Zip file - Write-Host "Downloading and extracting latest LoginMigration Zip file..." - Invoke-RestMethod -Uri $ZipSource -OutFile $ZipDestination; - Expand-Archive -Path $ZipDestination -DestinationPath $BaseFolder -Force; + $ConsoleAppFolders = $ConsoleAppFolders | Sort-Object -Property Name -Descending + $LatestLocalNameAndVersion = $ConsoleAppFolders[0].Name + Write-Host "Installed Login migration console app nupkg version: $LatestLocalNameAndVersion" + + if ($AvailablePackagesOnNugetOrg -eq "") + { + $LatestNugetOrgDetails.NameAndVersion = $LatestLocalNameAndVersion + } } else { - # Get local exe version - Write-Host "Checking installed Login.Console.exe version..."; - $installedVersion = (Get-Item $ExePath).VersionInfo.FileVersion; - Write-Host "Installed version: $installedVersion"; - - # Get latest console app version - Write-Host "Checking whether there is newer version..."; - $VersionFileSource = "https://sqlassess.blob.core.windows.net/app/loginconsoleappversion.json"; - $VersionFileDestination = Join-Path -Path $BaseFolder -ChildPath "loginconsoleappversion.json"; - Invoke-RestMethod -Uri $VersionFileSource -OutFile $VersionFileDestination; - $jsonObj = Get-Content $VersionFileDestination | Out-String | ConvertFrom-Json; - $latestVersion = $jsonObj.version; - - # Compare the latest exe version with the local exe version - if([System.Version]$installedVersion -lt [System.Version]$latestVersion) + #No local console app + if ($AvailablePackagesOnNugetOrg -eq "") { - Write-Host "Found newer version of Logins.Console.exe '$latestVersion'"; - - Write-Host "Removing old Logins.Console.exe..." - # Remove old zip file - Remove-Item -Path $ZipDestination; + #No version available to download + Write-Host "Connection to NuGet.org required. Please check connection and try again." + return; + } + } - # Remove existing folder and contents - $ConsoleAppDestination = Join-Path -Path $BaseFolder -ChildPath "Logins.Console.csproj"; - Remove-Item -Path $ConsoleAppDestination -Recurse; + Write-Host "Latest Login migration console app nupkg version on Nuget.org: $($LatestNugetOrgDetails.NameAndVersion)"; + $LatestNugetFolder = Join-Path -Path $DownloadsFolder -ChildPath $LatestNugetOrgDetails.NameAndVersion; + $ExePath = "tools\Microsoft.SqlServer.Migration.Logins.ConsoleApp.exe"; - # Remove version file - Remove-Item -Path $VersionFileDestination; + # Check for the latest console app version and download it if needed. + CheckAndDownloadConsoleAppFromNugetOrg $LatestLocalNameAndVersion $LatestNugetOrgDetails $ExePath ([ref]$LatestNugetFolder) - #Downloading and extracting LoginMigration Zip file - Write-Host "Downloading and extracting latest LoginMigration Zip file..." - Invoke-RestMethod -Uri $ZipSource -OutFile $ZipDestination; - Expand-Archive -Path $ZipDestination -DestinationPath $BaseFolder -Force; - } - else - { - Write-Host "Installed Logins.Console.exe is the latest one..."; - } + if(-Not (Test-Path -Path "$LatestNugetFolder\$ExePath")) + { + Write-Host "Failed to locate executable." + return } #Collecting data if(('CommandLine') -contains $PSCmdlet.ParameterSetName) { - # The array list $splat contains all the parameters that will be passed to '.\Logins.Console.exe LoginsMigration' + # The array list $splat contains all the parameters that will be passed to '.\Microsoft.SqlServer.Migration.Logins.ConsoleApp.exe LoginsMigration' $LoginsListArray = $($ListOfLogin -split " ") [System.Collections.ArrayList] $splat = @( @@ -173,7 +162,10 @@ function New-AzDataMigrationLoginsMigration } } - # Running LoginsMigration + + $ExePath = Join-Path -Path $LatestNugetFolder -ChildPath $ExePath; + # Running LoginsMigration + Write-Host "Starting Execution..." & $ExePath LoginsMigration @splat } else diff --git a/src/DataMigration/DataMigration.Autorest/custom/Helpers/LoginMigrations.ps1 b/src/DataMigration/DataMigration.Autorest/custom/Helpers/LoginMigrations.ps1 index 35ac022b9cc8..741fc0245aee 100644 --- a/src/DataMigration/DataMigration.Autorest/custom/Helpers/LoginMigrations.ps1 +++ b/src/DataMigration/DataMigration.Autorest/custom/Helpers/LoginMigrations.ps1 @@ -56,5 +56,175 @@ function Get-DefaultLoginMigrationsOutputFolder { return $DefualtPath } - } + +function Delete-OldLoginConsoleApp { + [Microsoft.Azure.PowerShell.Cmdlets.DataMigration.DoNotExportAttribute()] + param( + [Parameter(Mandatory=$true, Position=0)] + [System.String] + $DownloadsFolder + ) + + process { + #Remove old Login console app files + #Old console app download address + $ZipDestination = Join-Path -Path $DownloadsFolder -ChildPath "LoginsMigration.zip"; + + # Remove old zip file + if(Test-Path $ZipDestination) + { + Remove-Item -Path $ZipDestination; + } + + # Remove existing folder and contents + $ConsoleAppDestination = Join-Path -Path $DownloadsFolder -ChildPath "Logins.Console.csproj"; + if(Test-Path $ConsoleAppDestination) + { + Remove-Item -Path $ConsoleAppDestination -Recurse; + } + + # Remove version file + $VersionFileDestination = Join-Path -Path $DownloadsFolder -ChildPath "loginconsoleappversion.json"; + if(Test-Path $VersionFileDestination) + { + Remove-Item -Path $VersionFileDestination; + } + } +} + + +function Get-LatestConsoleAppVersionFromNugetOrg { + [Microsoft.Azure.PowerShell.Cmdlets.DataMigration.DoNotExportAttribute()] + param( + [Parameter(Mandatory=$true, Position=0)] + [System.String] + $PackageId + ) + + process { + $AvailablePackagesOnNugetOrg = "" + + try { + $AvailablePackagesOnNugetOrg = Find-Package -Source "https://api.nuget.org/v3/index.json" -Name $PackageId -AllowPrereleaseVersions -AllVersions + $AvailablePackagesOnNugetOrg = $AvailablePackagesOnNugetOrg | Sort-Object -Property Version -Descending + } catch { + Write-Host "Unable to connect to NuGet.org to check for updates." + } + + $LatestNugetOrgName = $AvailablePackagesOnNugetOrg[0].Name + $LatestNugetOrgVersion = $AvailablePackagesOnNugetOrg[0].Version + $LatestNugetOrgNameAndVersion = "$LatestNugetOrgName.$LatestNugetOrgVersion"; + + return @{Name=$LatestNugetOrgName; Version=$LatestNugetOrgVersion; NameAndVersion=$LatestNugetOrgNameAndVersion} + } +} + +function CheckAndDownloadConsoleAppFromNugetOrg { + [Microsoft.Azure.PowerShell.Cmdlets.DataMigration.DoNotExportAttribute()] + param( + [Parameter(Mandatory=$true, Position=0)] + [AllowEmptyString()] + [System.String] + $LatestLocalNameAndVersion, + + [Parameter(Mandatory=$true, Position=1)] + [HashTable] + $LatestNugetOrgDetails, + + [Parameter(Mandatory=$true, Position=2)] + [System.String] + $ExePath, + + [Parameter(Mandatory=$true, Position=3)] + [ref] + [System.String] + $LatestNugetFolder + ) + + process { + #User consent for Login migration console app update. By default it is set to yes. + $userUpdateConsent = "yes"; + + # Prompt for user consent on Login migration console app update + if($LatestLocalNameAndVersion -ne "" -and $LatestNugetOrgDetails.NameAndVersion -gt $LatestLocalNameAndVersion) + { + Write-Host "Newer Login migration console app nupkg version is available in Nuget.org..."; + while($true) { + $userUpdateConsent = Read-Host -Prompt "Do you want to upgrade to the latest version? (yes/no)" + + if ($userUpdateConsent -eq "yes") + { + Write-Host "You chose to upgrade. Proceeding..." + break; + } + elseif ($userUpdateConsent -eq "no") + { + Write-Host "You chose not to upgrade." + $LatestNugetFolder.Value = Join-Path -Path $DownloadsFolder -ChildPath $LatestLocalNameAndVersion; + break; + } + else + { + Write-Host "Invalid input. Please enter 'yes' or 'no'." + } + } + } + + if ($LatestNugetOrgDetails.NameAndVersion -gt $LatestLocalNameAndVersion -and $userUpdateConsent -eq "yes") + { + #Update is available + $DownloadUrl = "https://www.nuget.org/api/v2/package/$PackageId/$($LatestNugetOrgDetails.Version)" + + #Checking if LatestNugetFolder Path is valid or not + if(-Not (Test-Path $LatestNugetFolder.Value)) + { + $null = New-Item -Path $LatestNugetFolder.Value -ItemType "directory"; + } + + Write-Host "Downloading the latest Login migration console app nupkg: $($LatestNugetOrgDetails.NameAndVersion) ..." + Invoke-WebRequest $DownloadUrl -OutFile "$($LatestNugetFolder.Value)\\$($LatestNugetOrgDetails.NameAndVersion).nupkg" + + $ToolsPathExists = Test-Path -Path (Join-Path -Path $LatestNugetFolder.Value -ChildPath "tools"); + + if ($ToolsPathExists -eq $False) + { + $Nugets = Get-ChildItem -Path $LatestNugetFolder.Value -Filter "$PackageId.*.nupkg"; + + if ($Nugets.Length -gt 0) + { + Write-Host "Extracting the latest Login migration console app nupkg: $($LatestNugetOrgDetails.NameAndVersion) ..." + $Nugets = $Nugets | Sort-Object -Property Name -Descending; + $LatestNugetPath = $Nugets[0].FullName; + Expand-Archive -Path $LatestNugetPath -DestinationPath $LatestNugetFolder.Value; + } + } + + #Check if update was successful + $TestPathResult = Test-Path -Path "$($LatestNugetFolder.Value)\$ExePath" + + $NugetVersions = Get-ChildItem -Path $DownloadsFolder -Filter "$PackageId.*"; + $NugetVersions = $NugetVersions | Sort-Object -Property Name -Descending + + if($TestPathResult) + { + Write-Host "Removing all older Login migration console apps..." + #Remove all other NuGet versions except for the version just downloaded + for ($NugetIndex = 0; $NugetIndex -lt $NugetVersions.Length; $NugetIndex = $NugetIndex + 1) + { + if($NugetVersions[$NugetIndex].Name -ne $LatestNugetOrgDetails.NameAndVersion) + { + Remove-Item -Path $NugetVersions[$NugetIndex].FullName -Recurse -Force + } + } + } + else + { + if($NugetVersions.Length -gt 0) + { + $LatestNugetFolder.Value = $NugetVersions[0].Name; + } + } + } + } +} \ No newline at end of file