Création scripts
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user