(我是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
答案 0 :(得分:0)
听起来好像您已经到了那里。只需使用一些管道和一个或两个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
部分按照所说的做