比较后如何带入原来的字符串
How to bring in the original string after comparison
第一次提问。我是外国人,英语不好。所以有点难以解释我的问题。可能我的标题也错了。。。
我试图将Active Directory中的所有名称和信息与时间进行比较,然后将它们添加到组中,但是信息中有“WIFI”和数字,所以我将信息正则表达式并与时间进行比较
$time = Get-Date -Format "yyyyMMdd"
$ccc = Get-ADUser -Filter {info -like "WIFI*" }-properties name,info | select name,info
$bbbs = $ccc -replace '\D+([0-9]*).*',''
Get-ADUser -Filter {info -like "WIFI*" }-properties name,info | select name,info | export-csv -path c:\test.csv -NoTypeInformation -Encoding "UTF8"
Import-CSV "C:\test.csv" | % {Add-ADGroupMember -Identity namegroup -Members $_.Name}
$bbbs -lt $time
$ccc 的输出在这里
name info
---- ----
mike WIFI-20210515 VPN-20210512
terry WIFI-20210519 VPN-20210519
hack WIFI-20210530 VPN-20210513
$bbbs 的输出在这里
20210515
20210519
20210530
但我不知道如何WIFI-“数据时间”小于当前日期,将其从组中删除。(
我使用 CSV 文件加入了群组)
我不知道该怎么做。
虽然目前还不清楚您想比较 info
中的哪些(日期)数字,但是我 认为 您想要实现的一种方法可以是:
# get the reference date as DateTime object
$refDate = (Get-Date).Date # today at midnight
# better keep the returned ADUsers objects complete, with added 'Info' property
# if you want to add them to a Group. By default, Get-ADUser returnes these properties:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
$users = Get-ADUser -Filter "Info -like 'WIFI*'" -Properties Info |
# for convenience, also add a custom property 'WifiDate' converted to DateTime object to compare against.
# if you need to use the other date (the one after "VPN-"), then change below `` to ``
Select-Object *, @{Name = 'WifiDate'; Expression = {[datetime]::ParseExact(($_.Info -replace '^WIFI-(\d{8}).*(\d{8})', ''), 'yyyyMMdd', $null)}}
# next filter to get all users that have a 'WifiDate' earlier than today
$usersToAddToGroup = $users | Where-Object { $_.WifiDate -lt $refDate }
现在您可以使用以下方式将它们添加到广告组:
Add-ADGroupMember -Identity 'TheNameOfTheGroup' -Members $usersToAddToGroup
当然,如果你想从组中删除那些用户,将Add-GroupMember
替换为Remove-ADGroupMember
正则表达式详细信息:
^ Assert position at the beginning of the string
WIFI- Match the characters “WIFI-” literally
( Match the regular expression below and capture its match into backreference number 1
\d Match a single digit 0..9
{8} Exactly 8 times
)
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 2
\d Match a single digit 0..9
{8} Exactly 8 times
)
第一次提问。我是外国人,英语不好。所以有点难以解释我的问题。可能我的标题也错了。。。 我试图将Active Directory中的所有名称和信息与时间进行比较,然后将它们添加到组中,但是信息中有“WIFI”和数字,所以我将信息正则表达式并与时间进行比较
$time = Get-Date -Format "yyyyMMdd"
$ccc = Get-ADUser -Filter {info -like "WIFI*" }-properties name,info | select name,info
$bbbs = $ccc -replace '\D+([0-9]*).*',''
Get-ADUser -Filter {info -like "WIFI*" }-properties name,info | select name,info | export-csv -path c:\test.csv -NoTypeInformation -Encoding "UTF8"
Import-CSV "C:\test.csv" | % {Add-ADGroupMember -Identity namegroup -Members $_.Name}
$bbbs -lt $time
$ccc 的输出在这里
name info
---- ----
mike WIFI-20210515 VPN-20210512
terry WIFI-20210519 VPN-20210519
hack WIFI-20210530 VPN-20210513
$bbbs 的输出在这里
20210515
20210519
20210530
但我不知道如何WIFI-“数据时间”小于当前日期,将其从组中删除。( 我使用 CSV 文件加入了群组)
我不知道该怎么做。
虽然目前还不清楚您想比较 info
中的哪些(日期)数字,但是我 认为 您想要实现的一种方法可以是:
# get the reference date as DateTime object
$refDate = (Get-Date).Date # today at midnight
# better keep the returned ADUsers objects complete, with added 'Info' property
# if you want to add them to a Group. By default, Get-ADUser returnes these properties:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
$users = Get-ADUser -Filter "Info -like 'WIFI*'" -Properties Info |
# for convenience, also add a custom property 'WifiDate' converted to DateTime object to compare against.
# if you need to use the other date (the one after "VPN-"), then change below `` to ``
Select-Object *, @{Name = 'WifiDate'; Expression = {[datetime]::ParseExact(($_.Info -replace '^WIFI-(\d{8}).*(\d{8})', ''), 'yyyyMMdd', $null)}}
# next filter to get all users that have a 'WifiDate' earlier than today
$usersToAddToGroup = $users | Where-Object { $_.WifiDate -lt $refDate }
现在您可以使用以下方式将它们添加到广告组:
Add-ADGroupMember -Identity 'TheNameOfTheGroup' -Members $usersToAddToGroup
当然,如果你想从组中删除那些用户,将Add-GroupMember
替换为Remove-ADGroupMember
正则表达式详细信息:
^ Assert position at the beginning of the string
WIFI- Match the characters “WIFI-” literally
( Match the regular expression below and capture its match into backreference number 1
\d Match a single digit 0..9
{8} Exactly 8 times
)
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 2
\d Match a single digit 0..9
{8} Exactly 8 times
)