简体   繁体   English

Powershell | 有没有办法过滤掉数组中小于 6 个月大的项目?

[英]Powershell | Is there a way to filter out items in an array that are less than 6 months old?

Background Info背景信息

I've written a script that uses an API to get me all users of an application, which I've written to specifically get their email addresses and the last time they logged in. The script stores the results in an array, and then writes that array to a CSV file.我编写了一个脚本,它使用 API 来获取应用程序的所有用户,我编写该应用程序是为了专门获取他们的 email 地址和他们上次登录的时间。脚本将结果存储在一个数组中,然后写入该数组到 CSV 文件。 I then also have written a separate script using another API endpoint, that deactivates users within this application based on the email addresses I pass to it from a CSV.然后,我还使用另一个 API 端点编写了一个单独的脚本,该脚本根据我从 CSV 传递给它的 email 地址停用此应用程序中的用户。

Currently, I'm deactivating users who haven't signed in for 6 months or more and using Google Sheets/Excel to manipulate the initial data output, to filter results to only those users who last date logged in is 6 months ago or greater, exporting that as a separate CSV to then import into the deactivation script.目前,我正在停用 6 个月或更长时间未登录的用户,并使用 Google 表格/Excel 来操作初始数据 output,以仅将结果过滤到上次登录日期为 6 个月或更早的用户,将其作为单独的 CSV 导出,然后导入到停用脚本中。

Here's the question!这是问题!

Is there a way within powershell that I can complete the data manipulation completely, so I may be able to link these scripts into one, or be able to complete these actions without having to manually manipulate the data outside of powershell? powershell 中是否有一种方法可以完全完成数据操作,因此我可以将这些脚本链接成一个,或者能够完成这些操作而无需手动操作 powershell 之外的数据?

To make things slightly more challenging (to me, as a moderately experienced powershell user, but nowhere near an expert:) is that the date format output from the API call is in this format: 2020-01-01T15:00:00Z To make things slightly more challenging (to me, as a moderately experienced powershell user, but nowhere near an expert:) is that the date format output from the API call is in this format: 2020-01-01T15:00:00Z

I have under 50 reputation points, so I can't comment directly, but a solution would be to cast the date output string to the datetime type.我的声望点低于 50,因此无法直接发表评论,但解决方案是将日期 output 字符串转换为 datetime 类型。 The string looks to be under the Zulu date format, which is essentially UTC (doesn't observe daylight saving time).该字符串看起来采用 Zulu 日期格式,本质上是 UTC(不遵守夏令时)。

[datetime]'2020-01-01T15:00:00Z'
# Results in Wednesday, January 1, 2020 10:00:00 AM.

([datetime]'2020-01-01T15:00:00Z').AddDays(-1)
# Results in Tuesday, December 31, 2019 10:00:00 AM.

What @vonPryz mentioned is that once done, you can play around with the AddDays method and add the days elapsed in the form of negative integers. @vonPryz 提到的是,一旦完成,您可以使用 AddDays 方法并以负整数的形式添加经过的天数。

Thanks @Dan and @vonPryz for the support - have used both of these in my result!感谢@Dan 和@vonPryz 的支持——在我的结果中使用了这两个!

For context, in the script that queries the API for all users data, it stores the result of their last login time in a value with header of "Last Login" - in retrospect, I would have used LastLogin as that means I don't need quotes when calling later on.对于上下文,在查询 API 以获取所有用户数据的脚本中,它将他们上次登录时间的结果存储在“上次登录”的 header 的值中 - 回想起来,我会使用 LastLogin 因为这意味着我没有稍后调用时需要引号。 I also have a function that imports the CSV file after selecting the file using an FileDialog box and assigned it to $users我还有一个 function 在使用 FileDialog 框选择文件并将其分配给$users后导入 CSV 文件

I also did a bit more reading on other methods that can be used with Get-Date, and found there's also an addmonths() method, so used addmonths(-6)我还对可与 Get-Date 一起使用的其他方法进行了更多阅读,发现还有一个 addmonths() 方法,因此使用了 addmonths(-6)

Here's the link to the post I found on this. 这是我在此找到的帖子的链接

Here's the code that I used to get my final result这是我用来获得最终结果的代码

$currentDate = get-date
$filterDate = $currentDate.addmonths(-6)
$newTable = @()

Foreach($user in $users){
    $lastLogin = $user.'last login'
    if ($lastLogin -eq "") {
  
    } else {
       $convertedLastLogin = [datetime]$lastlogin
    }

    if ($convertedLastLogin -lt $filterDate -or $lastlogin -lt $filterDate) {
        $newTable += $user
    }
}

I can then export the $newTable output to a CSV if I want to.然后,如果我愿意,我可以将$newTable output 导出到 CSV。 I compared this to the data I was getting if I manually manipulated, and it matched perfectly too, so pretty sure I got there in the end!我将其与手动操作时获得的数据进行了比较,它也非常匹配,所以我很确定我最终到达了那里!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM