Как получить скрипт PowerShell для удаления пользователей на других компьютерах? - PullRequest
0 голосов
/ 29 ноября 2018

Я тестирую скрипт локально для удаления учетных записей пользователей на ВМ.Это скрипт:

#This script will do the following actions:
#1.- Get a computer list from a TXT file
#2.- Get a list of users from a TXT to be removed from the local users group
#3.- Do a ping to every computer on the list, if the computer is offline it will skip it and pass to the next one
#4.- If the computer answers the ping it will search into the local users group and if a user matches with a user from the user list it will be removed
#5.- Creates a log with all the transactions

# Log Time Variables
$Date = Get-Date -UFormat %b-%m-%Y
$Hour = (Get-Date).Hour
$Minutes = (Get-Date).Minute
$Log = "C:\Scripts\Remove-LocalAdmins Masive-" + $Date + "-" + $Hour + "-" + $Minutes + ".log"

#Creates a log file for this process
Start-Transcript -Path $Log  -Force 

#List of computers to be check
$ComputerNames = Get-Content "C:\Users\admin\Downloads\User Deletion Scripts\TestLocal.txt"

#Ping the computers on the list
foreach ($ComputerName in $ComputerNames) {

#If theres no ping answer pass to the next one
if ( -not(Test-Connection $ComputerName -Quiet -Count 1 -ErrorAction Continue )) {
Write-Output "Computer $ComputerName not reachable (PING) - Skipping this computer..." }

#If computer does answer the ping
Else { Write-Output "Computer $computerName is online"

#Search into Users
$LocalUsers = "Users"
$Group = [ADSI]("WinNT://$computerName/$localUsers,group")
$Group.Members() |
foreach {
$AdsPath = $_.GetType().InvokeMember('Adspath', 'GetProperty', $null, $_, $null)
$A = $AdsPath.split('/',[StringSplitOptions]::RemoveEmptyEntries)
$Names = $a[-1] 
$Domain = $a[-2]

#Gets the list of users to be removed from a TXT that you specify and checks if theres a match in the local group
foreach ($name in $names) {
Write-Output "Verifying the local users on computer $computerName" 
$localuser = Get-Content "C:\Users\admin\Downloads\User Deletion Scripts\TestUsers.txt"
foreach ($localuser in $localuser) {
if ($name -eq $localuser) {

#If it finds a match it will notify you and then remove the user from the local users group
Write-Output "User $localuser found on computer $computerName ... "
Remove-LocalUser $localuser

Write-Output "Removed" }}}}}

#Passes all the information of the operations made into the log file
}Stop-Transcript

Когда я проверяю его, виртуальная машина получает эхо-запрос.Однако я получаю следующее:

The following exception occurred while retrieving member "Members": "The 
network path was not found.
"
At C:\Users\admin\Downloads\User Deletion Scripts\Remove-LocalAdmins 
Masive.ps1:39 char:1
+ $Group.Members() |
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemExceptio 
   n
    + FullyQualifiedErrorId : CatchFromBaseGetMember

Возможно, я что-то упускаю из-за брандмауэров или чего-то еще, но мне нужен способ, чтобы этот скрипт работал с моего компьютера на виртуальную машину.Что мне нужно изменить в скрипте, чтобы выполнить удаление пользователей?

Согласно комментариям ниже, мой скрипт должен выглядеть примерно так:

    $Computer = Get-Content "C:\Users\admin\Downloads\Delete-Users\Computer(s).txt"

