2017-10-09 63 views
-1

如果用戶在某個AD組中並且連接到某個網絡,我正在創建一個代碼以打開網站。這是我這麼遠:如何濃縮此PowerShell腳本?

$user = $env:username 
$group1 = "examplegroup1" 
$group2 = "examplegroup2" 

if (Test-Connection "examplenetwork" -Quiet) 
{  
$members1 = Get-ADGroupMember -Identity $group1 | Select -ExpandProperty 
SamAccountName 

$members2 = Get-ADGroupMember -Identity $group2 | Select -ExpandProperty 
SamAccountName 

If ($members1 -contains $user -or $members2 -contains $user) {Start-Process 
"examplewebsite"} 
} 

它的工作原理,因爲它應該打開網站,如果用戶是在正確的組,並在網絡上,但是我如果有凝結的方式只是想知道代碼?

看起來很浪費必須創建2'$groups',然後重複Get-ADGroupMember也是。我曾經玩過「ForEach」,但還沒有成功實現它。

關於如何濃縮這個的任何想法?最好使用ForEach cmdlet。

+2

我正在投票結束這個問題作爲題外話,因爲工作代碼是脫離主題,並且SE網站審查/建議對工作代碼的更改是https://codereview.stackexchange.com/ – TessellatingHeckler

+1

您還假設最終用戶的計算機將安裝AD模塊。如果您將其設計爲登錄腳本,您將遇到該問題。 –

回答

1

雖然我其他的答案是「不這樣做」,如果你想你的代碼,凝聚:

$groups = 'group1', 'group2' 

if ((Test-Connection -ComputerName "examplenetwork" -Quiet) -and 
    ($env:USERNAME -in ($groups | Get-ADGroupMember -Recursive).SamAccountName)) 
{ 
    Start-Process "www.example.com" 
} 

你真不不需要任何foreach。

+1

謝謝,TessellatingHeckler。這是我正在尋找的。我對Powershell非常陌生(正如你可能知道的那樣),管道是我需要增加知識的一件事!至於公元問題......我將不得不在明天解決這個問題!再次感謝。 – Powershelln00b

-1

您可以添加一個foreach循環,但會使已經非常小巧簡單的腳本複雜化。

我做的最多的就是來自這兩個組一起添加成員...

$user = $env:username 
$group1 = "examplegroup1" 
$group2 = "examplegroup2" 

if (Test-Connection "examplenetwork" -Quiet) 
{  
    $members = Get-ADGroupMember -Identity $group1 | Select -ExpandProperty SamAccountName 

    $members += Get-ADGroupMember -Identity $group2 | Select -ExpandPropertySamAccountName 

    If ($members -contains $user) {Start-Process "http://www.example.com"} 
} 
+0

這不能解決「*重複Get-ADGroupMember *」部分似乎很浪費的問題,但它確實改變了它,因此如果Group1只有一個成員,它現在會中斷。 – TessellatingHeckler

0
If (("examplegroup1", "examplegroup2" | % {Get-ADGroupMember -Identity $_} | Select -ExpandProperty SamAccountName) -Contains $env:username) {Start-Process "examplewebsite"} 
+0

如果用戶包含在兩個組中,則可以啓動該過程兩次。 – TToni

+0

@ TToni:不,'-unique'參數應該可以防止這個問題 – iRon

+0

事實上,'-unique'甚至不需要(我已經將它從答案中刪除了),如下所示:If( -contains $ env :username){}即使<<成員集合>包含重複成員,也不會再運行給定的''。 – iRon

0

我可能會做反向:

$user = $env:username 
$groups = "examplegroup1", "examplegroup1" 

$CheckMembership = Get-ADUser -Identity $user -Property MemberOf | Select-Object -ExpandProperty MemberOf | Where-Object { $_ -in $groups } 

if ($CheckMembership) { 
    Start-Process "http://www.example.com" 
} 

你會想請確保您的組列表是可分辨名稱的列表,但除此之外,這會將AD查詢的數量減少到1.

0

任何依靠Get-AD___需要RSAT工具才能獲得ActiveDirectory模塊,對於最終用戶工作站來說這是不太可能的假設,正如@Rohin Sidharth所評論的。

@詹姆斯C.目前接受的答案不會處理遞歸組成員(需要參數-Recursive),但也包括列出兩個組的所有成員 - 想象一下,如果每個組的成員數爲十億 - 並且它有陣列增加的不良習慣。

@Bacon位答案獲取用戶的組成員資格,這對於'獲取更少的數據'更好,但仍然不會處理遞歸組成員資格,仍然依賴於ActiveDirectory模塊。

爲避免RSAT,可以使用類似ADSI的東西 - 它由System.DirectoryServices.AccountManagement包裝。 Richard Siddaway討論了here

這有一個很好的方法來列出用戶的組成員,這似乎是壞的 - 從Terry Tsay的C#答案捏here,我將他的代碼移植到此,但我專注於當前用戶和默認包含的通訊組:

Add-Type -AssemblyName System.DirectoryServices.AccountManagement 

Function IsUserInGroup([string] $groupName) 
{ 
    # Remove DOMAIN\ from the start of the groupName. 
    $groupName = $groupName -replace '^.*\\' 


    # Get an AD context for the current user's domain 
    $context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList 'Domain', $ENV:USERDOMAIN 


    # Find the current user account in AD, and refresh the security and distribution groups 
    $user = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($context, 'SAMAccountName', $env:USERNAME) 
    $userEntry = [System.DirectoryServices.DirectoryEntry] $user.GetUnderlyingObject() 
    $userEntry.RefreshCache(@('tokenGroupsGlobalAndUniversal')) 


    # Get all the security and distribution groups the user belongs to, including nested memberships 
    $usersGroupSIDs = foreach ($sid in $userEntry.Properties.tokenGroupsGlobalAndUniversal.Value) 
    { 
     New-Object System.Security.Principal.SecurityIdentifier -ArgumentList $sid, 0 
    } 


    # Get the AD details for the group to test, and test membership 
    $group = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, 'SamAccountName', $groupName) 

    $usersGroupSIDs.Contains($group.Sid) 
} 

eg

PS C:\> IsUserInGroup 'parent-nested-group-here' 
True 

這是不凝結或者更簡單,但它應該用更少的開銷AD連接尤其是作爲組成員的增加數,以及額外的模塊不太需要,只是使用.NET Framework處理更多的條件。

然後,你可以修改,這樣做

$group2 = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, 'SamAccountName', $group2Name) 

$usersGroupSIDs.Contains($group.Sid) -or $usersGroupSIDs.Contains($group2.Sid)