从文本文件中删除匹配的集合对象

PCTek支持

我有一个存储在文本文件中的用户列表。我正在尝试更新文本文件,以便删除与 $NotExpiring users 变量匹配的任何用户,该变量是一个集合。如果需要从文本文件中删除多个用户,我就是不知道如何正确更新文本文件。

下面是完整的功能。您可以忽略其中的大部分内容只需查看#Stuck Here 以了解重点。

function Get-NotExpiring{

$NotExpiring=New-Object System.Collections.Generic.List[System.Object]

$MatchedUser=New-Object System.Collections.Generic.List[System.Object]

$textfiles = Get-ChildItem $email_dir



#Day of Span

$Days="20"

#Settings

$Date=Get-Date ((Get-Date).adddays($Days))

$Users=Get-ADUser -filter {(Enabled -eq $True) -and (PasswordNeverExpires -eq $False)} -Properties SamAccountName, DisplayName, msDS-UserPasswordExpiryTimeComputed, Mail | Where-Object { $_.DisplayName -ne $nul -and ($_."msDS-UserPasswordExpiryTimeComputed" -gt ($NotExpDate.ToFileTime()))} | Select SamAccountName, Mail, DisplayName,@{Name="ExpiryDate";Expression={([datetime]::fromfiletime($_."msDS-UserPasswordExpiryTimeComputed")).DateTime}}

#Magic

foreach ($Entry in $Users) {



$EntryDate = Get-date($Entry.ExpiryDate)

if ($EntryDate -gt $Date){



$Account = $Entry.SamAccountName

$ExpDate = $Entry.ExpiryDate

$NotExpiring.add($Account)

}

}



#STUCK HERE

foreach($file in $textfiles){

foreach ($user in $NotExpiring){

if((Get-Content "$email_dir\$file") -contains $user){

$temp_get = Get-Content $email_dir\$file | where {$_ -notmatch $user}

}}}



$temp_get}

我在下面尝试过,但如果超过一个用户 $NotExpiring 也在现有文本文件中,它似乎不起作用。任何帮助,将不胜感激。我知道这是一个简单的修复,但我似乎无法弄清楚。

Get-Content $email_dir\$file | where {$_ -notmatch $user} | Set-Content <path>.txt

我能够使用以下解决方案完全实现我所需要的。

foreach($file in $textfiles){ foreach ($user in $NotExpiring){

if((Get-Content "$email_dir\$file") -contains $user){ 
$MatchedUser.add($user) 
}}
Get-Content "$email_dir\$file" | Where {$MatchedUser -NotContains $_ } | Set Content "$temp_dir\$file" 

Copy-Item -path "$temp_dir\$file" -Destination "$email_dir\$file" -ErrorAction SilentlyContinue }

基本上,您正在尝试匹配两个数组。where你一起做foreach对象。现在您必须将单个对象$_与数组匹配$user
用:

...| where {$_ -notin $user}

或者

...| where {$user -notcontains $_}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从文本文件中删除文本

来自分类Dev

从文本文件中删除与文本文件上的多个正则表达式匹配的行

来自分类Dev

从文本文件中删除^ M

来自分类Dev

从文本文件中删除记录

来自分类Dev

从文本文件中删除空行

来自分类Dev

删除文本文件中的行

来自分类Dev

删除文本文件中的内容

来自分类Dev

文本文件中的对象列表

来自分类Dev

使用批处理脚本删除文本文件中的所有匹配项

来自分类Dev

Bash/sed:从文本文件中删除除匹配项之外的所有内容

来自分类Dev

如何使用 bash 命令删除文本文件中匹配多个模式的行?

来自分类Dev

读取文本文件,在Excel中匹配并粘贴文本

来自分类Dev

读取文本文件,在Excel中匹配并粘贴文本

来自分类Dev

在文本文件中搜索模式与输入匹配的文本

来自分类Dev

复制名称与文本文件中的行匹配的文件

来自分类Dev

从文本文件中删除大量文本(Python)

来自分类Dev

Java文本文件从四列文本文件中删除双打

来自分类Dev

如何删除文本文件中列出的文件

来自分类Dev

如何删除文本文件中列出的文件

来自分类Dev

从文本文件中检索匹配的字符串

来自分类Dev

与文本文件中的模式匹配的Scala RDD

来自分类Dev

检查文本文件中的完全匹配

来自分类Dev

匹配两个文本文件中的行

来自分类Dev

在 Python 中匹配和从文本文件中提取

来自分类Dev

从R中的文本文件中删除特定行

来自分类Dev

如何从Python中的文本文件中删除行?

来自分类Dev

难以从Java中的文本文件中删除行

来自分类Dev

从文本文件中删除单词中的特定模式

来自分类Dev

在 UNIX 中从文本文件中删除记录

Related 相关文章

热门标签

归档