Пример #1
0
        public static void CheckVersion(PSCmdlet cmdlet)
        {
            // do we need to check versions. Is the environment variable set?
            var pnppowershellUpdatecheck = Environment.GetEnvironmentVariable("PNPPOWERSHELL_UPDATECHECK");

            if (!string.IsNullOrEmpty(pnppowershellUpdatecheck))
            {
                if (pnppowershellUpdatecheck.ToLower() == "off" || pnppowershellUpdatecheck.ToLower() == "false")
                {
                    VersionChecked = true;
                }
            }

            try
            {
                if (!VersionChecked)
                {
                    var onlineVersion = GetAvailableVersion();

                    if (isNewer(onlineVersion) && cmdlet != null)
                    {
#if DEBUG
                        var updateMessage = $"\nA newer version of PnP PowerShell is available: {onlineVersion}.\n\nUse 'Update-Module -Name PnP.PowerShell -AllowPrerelease' to update.\n\nYou can turn this check off by setting the 'PNPPOWERSHELL_UPDATECHECK' environment variable to 'Off'.\n";
#else
                        var updateMessage = $"\nA newer version of PnP PowerShell is available: {onlineVersion}.\n\nUse 'Update-Module -Name PnP.PowerShell' to update.\n\nYou can turn this check off by setting the 'PNPPOWERSHELL_UPDATECHECK' environment variable to 'Off'.\n";
#endif
                        CmdletMessageWriter.WriteFormattedWarning(cmdlet, updateMessage);
                    }
                    VersionChecked = true;
                }
            }
            catch (Exception)
            { }
        }
Пример #2
0
        public static void CheckVersion(PSCmdlet cmdlet)
        {
            // do we need to check versions. Is the environment variable set?
            var pnppowershellUpdatecheck = Environment.GetEnvironmentVariable("PNPPOWERSHELL_UPDATECHECK");

            if (!string.IsNullOrEmpty(pnppowershellUpdatecheck))
            {
                if (pnppowershellUpdatecheck.ToLower() == "off" || pnppowershellUpdatecheck.ToLower() == "false")
                {
                    VersionChecked = true;
                }
            }

            try
            {
                if (!VersionChecked)
                {
                    var assembly       = Assembly.GetExecutingAssembly();
                    var versionInfo    = FileVersionInfo.GetVersionInfo(assembly.Location);
                    var productVersion = versionInfo.ProductVersion;
                    var isNightly      = productVersion.Contains("-");

                    var onlineVersion = GetAvailableVersion(isNightly);

                    if (IsNewer(onlineVersion) && cmdlet != null)
                    {
                        var updateMessage = $"\nA newer version of PnP PowerShell is available: {onlineVersion}.\n\nUse 'Update-Module -Name PnP.PowerShell {(isNightly ? "-AllowPrerelease" : "")}' to update.\nUse 'Get-PnPChangeLog {(!isNightly ? $"-Release {onlineVersion}" : "-Nightly")}' to list changes.\n\nYou can turn this check off by setting the 'PNPPOWERSHELL_UPDATECHECK' environment variable to 'Off'.\n";
                        CmdletMessageWriter.WriteFormattedWarning(cmdlet, updateMessage);
                    }
                    VersionChecked = true;
                }
            }
            catch (Exception)
            { }
        }
