Public/Get-FabricSemanticModel.ps1

Function Get-FabricSemanticModel {
    <#
    .SYNOPSIS
        Retrieves Fabric semantic model(s) from a specified workspace.
 
    .DESCRIPTION
        The Get-FabricSemanticModel function retrieves information about semantic models within a Microsoft Fabric workspace.
        If a specific semantic model name is provided, the function returns details for that model only.
        Otherwise, it returns all semantic models in the workspace.
 
    .PARAMETER workspaceName
        The name of the Fabric workspace containing the semantic model(s).
         
    .PARAMETER semanticModelName
        The name of the specific semantic model to retrieve from the workspace.
         
    .EXAMPLE
        Get-FabricSemanticModel -workspaceName "Sales" -semanticModelName "SalesAnalysis"
         
        Retrieves details of the "SalesAnalysis" semantic model in the "Sales" workspace.
 
    .EXAMPLE
        Get-FabricSemanticModel -workspaceName "Sales"
         
        Retrieves all semantic models in the "Sales" workspace.
 
    .OUTPUTS
        System.Object
        Returns an object or collection of objects containing details about the semantic model(s).
         
    .NOTES
        This function requires the Get-FabricWorkspace and Invoke-FabricApiRequest functions.
        The user must have appropriate permissions to access the specified workspace and semantic models.
    #>

    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [string]$workspaceName,

        [Parameter()]
        [string]$semanticModelName
    )
      
    $workspace = Get-FabricWorkspace -workspaceName $workspaceName
    
    $result = Invoke-FabricApiRequest -Uri "workspaces/$($workspace.id)/semanticmodels" -Method Get

    if ($semanticModelName) {
        $semanticModel = $result | Where-Object { $_.displayName -ieq $semanticModelName }

        if (!$semanticModel) {
            throw "Cannot find semantic model '$semanticModelName'"
        }

        Write-Output $semanticModel
    }
    else {
        Write-Output $result
    }
}