【发布时间】:2021-11-06 11:28:44
【问题描述】:
我正在尝试创建一个脚本,该脚本使用对象 ID 和应用程序 ID(用于应用程序和服务主体)来定位用户、组、应用程序或服务主体。我希望脚本能够在有或没有开关参数的情况下运行。我希望它能够搜索全部或特定搜索位置。例如:
搜索全部:./test.ps1 -ObjectId "xxxx-xxxx-xxxx-xxxxxx"
仅限用户和组:./test.ps1 -ObjectId "xxxx-xxxx-xxxx-xxxxxx" -User -Group
仅限应用程序:./test.ps1 -ObjectId "xxxx-xxxx-xxxx-xxxxxx" -App
如果有人可以检查我的工作并提供反馈和/或建议,我们将不胜感激!谢谢!
[CmdletBinding(DefaultParameterSetName='SearchAll')]
param(
[Parameter(Mandatory=$true)]
[ValidatePattern("^[a-z_0-9]{8}[-][a-z_0-9]{4}[-][a-z_0-9]{4}[-][a-z_0-9]{4}[-][a-z_0-9]{12}")]
$objectID,
[Parameter(ParameterSetName = 'Switches')]
[switch]$User,
[Parameter(ParameterSetName = 'Switches')]
[switch]$Group,
[Parameter(ParameterSetName = 'Switches')]
[switch]$sp,
[Parameter(ParameterSetName = 'Switches')]
[switch]$App
)
if($PSCmdlet.ParameterSetName -eq 'Switches')
{
if ($user.IsPresent) {
Get-AzADUser -ObjectId $objectID | Select-Object Mail, DisplayName
$found = $true }
if ($group.IsPresent) {
Get-AzADGroup -ObjectId $objectID | Select-Object DisplayName, Description, Id
$found = $true }
if ($app.IsPresent) {
Get-AzADApplication -ObjectId $objectID | Select-Object ObjectType, DisplayName, Id, Type, ApplicationId
Get-AzADApplication -ApplicationId $objectID | Select-Object ObjectType, DisplayName, Id, Type, ApplicationId
$found = $true }
if ($sp.IsPresent) {
Get-AzADServicePrincipal -ObjectId $objectID | Select-Object ObjectType, DisplayName, Id, Type, ApplicationId
Get-AzADServicePrincipal -ApplicationId $objectID | Select-Object ObjectType, DisplayName, Id, Type, ApplicationId
$found = $true }
if (-not $found) { Write-Warning "Not found." }
}
else{
$user = Get-AzADUser -ObjectId $objectID
if ($user)
{
$user | Format-List Mail, DisplayName
}
else
{
$group = Get-AzADGroup -ObjectId $objectID
if ($group)
{
$group | Format-List DisplayName, Description, Id
}
else
{
$appO = Get-AzADApplication -ObjectId $objectID
if ($appO)
{
Write-Host "ObjectID"
$appO | Format-List ObjectType, DisplayName, Id, Type, ApplicationId
}
else
{
$appA = Get-AzADApplication -ApplicationId $objectID
if ($appA)
{
Write-Host "Application ID"
$appA | Format-List ObjectType, DisplayName, Id, Type, ApplicationId
}
else
{
$spO = Get-AzADServicePrincipal -ObjectId $objectID
if ($spO)
{
$spO | Format-List ObjectType, DisplayName, Id, Type, ApplicationId
}
else
{
$spA = Get-AzADServicePrincipal -ApplicationId $objectID
if ($spA)
{
$spA | Format-List ObjectType, DisplayName, Id, Type, ApplicationId
}
else
{
Write-Warning "Object does not exist."
}
}
}
}
}
}
}
还有,
if (-not $found) { Write-Warning "Not found." }
行似乎不起作用。有什么建议吗?
【问题讨论】:
-
我提出了回复
$found。我为您的问题的代码审查留下了一些指示。但我认为还有另一个代码审查论坛。让我知道,因为我想从答案中剔除一些绒毛。 -
@Steven 在发布到 CR 之前,OP 需要让代码正常工作(即包括似乎不起作用的行)——否则它不会是 on-topic
-
@SᴀᴍOnᴇᴌᴀ 谢谢。我们会看看这是怎么回事。我只是想确保答案最终得到清理。假设它是值得的。
标签: azure powershell scripting powershell-core