Пример #3
0
 internal static string AuthenticateInteractive(CancellationTokenSource cancellationTokenSource, CmdletMessageWriter messageWriter, bool noPopup, AzureEnvironment azureEnvironment, string tenantId)
 {
     try
     {
         using (var authManager = PnP.Framework.AuthenticationManager.CreateWithInteractiveLogin(CLIENTID, (url, port) =>
         {
             BrowserHelper.OpenBrowserForInteractiveLogin(url, port, !noPopup, cancellationTokenSource);
         },
                                                                                                 tenantId,
                                                                                                 $"You successfully authenticated with PnP PowerShell. Feel free to close this {(noPopup ? "tab" : "window")}.",
                                                                                                 $"You did not authenticate with PnP PowerShell. Feel free to close this browser {(noPopup ? "tab" : "window")}.",
                                                                                                 azureEnvironment)
                )
         {
             authManager.ClearTokenCache();
             try
             {
                 return(authManager.GetAccessTokenAsync(new string[] { $"https://{GetGraphEndPoint(azureEnvironment)}/.default" }, cancellationTokenSource.Token).GetAwaiter().GetResult());
             }
             catch (Microsoft.Identity.Client.MsalException)
             {
                 return(null);
             }
         }
     }
     catch (OperationCanceledException)
     {
         cancellationTokenSource.Cancel();
     }
     return(null);
 }
Пример #4
0
 internal static string AuthenticateDeviceLogin(CancellationTokenSource cancellationTokenSource, CmdletMessageWriter messageWriter, bool noPopup, AzureEnvironment azureEnvironment, string clientId = "1950a258-227b-4e31-a9cf-717495945fc2")
 {
     try
     {
         using (var authManager = PnP.Framework.AuthenticationManager.CreateWithDeviceLogin(clientId, (result) =>
         {
             if (Utilities.OperatingSystem.IsWindows() && !noPopup)
             {
                 ClipboardService.SetText(result.UserCode);
                 messageWriter.WriteWarning($"Please login.\n\nWe opened a browser and navigated to {result.VerificationUrl}\n\nEnter code: {result.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the popup after you authenticated successfully to continue the process.");
                 BrowserHelper.GetWebBrowserPopup(result.VerificationUrl, "Please login", cancellationTokenSource: cancellationTokenSource, cancelOnClose: false);
             }
             else
             {
                 messageWriter.WriteWarning(result.Message);
             }
             return(Task.FromResult(0));
         }, azureEnvironment))
         {
             authManager.ClearTokenCache();
             try
             {
                 return(authManager.GetAccessTokenAsync(new string[] { $"https://{GetGraphEndPoint(azureEnvironment)}/.default" }, cancellationTokenSource.Token).GetAwaiter().GetResult());
             }
             catch (Microsoft.Identity.Client.MsalException)
             {
                 return(null);
             }
         }
     }
     catch (OperationCanceledException)
     {
         cancellationTokenSource.Cancel();
     }
     return(null);
 }
Пример #5
0
        internal static string AuthenticateDeviceLogin(string tenantId, CancellationTokenSource cancellationTokenSource, CmdletMessageWriter messageWriter, bool NoPopup, string loginEndPoint = "https://login.microsoftonline.com")
        {
            if (string.IsNullOrEmpty(tenantId))
            {
                throw new ArgumentException($"{nameof(tenantId)} is required");
            }
            var authority = $"{loginEndPoint}/{tenantId}";


            var app    = PublicClientApplicationBuilder.Create(CLIENTID).WithAuthority(authority).Build();
            var scopes = new string[] { "https://graph.microsoft.com/.default" };

            try
            {
                var tokenResult = app.AcquireTokenWithDeviceCode(scopes, result =>
                {
                    if (Utilities.OperatingSystem.IsWindows() && !NoPopup)
                    {
                        ClipboardService.SetText(result.UserCode);
                        messageWriter.WriteMessage($"Provide consent.\n\nWe opened a browser and navigated to {result.VerificationUrl}\n\nEnter code: {result.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the popup after you authenticated successfully to continue the process.");
                        BrowserHelper.GetWebBrowserPopup(result.VerificationUrl, "Provide consent");
                    }
                    else
                    {
                        messageWriter.WriteMessage(result.Message);
                    }
                    return(Task.FromResult(0));
                }).ExecuteAsync(cancellationTokenSource.Token).GetAwaiter().GetResult();
                return(tokenResult.AccessToken);
            }
            catch (OperationCanceledException)
            {
                cancellationTokenSource.Cancel();
            }
            return(null);
        }