function Set-AccessOnRemoteMachine {
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[string[]]$ComputerName,
[Parameter(Mandatory = $True)]
[string]$User,
[Parameter(Mandatory = $True)]
[String]$Group,
[Parameter(Mandatory = $True)]
[String]$AccessType
)
if (($AccessType -eq 'Grant') -and ($Group -eq 'Remote Desktop Users')) {
foreach ($Computer in $ComputerName) {
if ((Invoke-Command -ComputerName $Computer -ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
Invoke-Command -ComputerName $Computer -ScriptBlock { param($Group, $User) net localgroup $Group $User /add } -ArgumentList $Group, $User
Write-Output "Successfully added the user $User in the Group $Group on the computer $Computer"
} #if
else {
Write-Output "$Computer is not reachble"
} #else
} #foreach
} #if
elseif (($AccessType -eq 'Grant') -and ($Group -eq 'Administrators')) {
foreach ($Computer in $ComputerName) {
if ((Invoke-Command -ComputerName $Computer -ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
Invoke-Command -ComputerName $Computer -ScriptBlock { param($Group, $User) net localgroup $Group $User /add } -ArgumentList $Group, $User
Write-Output "Successfully added the user $User in the Group $Group on the computer $Computer"
} #if
else {
Write-Output "$Computer is not reachble"
} #elser"
} #foreach
} #elseif
elseif (($AccessType -eq 'Revoke') -and ($Group -eq 'Remote Desktop Users')) {
foreach ($Computer in $ComputerName) {
if ((Invoke-Command -ComputerName $Computer -ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
Invoke-Command -ComputerName $Computer -ScriptBlock { param($Group, $User) net localgroup $Group $User /delete } -ArgumentList $Group, $User
Write-Output "Successfully added the user $User in the Group $Group on the computer $Computer"
} #if
else {
Write-Output "$Computer is not reachble"
} #else
} #foreach
} #elseif
elseif (($AccessType -eq 'Revoke') -and $Group -eq ('Administrators')) {
foreach ($Computer in $ComputerName) {
if ((Invoke-Command -ComputerName $Computer -ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
Invoke-Command -ComputerName $Computer -ScriptBlock { param($Group, $User) net localgroup $Group $User /delete } -ArgumentList $Group, $User
Write-Output "Successfully added the user $User in the Group $Group on the computer $Computer"
} #if
else {
Write-Output "$Computer is not reachble"
} #else
} #foreach
} #elseif
} #Function
Set-AccessOnRemoteMachine -ComputerName $Computer -Group $Group -User $User -AccessType $AccessType