2017-10-09 2 views
-1

ユーザーが特定のADグループに属していて、特定のネットワークに接続している場合、Webサイトを開くためのコードを作成しています。この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コマンドレットを使用します。

+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には非常に新しいです(あなたが言うことができるように)、パイプラインは私が自分の知識を増やすために必要なものです! ADの問題については....私は明日それに対処する必要があります!再度、感謝します。 – 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にメンバーが1つしかない場合には壊れるように変更します。 – TessellatingHeckler

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

ユーザーが両方のグループに含まれている場合、プロセスを2回起動することができます。 – TToni

+0

@ TToni:いいえ、 '-unique'引数はそれを防ぐべきです – iRon

+0

実際、' -unique'は 'If( -contains $ env :){} '' 'に重複メンバーが含まれていても、与えられた' 'をそれ以上実行することはありません。 – 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___には、ActiveDirectoryモジュールを入手するためのRSATツールが必要です。これは、@ Rohin Sidharthのコメントのように、エンドユーザーのワークステーションにとってはほとんど想定されていません。

@James C.さんの現在受け入れられている回答では、再帰的グループメンバーシップ(-Recursiveパラメータが必要です)は処理されませんが、両方のグループのすべてのメンバーをリストアップすることも含まれます。それは配列の追加の貧しい習慣を持っています。

@Baconビットの答えは「少ないデータを取得」が、まだ再帰的なグループメンバーシップを処理しませんし、まだのActiveDirectoryモジュールに依存しているために優れているユーザーのグループメンバーシップを取得します。

RSATを回避するには、ADSIのようなものを使用できます。これは、System.DirectoryServices.AccountManagementでラップされています。 Richard Siddawayによるhereについて議論しました。

壊れているように見えるユーザーのためのグループメンバーをリストアップするための良い方法がある - 類似した質問hereにテリーTsayのC#の答えからピンチを、Iこれにポートの彼のコードが、私は現在に焦点を当ててきました既定でユーザーと配布グループが含まれます。

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) 
} 

凝縮または単純な、しかしそれだけで.NET Frameworkを使用して、特にメンバーのグループが増加の数、および追加のモジュールにはあま​​り必要とオーバーヘッド接続以下ADとのより多くの条件を処理する必要がありますされていない
PS C:\> IsUserInGroup 'parent-nested-group-here' 
True 

私の他の答えはあなたが凝縮され、あなたのコードをしたいならば、「それをしない」ですが、その後、あなたは

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

$usersGroupSIDs.Contains($group.Sid) -or $usersGroupSIDs.Contains($group2.Sid) 
関連する問題