【问题标题】:Delete a list of User Profiles删除用户配置文件列表
【发布时间】:2020-08-06 18:04:11
【问题描述】:

(我是 PS 新手,主要使用 VBS 和 Batch,所以我仍在研究 PS 脚本)

我需要从我们所有 500 个系统中删除大部分(但不是全部)域帐户。 其中一些来自特定列表。 有些遵循通用格式 *21、*19 等...

我可以找到可以让我删除特定用户帐户的脚本,但我不知道如何将长列表传递给它或使用通配符...

如果我能弄清楚如何在其中获取所需的值,这似乎很有希望......

:: 这个脚本取自https://www.nextofwindows.com/delete-user-profiles-on-a-remote-computer-in-powershell


 $Computer = Read-Host "Please Enter Computer Name: "

 $user = Read-Host "Enter User ID: "

Invoke-Command -ComputerName $computer -ScriptBlock {

param($user)

$localpath = 'c:\users\' + $user

Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq $localpath} | 

Remove-WmiObject

} -ArgumentList $user

【问题讨论】:

  • 看来您需要做的只是将条件放入Where-Object 大括号中。
  • 是的,但是当我有一个需要删除的 15 个或更多配置文件的列表时,是否有办法将其传递给列表,而无需专门对每个配置文件进行硬编码?
  • 您是否要从多台计算机上删除多个帐户?
  • 是的。我们有 500 台员工个人电脑,我想从所有系统中删除所有承包商、IT 员工、测试帐户等。所以实际上我想向代码传递计算机列表和配置文件名称列表。

标签: powershell parameter-passing user-profile


【解决方案1】:

听起来你是大部分的路。只需使用一些管道和一个或两个 foreach 循环。

这将尝试从列表中的所有计算机中删除列表中的所有用户:

# define function that takes user list from pipeline
function Remove-User {
    [cmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true,Mandatory=$true)]
        [string]
        $user,
        
        [Parameter(Mandatory=$true)]
        [string]
        $computer
    )
    
    process {
        # copied from question
        Invoke-Command -ComputerName $computer -ScriptBlock {
            Get-WmiObject -Class Win32_UserProfile | 
                Where-Object { $_.LocalPath -eq "c:\users\${$user}" } |
                    Remove-WmiObject
        } -ArgumentList $user
    }
}

# get your lists whatever way makes sense
$userList = Import-Csv -Path "users.csv" -Delimiter ','
$computerList = Import-Csv -Path "computers.csv" -Delimiter ','

# call function to remove all users for each computer
$computerList | ForEach-Object {
    $userList | Remove-User -computer $_
}

我不确定你从哪里得到你的列表,但我使用 csv 只是因为。

*注意:这是假设来自nextOfWindows 的代码的Invoke-Command 部分按照它所说的那样做

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    相关资源
    最近更新 更多