Skip to content

Commit 83ea63e

Browse files
authored
Add Token parameter to Search-AzResourceGraph command (#8)
1 parent b10b7b3 commit 83ea63e

4 files changed

Lines changed: 26 additions & 6 deletions

File tree

AzResourceGraph/Public/Search-AzResourceGraph.ps1

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ Allow partial scopes in the query. Only applicable for tenant and management gro
4242
Number of rows to request per page (1-1000).
4343
The function continues paging until all rows are retrieved.
4444
45+
.PARAMETER Token
46+
Use to call Azure Resource Graph with a specified access token. Using this parameter will override any sign-in made with Connect-AzResourceGraph for a single command.
47+
4548
.EXAMPLE
4649
# Execute a query stored in a file against two subscriptions
4750
Search-AzResourceGraph -QueryPath '.\vm-details.kql' `
@@ -90,15 +93,22 @@ function Search-AzResourceGraph {
9093
[Parameter(ParameterSetName = 'Path')]
9194
[Parameter(ParameterSetName = 'String')]
9295
[ValidateRange(1, 1000)]
93-
[int]$PageSize = 1000
96+
[int]$PageSize = 1000,
97+
98+
[Parameter(ParameterSetName = 'Path', DontShow)]
99+
[Parameter(ParameterSetName = 'String', DontShow)]
100+
[string]$Token
94101
)
95102

96103
# Ensure only one of SubscriptionId or ManagementGroup is provided
97104
if ($PSBoundParameters.ContainsKey('SubscriptionId') -and $PSBoundParameters.ContainsKey('ManagementGroup')) {
98105
throw 'KQL Query can only be run against either a Subscription or a Management Group, not both.'
99106
}
100107

101-
Assert-AzureConnection -TokenSplat $script:TokenSplat
108+
if (-not $PSBoundParameters.ContainsKey('Token')) {
109+
Assert-AzureConnection -TokenSplat $script:TokenSplat
110+
$Token = $script:Token.Token
111+
}
102112

103113
if ($PSCmdlet.ParameterSetName -eq 'Path') {
104114
$Query = Get-Content $QueryPath -Raw
@@ -120,7 +130,7 @@ function Search-AzResourceGraph {
120130
if ($PSBoundParameters.ContainsKey('ManagementGroup')) { $Body['managementGroups'] = @($ManagementGroup) }
121131

122132
$Headers = @{
123-
'Authorization' = "Bearer $($script:Token.Token)"
133+
'Authorization' = "Bearer $Token"
124134
'Content-Type' = 'application/json'
125135
}
126136

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Added
9+
- Added Token parameter to Search-AzResourceGraph function to allow specifying a token for authentication
10+
811
### Changed
912
- Updated dependency on AzAuth to version 2.5.0
1013

tests/Unit/Private/Assert-AzureConnection.tests.ps1

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,6 @@ Describe 'Assert-AzureConnection' {
105105
}
106106
} | Should -Throw
107107
}
108-
109108
}
110109
}
111-
112-
113110
}

tests/Unit/Public/Search-AzResourceGraph.tests.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@ Describe 'Search-AzResourceGraph' {
5454
$Body.managementGroups -contains 'myMG'
5555
}
5656
}
57+
58+
It 'Calls Assert-AzureConnection when not given a token' {
59+
$null = Search-AzResourceGraph -QueryPath '~/foo.kql' -PageSize 10
60+
Should -Invoke 'Assert-AzureConnection' -Times 1 -Exactly -ModuleName 'AzResourceGraph'
61+
}
62+
63+
It 'Does not call Assert-AzureConnection when given a token' {
64+
$null = Search-AzResourceGraph -QueryPath '~/foo.kql' -PageSize 10 -Token 'fakeToken'
65+
Should -Invoke 'Assert-AzureConnection' -Times 0 -Exactly -ModuleName 'AzResourceGraph'
66+
}
5767
}

0 commit comments

Comments
 (0)