[英]Extract string from specific key word in Powershell
我在 AD 中有一个用户,其描述包含字符串,例如:“accountant TEMP 10/02/2021 and sth”
我想做两件事:
我相信它应该是这样的:
$users = Get-ADUser -Filter * -Properties * -SearchBase "OU=Toster,DC=I,DC=Like,DC=Chocolate"|where {$_.Description -like "*TEMP*" -or $_.Description -like "*PERM*" } |select samaccountname,description,name
现在我有一个条件是 PERM 和 TERM 在描述的开头,但我不希望这是一个永久的解决方案:(
$DataPart=$use.description
$DatePart= $DataPart.substring(5,10) #it substrings after PERM and TEMP a date in description and then i use below code to convert this string to date:
$FinalDate = [datetime]::ParseExact($DatePart,'dd/MM/yyyy',$null)
所以我的问题是我如何从 PERM 和 TERM 关键字而不是字符串中的特定位置进行 substring ,就像我在上面的代码中所做的那样?
尝试:
$users = Get-ADUser -Filter * -Properties Description -SearchBase "OU=Toster,DC=I,DC=Like,DC=Chocolate" |
Where-Object {$_.Description -match "TEMP|PERM" } |
Select-Object SamAccountName,Description,Name,
@{Name = 'FinalDate'; Expression = {
$date = ([regex]'(\d{2}/\d{2}/\d{4})').Match($_.Description).Groups[1].Value
[datetime]::ParseExact($date,'dd/MM/yyyy', $null)
}}
# output on screen
$users | Format-Table -AutoSize
# output to CSV
$users | Export-Csv -Path 'Path\To\tempusers.csv' -NoTypeInformation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.