繁体   English   中英

安排 Powershell 脚本每周在 AWS 中运行

[英]Scheduling a Powershell script to run weekly in AWS

因此,我有以下 powershell 脚本来查找不活动的 AD 用户并禁用他们的帐户,创建一个包含已禁用帐户列表的日志文件:

    Import-Module ActiveDirectory

    # Set the number of days since last logon
    $DaysInactive = 60
    $InactiveDate = (Get-Date).Adddays(-($DaysInactive))


    # Get AD Users that haven't logged on in xx days
    $Users = Get-ADUser -Filter { LastLogonDate -lt $InactiveDate -and Enabled -eq $true } -                
    Properties LastLogonDate | Select-Object @{ Name="Username"; Expression=. 
    {$_.SamAccountName} }, Name, LastLogonDate, DistinguishedName


    # Export results to CSV
    $Users | Export-Csv C:\Temp\InactiveUsers.csv -NoTypeInformation


    # Disable Inactive Users
     ForEach ($Item in $Users){
      $DistName = $Item.DistinguishedName
      Disable-ADAccount -Identity $DistName
      Get-ADUser -Filter { DistinguishedName -eq $DistName } | Select-Object @{ Name="Username"; Expression={$_.SamAccountName} }, Name, Enabled
    }

该脚本有效并且正在做它应该做的一切。 我想弄清楚的是如何在 AWS 环境中实现自动化。

我猜我需要在 AWS 中使用 Lambda 函数来触发此脚本按计划运行,但不知道从哪里开始。

非常感谢任何帮助。

我建议使用 dotnet 环境创建一个 Lambda 函数: https : //docs.aws.amazon.com/lambda/latest/dg/lambda-powershell.html

按计划使用 CloudWatch 事件来触发该函数: https ://docs.aws.amazon.com/AmazonCloudWatch/latest/events/RunLambdaSchedule.html

另一种方法是,如果您希望以更多管道风格执行,您可以使用 CodePipeline 和 CodeBuild 来运行脚本。 再次使用 CloudWatch 以按计划触发 CodePipeline!

暂无
暂无

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

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