Invoke-Command -ComputerName $ComputerName.Trim() -ScriptBlock {

#enable remoting
Enable-PSRemoting -Force

#Get local users from admin group
$LocalUsers = "Users"
$Group = [ADSI]("WinNT://$computerName/$localUsers,group")
$Group.Members() |
foreach {
$AdsPath = $_.GetType().InvokeMember('Adspath', 'GetProperty', $null, $_, $null)
$A = $AdsPath.split('/',[StringSplitOptions]::RemoveEmptyEntries)
$Names = $a[-1] 
$Domain = $a[-2]

$LocalUser = foreach ($name in $names) {
Write-Output "Verifying the local users on computer $computerName" 
$localuser = Get-Content "C:\Users\admin\Downloads\Delete-Users\User(s).txt"
foreach ($localuser in $localuser) {
if ($name -eq $localuser) {

Foreach($User in $LocalUser){
#Check if the user is the one to delete and delete it using Remove-LocalUser
Write-Output "User $localuser found on computer $computerName ... "
Remove-LocalUser $localuser
Write-Output "Removed" 

                    }
                }
            }
        }
    }
}

Ответы [ 2 ]

0 голосов
/ 29 ноября 2018

Вы не говорите, какую версию PowerShell вы используете?

v5 имеет командлеты для локального управления пользователями, а если вас там нет, в галерее MS PS есть модель для локального управления пользователями.

Вы пытаетесь подключиться к удаленному хосту и не говорите, как настроили PSRemting.Это означает, что если это домен, в котором находятся ваша рабочая станция и хосты, или если это рабочая группа.

Вы пытаетесь использовать локальные переменные на удаленном хосте, и вы не настраиваете область действия переменной для этого.

  • PSRemoting должен быть включен, чтобы это работало.
  • Вы должны указать свои переменные для удаленного использования.
  • Вам понадобится Invoke-Command для запуска этого кода на удаленном хосте.
  • Вы должны быть администратором накаждый удаленный хост.

Итак, эту среднюю часть следует заменить на что-то вроде этого ...

Else 
{ 
    Write-Output "Computer $computerName is online"

    Invoke-Command -ComputerName $computerName -ScriptBlock {
        # Search into Users
        $LocalUsers = "Users"
        $Group = [ADSI]("WinNT://$Using:computerName/$localUsers,group")

        $Group.Members() |
        foreach 
        {
            $AdsPath = $_.GetType().InvokeMember('Adspath', 'GetProperty', $null, $_, $null)
            $A = $AdsPath.split('/',[StringSplitOptions]::RemoveEmptyEntries)
            $Names = $a[-1] 
            $Domain = $a[-2]


            # Gets the list of users to be removed from a TXT that you specify and 
            # checks if theres a match in the local group

            foreach ($name in $names) 
            {
                Write-Output "Verifying the local users on computer $computerName" 
                $localuser = Get-Content "C:\Users\admin\Downloads\User Deletion Scripts\TestUsers.txt"

                foreach ($localuser in $localuser) 
                {
                    if ($name -eq $localuser) 
                    {

                        # If it finds a match it will notify you and then remove 
                        # the user from the local users group

                        Write-Output "User $localuser found on computer $computerName ... "
                        Remove-LocalUser $localuser

                        Write-Output "Removed" 
                    }
                }
            }
        }
    }
0 голосов
/ 29 ноября 2018

Убедитесь, что в текстовом файле нет пробелов в конце.

И я хотел бы указать на некоторые ошибки в коде.

  • foreach ($localuser in $localuser) нетЯ считаю, что это опечатка, и вы можете это понять.
  • Remove-LocalUser $localuser не сработает, поскольку $LocalUser является пользователем с виртуальной машины, а Remove-LocalUser выполняется на локальной машине.

Вы можете делать все внутри Invoke-Command, как показано ниже.

$Computer = Get-Content "C:\Users\admin\Downloads\User Deletion Scripts\TestLocal.txt"
Invoke-Command -CompuerName $ComputerName.Trim() -ScriptBlock {
    #Get local users from admin group
    $LocalUser = #Get all the local users
    Foreach($User in $LocalUser){
       #Check if the user is the one to delete and delete it using Remove-LocalUser
   }

}

Если ваши виртуальные машины используют Hyper-V, вы можете использовать PowerShell direct, который не зависит ни от одной сети.соединение.

Подробнее здесь о PowerShell direct.

...