andri190
15-09-2015, 12:33
Есть код с помощью которого находим в каких группах состоит пользователь.
Как из этого сделать, чтобы по имени пользователя выдавалась информация по определённым параметрам.
import-module ActiveDirectory
Function Get-UserMembership
{
Param($UserAccount)
Process
{
Try
{
$Groups = (Get-ADUser -Identity $UserAccount -Properties MemberOf | `
Select-Object MemberOf).MemberOf
}
Catch
{
Return $Nothing
}
$GroupItems = @()
ForEach ($Group in $Groups)
{
$var = $group.split(",")
$var1 = $var[0]
$ADGroup = $var1.Substring(3)
$GrpItems = New-Object -TypeName PSObject -Property @{
Memberof = $ADGroup}
$GroupItems += $GrpItems
}
Return $GroupItems | Sort memberOf
}
}
Function Get-UserMembership_ByName
{
Param($UserName)
Process
{
Try
{
$ResultNumber = (Get-ADUser -Filter {Name -eq $UserName} | Measure-Object).Count
}
Catch
{
Return $Nothing
}
If ($ResultNumber -eq 0) {
Write-Host "There is no such user in the system."
Return $Nothing
}
ElseIf ($ResultNumber -ge 2)
{
Write-Host "There are several users in the system with the provided name." `
"Please use the user account way."
Return $Nothing
}
Else
{
$Groups = (Get-ADUser -Filter {Name -eq $UserName} -Properties memberof | `
Select-Object MemberOf).memberof
$GroupItems = @()
ForEach ($Group in $Groups)
{
$var = $group.split(",")
$var1 = $var[0]
$ADGroup = $var1.Substring(3)
$GrpItems = New-Object -TypeName PSObject -Property @{
Memberof = $ADGroup}
$GroupItems += $GrpItems
}
Return $GroupItems | Sort memberOf
}
}
}
$UserAccount = $Nothing
$UserAccount = Read-Host 'What is the user account? (Type nothing to search by a user name)'
If ($UserAccount -eq '')
{
$UserAccount = Read-Host 'What is the user name? (Last name and first name)'
$Result = UserMembership_ByName $UserAccount | Sort Memberof
}
Else
{
$Result = Get-UserMembership $UserAccount | Sort Memberof
}
If ($Result -eq $Nothing)
{
Write-Host "Nothing is found. The result file is not created."
}
Else
{
$OutFile = (Get-Childitem env:USERPROFILE).Value + "\Desktop\$UserAccount.csv"
$Result | Export-Csv $OutFile -Delimiter "`t" -Encoding UTF8
Write-Host "The result is in $OutFile file."
}
Как из этого сделать, чтобы по имени пользователя выдавалась информация по определённым параметрам.
import-module ActiveDirectory
Function Get-UserMembership
{
Param($UserAccount)
Process
{
Try
{
$Groups = (Get-ADUser -Identity $UserAccount -Properties MemberOf | `
Select-Object MemberOf).MemberOf
}
Catch
{
Return $Nothing
}
$GroupItems = @()
ForEach ($Group in $Groups)
{
$var = $group.split(",")
$var1 = $var[0]
$ADGroup = $var1.Substring(3)
$GrpItems = New-Object -TypeName PSObject -Property @{
Memberof = $ADGroup}
$GroupItems += $GrpItems
}
Return $GroupItems | Sort memberOf
}
}
Function Get-UserMembership_ByName
{
Param($UserName)
Process
{
Try
{
$ResultNumber = (Get-ADUser -Filter {Name -eq $UserName} | Measure-Object).Count
}
Catch
{
Return $Nothing
}
If ($ResultNumber -eq 0) {
Write-Host "There is no such user in the system."
Return $Nothing
}
ElseIf ($ResultNumber -ge 2)
{
Write-Host "There are several users in the system with the provided name." `
"Please use the user account way."
Return $Nothing
}
Else
{
$Groups = (Get-ADUser -Filter {Name -eq $UserName} -Properties memberof | `
Select-Object MemberOf).memberof
$GroupItems = @()
ForEach ($Group in $Groups)
{
$var = $group.split(",")
$var1 = $var[0]
$ADGroup = $var1.Substring(3)
$GrpItems = New-Object -TypeName PSObject -Property @{
Memberof = $ADGroup}
$GroupItems += $GrpItems
}
Return $GroupItems | Sort memberOf
}
}
}
$UserAccount = $Nothing
$UserAccount = Read-Host 'What is the user account? (Type nothing to search by a user name)'
If ($UserAccount -eq '')
{
$UserAccount = Read-Host 'What is the user name? (Last name and first name)'
$Result = UserMembership_ByName $UserAccount | Sort Memberof
}
Else
{
$Result = Get-UserMembership $UserAccount | Sort Memberof
}
If ($Result -eq $Nothing)
{
Write-Host "Nothing is found. The result file is not created."
}
Else
{
$OutFile = (Get-Childitem env:USERPROFILE).Value + "\Desktop\$UserAccount.csv"
$Result | Export-Csv $OutFile -Delimiter "`t" -Encoding UTF8
Write-Host "The result is in $OutFile file."
}