microsoft / powerbi-powershell

PowerShell community for Microsoft PowerBI. Here you will find resources and source for PowerShell modules targeting PowerBI.
MIT License
349 stars 121 forks source link

Azure DevOps Pipeline unable to Connect-PowerBIServiceAccount via PSCredentials #387

Closed shengdoescoding closed 1 year ago

shengdoescoding commented 1 year ago
   - powershell: |
        $Env:PSModulePath = $Env:PSModulePath+";${{ variables.modules_folder }}"
        Import-Module -Name 'MicrosoftPowerBIMgmt.Admin', 'MicrosoftPowerBIMgmt.Capacities', 'MicrosoftPowerBIMgmt.Data', 'MicrosoftPowerBIMgmt.Profile', 'MicrosoftPowerBIMgmt.Reports', 'MicrosoftPowerBIMgmt.Workspaces'
        Get-Module
        $pass = ConvertTo-SecureString $env:PASS -AsPlainText -Force
        $username = 'someusername'
        # [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 I saw this on a post saying it might help, it does not
        $cred = New-Object System.Management.Automation.PSCredential($username, $pass)
        $null = Connect-PowerBIServiceAccount -Credential $cred
        $null = Resolve-PowerBIError -Last
      name: something
      displayName: 'something'
      env:
        PASS: $(somepass)

The commandlet module I installed via this line

Save-Module -Name MicrosoftPowerBIMgmt -Path ${{ variables.modules_folder }}

I'm sure my script has access to the module as the Get-Module line returns this:

ModuleType Version    Name                                ExportedCommands                                             
---------- -------    ----                                ----------------                                             
Binary     1.2.1111   MicrosoftPowerBIMgmt.Admin          {Add-PowerBIEncryptionKey, Get-PowerBIActivityEvent, Get-P...
Binary     1.2.1111   MicrosoftPowerBIMgmt.Capacities     Get-PowerBICapacity                                          
Binary     1.2.1111   MicrosoftPowerBIMgmt.Data           {Add-PowerBIDataset, Add-PowerBIRow, Export-PowerBIDataflo...
Binary     1.2.1111   MicrosoftPowerBIMgmt.Profile        {Connect-PowerBIServiceAccount, Disconnect-PowerBIServiceA...
Binary     1.2.1111   MicrosoftPowerBIMgmt.Reports        {Copy-PowerBIReport, Copy-PowerBITile, Export-PowerBIRepor...
Binary     1.2.1111   MicrosoftPowerBIMgmt.Workspaces     {Add-PowerBIWorkspaceUser, Get-PowerBIWorkspace, Get-Power...

No matter what I try, I always get this error:

Connect-PowerBIServiceAccount : One or more errors occurred.
At somelocaton/location
+ $null = Connect-PowerBIServiceAccount -Credential $cred
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (Microsoft.Power...IServiceAccount:ConnectPowerBIServiceAccount) [Connect-Po 
   werBIServiceAccount], AggregateException
    + FullyQualifiedErrorId : One or more errors occurred.,Microsoft.PowerBI.Commands.Profile.ConnectPowerBIServiceAcc 
   ount

Unfortunately, including Resolve-PowerBIError -Last at the end of the script doesn't return anything.

I have checked this PowerShell instances version with $PSVersionTable, and its output is:

Name                           Value                                                                                   
----                           -----                                                                                   
PSVersion                      5.1.20348.1366                                                                          
PSEdition                      Desktop                                                                                 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                 
BuildVersion                   10.0.20348.1366                                                                         
CLRVersion                     4.0.30319.42000                                                                         
WSManStackVersion              3.0                                                                                     
PSRemotingProtocolVersion      2.3                                                                                     
SerializationVersion           1.1.0.1     

I appreciate any help.

shengdoescoding commented 1 year ago
        try{
          Connect-PowerBIServiceAccount -Credential $cred        
        }
        catch{
          Write-Host "MESSAGE`n$($_.Exception.Message)"
        }

Debugging with this showed the Microsoft Hosted Agents are not compliment with conditional access policy.

rangnar14 commented 5 months ago

@shengdoescoding what is the fix for this issue? I am not using Microsoft hosted agent. I have a self hosted one.