Powershell 使用csv文件删除power shell中的用户(我缺少什么?)
现在我正在尝试这个 ForEach($list中的名称){Get CimInstance-ComputerName oa exwit-ClassName Win32_UserProfile |其中对象{$\本地路径.split('')[-1]-eq$name}|删除CimInstancePowershell 使用csv文件删除power shell中的用户(我缺少什么?),powershell,opencsv,Powershell,Opencsv,现在我正在尝试这个 ForEach($list中的名称){Get CimInstance-ComputerName oa exwit-ClassName Win32_UserProfile |其中对象{$\本地路径.split('')[-1]-eq$name}|删除CimInstance 但是什么都没有发生,没有错误没有删除的用户,什么都没有到处都有语法错误。除非您使用显示的cmdlet更正错误,否则这永远不会起作用。例如:GetContent应该是Get Content。Powershell中
但是什么都没有发生,没有错误没有删除的用户,什么都没有到处都有语法错误。除非您使用显示的cmdlet更正错误,否则这永远不会起作用。例如:
GetContent
应该是Get Content
。Powershell中的cmdlet(命令行实用程序)遵循以下“动词名词”结构,并始终在“-
”中使用连字符。获取或设置某些内容
getchilditem
#通过当前目录枚举。你明白了
#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}
查看此功能可删除远程配置文件(或本地配置文件)以保存您的工作:的尝试此
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”命令来运行脚本
#列表是连接到计算机并收集配置文件的变量数组
#users是一个变量数组,用于收集要删除的用户
#第一个循环与users数组交互,并要求每个名称在包含profiles数组的第二个循环中进行交互;
#一旦循环检查第一个循环和第二个循环之间是否一致,它就会删除该实例
$list=获取CimInstance-ComputerName oa exwit-Class Win32\u用户配置文件
$users=获取内容文件.csv
ForEach($users中的用户){
}请使用正确的代码插入。1.)您应该更改为
导入CSV
.2.)代码中到处都有语法错误。3.)导入CSV后,您可以使用点符号,$list.names
(假设您有一个标题为“names”的列)。这不是WQL或Batch,您必须使用%var%
不起作用的适当变量。4.)如果要使用Foreach对象
cmdlet,您必须通过管道将|
传递到它。5。)正在删除本地计算机上的相应用户,这是您想要的吗?
ForEach($name in $user){
ForEach($profile in $list) {
if ($name -eq $profile.LocalPath.split('\')[-1]){
Remove-CimInstance($profile)
}
else{
continue
}
}
}