【问题标题】:Powershell - Combining Variables Properties from AD outputPowershell - 结合 AD 输出的变量属性
【发布时间】:2022-01-20 17:44:35
【问题描述】:

我尝试编写的第一个脚本。我试图通过比较用户名属性和描述属性来获取具有广告用户名和广告计算机的新变量。我不知道如何根据比较对象或匹配将我想要的属性拉入新变量中。描述属性有一个用户名设置 - ######## 数字非常。 使用的变量(日期告诉过期) $SevenDayWarnDate、$ThreeDayWarnDate、$OneDayWarnDate 广告用户 $7, $3, $1 -properties "Name", "PasswordExpiry 广告电脑 $comp “名称”、“描述” 然后,我将根据到期密码在用户计算机上弹出一个窗口。 以下是我正在尝试做的事情,但我不确定是否通过了所需的信息,因为计算机归档返回为空。

$SevenDayWarnDate = (get-date).adddays(7).ToLongDateString()

$7= Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and PasswordLastSet -gt 0 } `
 -Properties "Name", "msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property "Name", `
 @{Name = "PasswordExpiry"; Expression = {[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed").tolongdatestring() }} `
 |Where-object -Property PasswordExpiry -EQ $SevenDayWarnDate

$comp = Get-Adcomputer -Filter {Enabled -eq $True}  -SearchBase "OU=,DC=" -properties "Name", "Description" `
| Select-Object -Property "Name", "Description" 
Compare-Object -ReferenceObject $7 -DifferenceObject $comp -IncludeEqual -ExcludeDifferent -PassThru |
 ForEach-Object {
  [PSCustomObject]@{
  Name = $_.name
  Computer = ($comp.name | Where-Object Description -match $_.name).Directory
  }
}

基于 Santiago Squarzon 下面的工作代码。

$dayArray= @()
$dayArray=@(7,3,1)

foreach ($day in $dayArray)
{
$SevenDayWarnDate = (get-date).adddays($day).ToLongDateString()
$filter = "Enabled -eq '$True' -and PasswordNeverExpires -eq '$False' -and PasswordLastSet -gt '0'"

$computerArray= @()
$users = Get-ADUser -Filter $filter -Properties "Name", "msDS-UserPasswordExpiryTimeComputed" |
Select-Object Name, @{
            Name = "PasswordExpiry"
            Expression = 
            {
                    [datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed").tolongdatestring()
                }
    } | Where-object -Property PasswordExpiry -EQ $SevenDayWarnDate

# => It might be better to use:
#    PasswordExpiry -ge [datetime]::Now -and PasswordExpiry -le $sevenDayWarnDate

# Find the computers each user is using
$result = foreach($user in $users)
    {
            $temp=$user.Name
            if ($comp = Get-ADComputer -Filter "Description -like '*$temp*'" -Properties Description)
                { 
                

                    [PSCustomObject]@{
                        Name = $user.Name
                        PasswordExpiry = $user.PasswordExpiry
                        ComputerName = $comp.Name
                        ComputerDescription = $comp.Description
                }

                $tmpArray= @()
                $tmpArray= $comp.Name.Split(" ")
                foreach($item in $tmparray)
                    {
                        $computerArray += $item
                    }
                $tmpArray = $Null
                #   }

                }
        continue
        }

foreach($computer in $computerArray) 

$tmpMessage = 
$tmpMessageTitle =

    {Send-RDUserMessage -HostServer $env:COMPUTERNAME -UnifiedSessionID 1 -MessageTitle $tmpMessageTitle -MessageBody $tmpMessage

    }


$result | Format-Table
}

【问题讨论】:

  • 发布了更多代码。我正在尝试将一个变量中的名称与另一个变量中的描述进行比较。取相同的对象,取不同的字段,一个是用户名,一个是计算机名。然后我打算向正在使用的用户发送消息(“ Send-RDUserMessage -HostServer “计算机” -UnifiedSessionID 1 -MessageTitle “来自管理员的消息” -MessageBody “Test 1”)对不起,这是第一次我真的使用 powershell 来收集基本信息,我可能很难解释。
  • 所以您希望将$user 数组与$computer 一起加入。 $computerDescription 属性有用户名或者你怎么知道哪台计算机属于哪个用户?
  • 正确,从它的声音。所以我试图将密码在 7 天内到期的用户与那里的计算机名称进行匹配。
  • 我曾一度试图从描述中删除 -##### 但没有运气。这样我就可以翻转 $7 和 $comp 我认为可能会解决问题,但我不确定。
  • 您确定要这个:PasswordExpiry -EQ $SevenDayWarnDate 吗?这意味着您只会找到具有该确切日期的用户,使用范围不是更有意义吗?即:从这个日期到$SevenDayWarnDate

标签: powershell variables scripting popup output


【解决方案1】:

根据 cmets 和相关代码,我猜这就是您要查找的内容。无需使用Compare-Object,只需根据Description属性查询Active Directory即可获取用户的计算机。

$SevenDayWarnDate = [datetime]::Now.AddDays(7)
$filter = "Enabled -eq '$True' -and PasswordNeverExpires -eq '$False' -and PasswordLastSet -gt '0'"


$users = Get-ADUser -Filter $filter -Properties "Name", "msDS-UserPasswordExpiryTimeComputed" |
Select-Object Name, @{
    Name = "PasswordExpiry"
    Expression = {
        [datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")
    }
} | Where-object -Property PasswordExpiry -EQ $SevenDayWarnDate
# => It might be better to use:
#    {$_.PasswordExpiry -ge [datetime]::Now -and $_.PasswordExpiry -le $sevenDayWarnDate}

# Find the computers each user is using
$result = foreach($user in $users)
{
    if($comp = Get-ADComputer -LDAPFilter "(description=$($user.Name))" -Properties Description)
    {
        [PSCustomObject]@{
            Name = $user.Name
            PasswordExpiry = $user.PasswordExpiry
            ComputerName = $comp.Name
            ComputerDescription = $comp.Description
        }
        
        continue
    }

    Write-Host "No computer was found for User: $($user.Name)"
}

$result | Format-Table

【讨论】:

  • 我正在尝试运行脚本并分解它所做的一切。当以下部分运行时,当我检查 $users 时,$users 保持为空。 br/ 测试
  • @Shawn 对,这就是为什么我在评论中告诉你-EQ 不是比较这个的最佳方法,你应该使用一个日期和另一个日期之间的范围。 => PasswordExpiry -EQ $SevenDayWarnDate 应该换成别的东西。
  • 但是有七个人的过期日期应该放在那里。我尝试了另一个,但出现错误:Where-Object:无法使用指定的命名参数解析参数集。在 line:11 char:5 + } | Where-object -Property PasswordExpiry -ge [datetime]::Now -and Pa ... 感觉我很新,这可能是我做错了什么,我会继续查看并分解代码。
  • 我想我可能已经想通了。 $user 的月份是 2022 年 1 月 13 日下午 3:29:27 的数字,其中 $SevenDayWarnDate 出现在 2021 年 12 月 24 日星期五晚上 7:31:22 我只是想弄清楚日期格式。
  • @ Santiago Squarzon 我能够以您的代码为基础获得工作代码。谢谢大家的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 2015-05-08
相关资源
最近更新 更多