使用 csv 文件删除掌权 shell 的用户(我缺少什么?)

delete users in power shell using a csv file (What I am missing?)

现在我正在尝试这个

ForEach($list 中的 $name){ Get-CimInstance -ComputerName oa-exwit -ClassName Win32_UserProfile | Where-Object { $_.LocalPath.split('')[-1] -eq $name } |删除 CimInstance

但什么也没发生,没有错误,没有删除用户,什么都没有

到处都是语法错误。除非您使用显示的 cmdlet 更正错误,否则这永远不会起作用。例如:GetContent 应该是 Get-Content。 Powershell 中的 Cmdlet(命令行实用程序)遵循这种“动词-名词”结构,并且始终使用连字符“-”。获取某物,或设置某物。

Get-ChildItem #遍历当前目录。你明白了。

#Import the csv file
$list = Import-Csv -Path C:\my\location\file.csv 

#Assuming you have headers in your csv with one being names for the samaccount name.
#We can iterate through them using a Foreach loop.
    Foreach($name in $list.names){
    Get-CimInstance -ComputerName oa-exwit -ClassName Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq $name } | Remove-CimInstance}

查看此功能以删除远程配置文件(或本地)以节省您的工作:Delete Remote Profiles

试试这个...

Import-Csv -Path "$PWD\file.csv" | 
ForEach{
    Try
    {
        ((Get-CimInstance -ComputerName 'oa-exwit' -ClassName Win32_UserProfile).LocalPath -split('\'))[-1] -eq $PSItem.SamAccountName | 
        Remove-CimInstance -WhatIf
    }
    Catch{$PSItem.Exception.Message}
}

#"C:\Users\caro\script.ps1" 命令 运行 脚本 #list 是一个可变数组,连接到计算机并收集配置文件 #users是一个变量数组,收集要删除的用户 #first loop interact the users array and ask that for each name do interact in the second loop that contains the profiles array; #一旦循环检查第一个循环和第二个循环之间是否有重合,它就会删除实例

$list= Get-CimInstance -ComputerName oa-exwit -Class Win32_UserProfile $users= 获取内容 file.csv

ForEach($users in $users){

ForEach($name in $user){ 
        
    ForEach($profile in $list) {
        if ($name -eq $profile.LocalPath.split('\')[-1]){
        Remove-CimInstance($profile)
        }
    else{
    continue
    }
}
}

}