仅从powershell或cmd-AzuredAD获取带空格的显示名称
我正在尝试从cmd或powershell中提取显示名称(名字和姓氏)。(AzureAD-不在prem AD上) 我已经得到了几个不同的命令,但它们都保持在一起的名称。 示例:仅从powershell或cmd-AzuredAD获取带空格的显示名称,powershell,azure-active-directory,Powershell,Azure Active Directory,我正在尝试从cmd或powershell中提取显示名称(名字和姓氏)。(AzureAD-不在prem AD上) 我已经得到了几个不同的命令,但它们都保持在一起的名称。 示例: $env:UserName=jatonjustice [System.Security.Principal.WindowsIdentity]::GetCurrent().Name=azureAD\jatonjustice 我试图找到一种方法,将结果中的名字和姓氏分开,或者将名字和姓氏分开,作为另一种想法。(比如:'Jato
$env:UserName=jatonjustice
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name=azureAD\jatonjustice
我试图找到一种方法,将结果中的名字和姓氏分开,或者将名字和姓氏分开,作为另一种想法。(比如:'Jaton Justice')我自己无法解析它,因为我不知道显示名称将是什么。(AzureAD-不在prem AD上)
有什么想法吗?
研究:
谢谢$env:UserName应该返回SamAccountName,而不是用户名。 “Get ADUser-Property*”应显示您正在查询的用户的所有信息,您应能够找到名为GivenName和姓氏的属性。PowerShell帮助文件中通过示例解释和详细说明了您所追求的一切。稍后将详细介绍。
$search = [adsisearcher]"(SamAccountName=$env:USERNAME)"
$search.PropertiesToLoad.AddRange(@('givenname','sn'))
$adAccount = $search.FindOne()
$firstName = $adAccount.Properties.givenname
$lastName = $adAccount.Properties.sn
$fullname = "$firstName $lastName"
至于
“但他们都把这个名字放在一起。示例:'
…而且他们应该是故意的
使用这些,您需要本地登录用户名(SamAccountName,这是本地主机上的用户配置文件和UPN的ADDS/AAD-中定义的短名称,SamAccountName@DomainName.com)对于这些,not添加了/AAD名称细节
如果您想从本地登录的用户那里得到第一个和最后一个,那么您必须在帐户中填充该信息,或者您必须从ADDS/AAD请求该信息。您的用例是什么
如果您使用的是PSv5x及更高版本,则有以下模块:
# Results
<#
Get-Module -Name '*local*'
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Binary 1.0.0.0 Microsoft.PowerShell.LocalAccounts {Add-LocalGroupMember, Disable-LocalUser, Enable-LocalUser, Get-LocalGroup...}
#>
。。。然后使用该Name/SamAccountName
询问ADDS/AAD用户的FullName
或任何您想要的内容
如果您使用的是早期版本,则需要从Microsofts的PowerShellGallery.com安装其中一个模块
Find-Module -Name '*local*'
# Results
<#
Version Name Repository Description
------- ---- ---------- -----------
...
1.6 localaccount PSGallery A Simple module to allow the management of local users and groups on a computer
1.0.0.0 Microsoft.PowerShell.LocalAccounts PSGallery Provides cmdlets to work with local users and local groups
3.0 LocalUserManagement PSGallery a module that performs various local user management functions
...
0.1.1 LocalAccountManagement PSGallery Manage local and remote user accounts and profiles
...
#>
查找模块-名称'*local*'
#结果
。。。并执行相同的操作或使用WMI、ADSI等
[adsisearcher]"(SamAccountName=$env:USERNAME)"
# Results
<#
CacheResults : True
ClientTimeout : -00:00:01
PropertyNamesOnly : False
Filter : (SamAccountName=TestUser)
PageSize : 0
PropertiesToLoad : {}
ReferralChasing : External
SearchScope : Subtree
ServerPageTimeLimit : -00:00:01
ServerTimeLimit : -00:00:01
SizeLimit : 0
SearchRoot :
Sort : System.DirectoryServices.SortOption
Asynchronous : False
Tombstone : False
AttributeScopeQuery :
DerefAlias : Never
SecurityMasks : None
ExtendedDN : None
DirectorySynchronization :
VirtualListView :
Site :
Container :
#>
[adsisearcher](SamAccountName=$env:USERNAME)
#结果
现在,回到我的“阅读帮助文件注释”
获取ADUser | MS文档
#示例3:获取指定用户的所有属性
获取ADUser-Identity$env:USERNAME-Properties'*'
#结果
基本上,您需要一个脚本来预测用户的名字和姓氏,以及如何编写逻辑代码?除非您可以根据广告或某些身份服务查询$env:UserName
,否则您无法回答您的问题。您的问题和后续评论表明您是Powershell的新手,正在寻找可复制/粘贴并运行的代码,而没有真正考虑它是什么、如何工作以及为什么。如果您只是在执行get-*命令,好吧,但是,如果您正在执行破坏性代码,即添加/更改内容的内容,那么在对主机或环境造成实际损害之前,您应该先进行一些培训。有大量的资源/视频可供免费学习PowerShell。甚至有一些工具可以为您编写基线PowerShell代码,您可以对其进行调整。你的要求是非常基本的。保护自己和环境的规则:1。如果您不了解任何人的代码在做什么,或者不愿意完全接受其结果,则永远不要运行任何人的代码。无论你从哪里或从谁那里得到它。特别是如果您可以访问源代码),除非您愿意接受运行它的所有后果。2.在未完全检查结果之前,切勿运行破坏性代码(添加/创建/更新、移动/删除/修改等)。掌握WhatIf/Confirm/Trace命令/Invoke-ScriptAnalyzer的使用。3.所有的输入都是邪恶的,不管它来自哪里,直到你首先验证它。来帮助你的学习之旅。此代码给我一个错误,说明指定的域不存在或无法联系。我相信,因为这是azure ad帐户。您可以指定该域
Find-Module -Name '*local*'
# Results
<#
Version Name Repository Description
------- ---- ---------- -----------
...
1.6 localaccount PSGallery A Simple module to allow the management of local users and groups on a computer
1.0.0.0 Microsoft.PowerShell.LocalAccounts PSGallery Provides cmdlets to work with local users and local groups
3.0 LocalUserManagement PSGallery a module that performs various local user management functions
...
0.1.1 LocalAccountManagement PSGallery Manage local and remote user accounts and profiles
...
#>
[adsisearcher]"(SamAccountName=$env:USERNAME)"
# Results
<#
CacheResults : True
ClientTimeout : -00:00:01
PropertyNamesOnly : False
Filter : (SamAccountName=TestUser)
PageSize : 0
PropertiesToLoad : {}
ReferralChasing : External
SearchScope : Subtree
ServerPageTimeLimit : -00:00:01
ServerTimeLimit : -00:00:01
SizeLimit : 0
SearchRoot :
Sort : System.DirectoryServices.SortOption
Asynchronous : False
Tombstone : False
AttributeScopeQuery :
DerefAlias : Never
SecurityMasks : None
ExtendedDN : None
DirectorySynchronization :
VirtualListView :
Site :
Container :
#>
# Example 3: Get all of the properties for a specified user
Get-ADUser -Identity $env:USERNAME -Properties '*'
# Results
<#
Surname : David
Name : Chew David
UserPrincipalName :
GivenName : David
Enabled : False
SamAccountName : ChewDavid
ObjectClass : user
SID : S-1-5-21-2889043008-4136710315-2444824263-3544
ObjectGUID : e1418d64-096c-4cb0-b903-ebb66562d99d
DistinguishedName : CN=Chew David,OU=NorthAmerica,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM
#>