diff --git a/Linux/Powershell/get_linux_groups.ps1 b/Linux/Powershell/get_linux_groups.ps1 new file mode 100644 index 0000000..23c3b93 --- /dev/null +++ b/Linux/Powershell/get_linux_groups.ps1 @@ -0,0 +1,127 @@ +#Script créé par Nicolas Lang - Sous licence CC-BY-SA +#https://nicolaslang.fr + +function Get-LinuxGroups +{ + [CmdletBinding( + SupportsShouldProcess=$false, + HelpUri="http://nicolaslang.blogspot.com" + )] + param( + [Parameter (Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] + [ValidateNotNullOrEmpty()] + [string]$Name, + [Parameter (Mandatory=$false)] + [ValidateNotNullOrEmpty()] + [ValidatePattern('(?<propertyname>(Name|ID_Group|Member))\s+(?<operator>(-like|-eq|-ceq|-clike))\s+"(?<value>.*)"')] + [string]$filter + ) + begin + { + $groups = Get-Content /etc/group + $grouparray = New-Object System.Collections.ArrayList + + $maxgroup = 0 + #Calcul de la valeur maxgroup + foreach ($group in $groups) + { + $splitted = $group.split(":") + $count = $splitted[-1].split(",") + + if ($count.count -gt $maxgroup) + { + $maxgroup = $count.count + + } + } + + foreach ($group in $groups) + { + $splitted = $group.split(":") + $count = $splitted[-1].split(",") + $object = [PSCustomObject]@{ + "Name" = $splitted[0] + "ID_Group" = $splitted[2] + } + $loop = 0 + + foreach ($member in $count) + { + $object | Add-Member -Name "Member $($loop + 1)" -MemberType NoteProperty -Value $member + $loop++ + } + while ($loop -lt $maxgroup) + { + $object | Add-Member -Name "Member $($loop + 1)" -MemberType NoteProperty -Value "" + $loop++ + + } + + + $object | Add-Member -name "TotalList" -MemberType NoteProperty -Value $count + $grouparray.Add($object) | Out-Null + } + + #Définition des paramètres affichés par défaut + + + $defaultProperties = ($grouparray | Get-Member | Where-Object {$_.membertype -eq "NoteProperty" -and $_.name -like "Member*"}).Name + $defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(‘DefaultDisplayPropertySet’,[string[]]$defaultProperties) + $defaultdisplaypropertyset.ReferencedPropertyNames.insert(0,"ID_Group") + $defaultdisplaypropertyset.ReferencedPropertyNames.insert(0,"Name") + $PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet) + + $grouparray | Add-Member MemberSet PSStandardMembers $PSStandardMembers + + } + + Process + { + if ($filter) + { + $filter -match '(?<propertyname>(Name|ID_Group|Member))\s+(?<operator>(-like|-eq|-ceq|-clike))\s+"(?<value>.*)"' | Out-Null + + if ($matches.propertyname -eq "Member") + { + + + Switch ($matches.operator) + { + "-like" { + $return = $grouparray | Where-object {$_.totallist.split(",") -like "$($matches.value)"} + } + "-clike"{ + $return = $grouparray | Where-object {$_.totallist.split(",") -clike "$($matches.value)"} + } + "-eq" { + $return = $grouparray | Where-object {$_.totallist.split(",") -eq "$($matches.value)"} + } + "-ceq" { + $return = $grouparray | Where-object {$_.totallist.split(",") -ceq "$($matches.value)"} + } + } + + return $return + } + else + { + Switch ($matches.operator) + { + "-like" {$return = $grouparray | Where-Object {$_.$($matches.propertyname) -like "$($matches.value)"}} + "-clike"{$return = $grouparray | Where-Object {$_.$($matches.propertyname) -clike "$($matches.value)"}} + "-eq" {$return = $grouparray | Where-Object {$_.$($matches.propertyname) -eq "$($matches.value)"}} + "-ceq" {$return = $grouparray | Where-Object {$_.$($matches.propertyname) -ceq "$($matches.value)"}} + } + return $return + } + } + elseif ($name) + { + return $($grouparray | Where-Object {$_.name -eq $name}) + } + else + { + return $grouparray + } + } +} \ No newline at end of file diff --git a/Linux/Powershell/get_linux_users.ps1 b/Linux/Powershell/get_linux_users.ps1 new file mode 100644 index 0000000..15877e6 --- /dev/null +++ b/Linux/Powershell/get_linux_users.ps1 @@ -0,0 +1,69 @@ +#Script créé par Nicolas Lang - Sous licence CC-BY-SA +#https://nicolaslang.fr + + +function Get-LinuxUsers +{ + [CmdletBinding( + SupportsShouldProcess=$false, + HelpUri="http://nicolaslang.blogspot.com" + )] + param( + [Parameter (Position=0, ValueFromPipeline=$true)] + [ValidateNotNullOrEmpty()] + [string]$name, + [Parameter (Mandatory=$false)] + [ValidateNotNullOrEmpty()] + [ValidatePattern('(?<propertyname>(Name|Pass|ID_User|ID_Group|Comment|Home|Shell))\s+(?<operator>(-like|-eq|-ceq|-clike))\s+"(?<value>.*)"')] + [string]$filter, + [Parameter (DontShow,ValueFromPipelineByPropertyName=$true)] + [AllowNull()] + $totallist + ) + + $users = Get-content /etc/passwd + $userarray = New-Object System.Collections.ArrayList + foreach ($user in $users) + { + $splitted = $user.split(":") + $object = [PSCustomObject]@{ + "Name" = $splitted[0] + "Pass" = $splitted[1] + "ID_User" = $splitted[2] + "ID_Group" = $splitted[3] + "Comment" = $splitted[4] + "Home" = $splitted[5] + "Shell" = $splitted[6] + } + $userarray.Add($object) | Out-Null + + } + if($totallist) + { + $return = $userarray | Where-Object {$_.name -in $totallist.split(",")} + return $return + } + if ($filter) + { + $filter -match '(?<propertyname>(Name|Pass|ID_User|ID_Group|Comment|Home|Shell))\s+(?<operator>(-like|-eq|-ceq|-clike))\s+"(?<value>.*)"' | Out-Null + + + Switch ($matches.operator) + { + "-like" {$return = $userarray | Where-Object {$_.$($matches.propertyname) -like "$($matches.value)"}} + "-clike"{$return = $userarray | Where-Object {$_.$($matches.propertyname) -clike "$($matches.value)"}} + "-eq" {$return = $userarray | Where-Object {$_.$($matches.propertyname) -eq "$($matches.value)"}} + "-ceq" {$return = $userarray | Where-Object {$_.$($matches.propertyname) -ceq "$($matches.value)"}} + } + return ($return) + + } + elseif ($name) { + $return = $userarray | Where-Object {$_.name -eq $name} + return ($return) + } + else { + return $userarray + } + +} \ No newline at end of file