Skip to content

Commit

Permalink
Refactor: Support search by name
Browse files Browse the repository at this point in the history
Will support wildcards
  • Loading branch information
Splaxi committed Mar 3, 2024
1 parent ba5e248 commit 38ffd6b
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 3 deletions.
56 changes: 55 additions & 1 deletion d365bap.tools/functions/Get-BapEnvironmentVirtualEntity.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
.PARAMETER EnvironmentId
The id of the environment that you want to work against
.PARAMETER Name
The name of the virtual entity that you are looking for
The parameter supports wildcards, but will resolve them into a strategy that matches best practice from Microsoft documentation
It means that you can only have a single search phrase. E.g.
* -Name "*Retail"
* -Name "Retail*"
* -Name "*Retail*"
Multiple search phrases are not going to produce an output, as it will be striped into an invalid search string. E.g.
! -Name "*Retail*Entity*" -> "RetailEntity"
! -Name "Retail*Entity" -> "RetailEntity"
! -Name "*Retail*Entity" -> "RetailEntity"
! -Name "Retail*Entity*" -> "RetailEntity"
.PARAMETER VisibleOnly
Instruct the cmdlet to only output those virtual entities that are enabled / visible
Expand Down Expand Up @@ -50,15 +66,32 @@
This will fetch all virtual entities from the environment.
Will output all details into an Excel file, that will auto open on your machine.
.EXAMPLE
PS C:\> Get-BapEnvironmentVirtualEntity -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6 -Name "Retail*"
This will fetch all virtual entities that contains the "Retail" text in the name, from the environment.
Sample output:
EntityName IsVisible ChangeTrackingEnabled EntityGuid
---------- --------- --------------------- ----------
CustHierarchyRetailChannelEnt… False False 00002893-0000-0000-e314-005001000000
DimAttributeRetailChannelEnti… False False 00002893-0000-0000-0804-005001000000
DimAttributeRetailStoreEntity False False 00002893-0000-0000-0f03-005001000000
DimAttributeRetailTerminalEnt… False False 00002893-0000-0000-6e07-005001000000
EcoResRetailProductEntity False False 00002893-0000-0000-ae06-005001000000
.NOTES
Author: Mötz Jensen (@Splaxi)
#>
function Get-BapEnvironmentVirtualEntity {
[CmdletBinding()]
[OutputType('System.Object[]')]
param (
[parameter (mandatory = $true)]
[string] $EnvironmentId,

[string] $Name = "*",

[switch] $VisibleOnly,

[switch] $AsExcelOutput
Expand Down Expand Up @@ -93,9 +126,30 @@ function Get-BapEnvironmentVirtualEntity {
process {
$localUri = $($baseUri + '/api/data/v9.2/mserp_financeandoperationsentities')

[System.Collections.Generic.List[System.String]]$filters = @()

# Is the user search for specific entities?
if ($Name -ne "*") {
if ($Name.IndexOfAny("*") -gt -1) {
# It is a wildcard search
$filters.Add("contains(mserp_physicalname, '$($Name.Replace('*',''))')")
}
else {
# It is a full named search
$filters.Add("mserp_physicalname eq '$Name'")
}
}

if ($VisibleOnly) {
$localUri += '?$filter=mserp_hasbeengenerated eq true'
$filters.Add("mserp_hasbeengenerated eq true")
}

if ($filters.count -gt 0) {
# We need to handle multiple filters
$localUri += '?$filter='
$localUri += $($filters.ToArray() -join " and ")
}

$resEntities = Invoke-RestMethod -Method Get -Uri $localUri -Headers $headersWebApi

$resCol = @(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
$parameter.ParameterSets['__AllParameterSets'].ValueFromPipelineByPropertyName | Should -Be $False
$parameter.ParameterSets['__AllParameterSets'].ValueFromRemainingArguments | Should -Be $False
}
It 'Should have the expected parameter Name' {
$parameter = (Get-Command Get-BapEnvironmentVirtualEntity).Parameters['Name']
$parameter.Name | Should -Be 'Name'
$parameter.ParameterType.ToString() | Should -Be System.String
$parameter.IsDynamic | Should -Be $False
$parameter.ParameterSets.Keys | Should -Be '__AllParameterSets'
$parameter.ParameterSets.Keys | Should -Contain '__AllParameterSets'
$parameter.ParameterSets['__AllParameterSets'].IsMandatory | Should -Be $False
$parameter.ParameterSets['__AllParameterSets'].Position | Should -Be 1
$parameter.ParameterSets['__AllParameterSets'].ValueFromPipeline | Should -Be $False
$parameter.ParameterSets['__AllParameterSets'].ValueFromPipelineByPropertyName | Should -Be $False
$parameter.ParameterSets['__AllParameterSets'].ValueFromRemainingArguments | Should -Be $False
}
It 'Should have the expected parameter VisibleOnly' {
$parameter = (Get-Command Get-BapEnvironmentVirtualEntity).Parameters['VisibleOnly']
$parameter.Name | Should -Be 'VisibleOnly'
Expand Down Expand Up @@ -55,7 +68,7 @@
Describe "Testing parameterset __AllParameterSets" {
<#
__AllParameterSets -EnvironmentId
__AllParameterSets -EnvironmentId -VisibleOnly -AsExcelOutput
__AllParameterSets -EnvironmentId -Name -VisibleOnly -AsExcelOutput
#>
}

Expand Down
53 changes: 52 additions & 1 deletion docs/Get-BapEnvironmentVirtualEntity.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Get Virutal Entity
## SYNTAX

```
Get-BapEnvironmentVirtualEntity [-EnvironmentId] <String> [-VisibleOnly] [-AsExcelOutput] [<CommonParameters>]
Get-BapEnvironmentVirtualEntity [-EnvironmentId] <String> [[-Name] <String>] [-VisibleOnly] [-AsExcelOutput]
[<CommonParameters>]
```

## DESCRIPTION
Expand Down Expand Up @@ -60,6 +61,22 @@ Get-BapEnvironmentVirtualEntity -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c7
This will fetch all virtual entities from the environment.
Will output all details into an Excel file, that will auto open on your machine.

### EXAMPLE 4
```
Get-BapEnvironmentVirtualEntity -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6 -Name "Retail*"
```

This will fetch all virtual entities that contains the "Retail" text in the name, from the environment.

Sample output:
EntityName IsVisible ChangeTrackingEnabled EntityGuid
---------- --------- --------------------- ----------
CustHierarchyRetailChannelEnt… False False 00002893-0000-0000-e314-005001000000
DimAttributeRetailChannelEnti… False False 00002893-0000-0000-0804-005001000000
DimAttributeRetailStoreEntity False False 00002893-0000-0000-0f03-005001000000
DimAttributeRetailTerminalEnt… False False 00002893-0000-0000-6e07-005001000000
EcoResRetailProductEntity False False 00002893-0000-0000-ae06-005001000000

## PARAMETERS

### -EnvironmentId
Expand All @@ -77,6 +94,40 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -Name
The name of the virtual entity that you are looking for
The parameter supports wildcards, but will resolve them into a strategy that matches best practice from Microsoft documentation
It means that you can only have a single search phrase.
E.g.
* -Name "*Retail"
* -Name "Retail*"
* -Name "*Retail*"
Multiple search phrases are not going to produce an output, as it will be striped into an invalid search string.
E.g.
!
-Name "*Retail*Entity*" -\> "RetailEntity"
!
-Name "Retail*Entity" -\> "RetailEntity"
!
-Name "*Retail*Entity" -\> "RetailEntity"
!
-Name "Retail*Entity*" -\> "RetailEntity"
```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: *
Accept pipeline input: False
Accept wildcard characters: False
```
### -VisibleOnly
Instruct the cmdlet to only output those virtual entities that are enabled / visible
Expand Down

0 comments on commit 38ffd6b

Please sign in to comment.