question

NainZumran-5900 avatar image
0 Votes"
NainZumran-5900 asked DSPatrick answered

How to check if a user is an administrator for a given server name?

Using the username, I want to find a way to check if that user is an administrator for a given remote server.

I found the following function online but it only checks if the user is an admin for the local machine.

Any help is appreciated!

function Test-Administrator
{
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

windows-server
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DSPatrick avatar image
0 Votes"
DSPatrick answered

Give this one a go.
https://4sysops.com/archives/create-a-list-of-local-administrators-with-powershell/

--please don't forget to upvote and Accept as answer if the reply is helpful--


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YuhanDeng-MSFT avatar image
0 Votes"
YuhanDeng-MSFT answered YuhanDeng-MSFT commented

Hi,
You might want to refer to this article:
https://devblogs.microsoft.com/scripting/check-for-admin-credentials-in-a-powershell-script/
Also PowerShell version higher than 5.1 (Windows Server 2016) contains Get-LocalGroupMember cmdlet.
$user = "$env:COMPUTERNAME\$env:USERNAME"
$group = 'Administrators'
$isInGroup = (Get-LocalGroupMember $group).Name -contains $user

Thanks for your time.
Best regards,
Danny


If the Answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It says that Get-LocalGrupMember is not recognized as a cmdlet. Is there any way to to do the third line without Get-LocalGroupMember?

0 Votes 0 ·

Spelling?
Get-LocalGroupMember

--please don't forget to upvote and Accept as answer if the reply is helpful--



0 Votes 0 ·

Yes, I did Get-LocalGroupMember but its not recognized as a cmdlet. Is there any way to to do the third line without Get-LocalGroupMember?

0 Votes 0 ·
Show more comments
DSPatrick avatar image
0 Votes"
DSPatrick answered

Just checking if there's any progress or updates?

--please don't forget to upvote and Accept as answer if the reply is helpful--








5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.