-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveLocalAccOnRemoteMachine.ps1
145 lines (119 loc) · 3.95 KB
/
RemoveLocalAccOnRemoteMachine.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
$Username = "username"
$Password = ConvertTo-SecureString 'password' -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $Username, $Password
$Date=$(get-date -format "MM/dd/yyyy-hh:mmtt").ToString()
function LogAndConsole($Message,$Color){
if($Color -ne $null){
Write-Host $Message -ForegroundColor $Color
$Message | Out-File -FilePath C:\ops\logs\RemoveDheldAcc.txt -Append
}else{
Write-Host $Message
$Message | Out-File -FilePath C:\ops\logs\RemoveDheldAcc.txt -Append
}
}
function Get-LocalUser
{
[CmdletBinding()]
[Alias()]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]$name,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=1)]
[string[]]$Computername = "$Env:computername"
)
Begin
{
}
Process
{
if ($name)
{
$user=[adsi]"WinNT://$($ComputerName[0])/$($name[0]),user"
If ($User.Name -eq $NULL)
{
$user
}
}
else
{
$computer = [ADSI]"WinNT://$($ComputerName[0]),computer"
$user=$computer.psbase.Children | where { $_.psbase.schemaclassname -match 'user' }
}
$user | Select-Object -property `
@{Name='Name';Expression= { $_.name }},`
@{Name='Fullname';Expression= { $_.Fullname }},`
@{Name='Description';Expression= { $_.Description }}
}
End
{
}
}
function Remove-LocalUser
{
[CmdletBinding(SupportsShouldProcess=$true)]
[Alias()]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]$Name,
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
Position=1)]
[string[]]$Computername = "$Env:computername"
)
Begin
{
}
Process
{
if ($PSCmdlet.Shouldprocess("$Name Removed from $computername") )
{
$computer = [ADSI]"WinNT://$($ComputerName[0]),computer"
$computer.delete("user",$name[0])
}
}
End
{
}
}
function main2{
param($region,$Credential,$UserAcc)
$Instances=(Get-EC2Instance -Region $region -Filter @(@{name='instance-state-name';values="running"})).Instances
foreach($Instance in $Instances){
$function1 = "function Get-LocalUser { ${function:Get-LocalUser}}"
$IP= $Instance.PrivateIPAddress
$Users = Invoke-Command -ComputerName $IP -Credential $Credential -ArgumentList $function1 -ScriptBlock{
param($function1)
. ([ScriptBlock]::Create($function1))
$Users = Get-LocalUser
return $Users
}
$User = $Users | Where-Object {$_.Name -like $UserAcc}
if($User.Name -eq $UserAcc){
LogAndConsole -Message "[$Date] [DELETED] [$IP] The user account '$UserAcc' exist and is being deleted"
$us=$User.Name
$function2 = "function Remove-LocalUser { ${function:Remove-LocalUser}}"
Invoke-Command -ComputerName $IP -Credential $Credential -ArgumentList $function2,$us -ScriptBlock{
param($function2,$us)
. ([ScriptBlock]::Create($function2))
Remove-LocalUser -Name $us
}
}else{
LogAndConsole -Message "[$Date] [NOTEXIST] [$IP] The user account '$UserAcc' doesn't exist"
}
}
}
function main(){
$UserAcc = Read-Host -Prompt "Please enter the exact user account name you want to delete inevery region for every running instace"
main2 -region "us-east-1" -Credential $Credential -UserAcc $UserAcc
}
main