Powershell脚本查询多个条件的域

问题描述 投票:0回答:1

测试代码 Powershell 脚本来查询多个条件的域。下面是我的 PowerShell 脚本,该脚本在域中查询 90 天或以上的计算机对象,并将结果输出到 CSV 文件。此脚本使用 Active Directory 模块中的 Get-ADComputer cmdlet。

# Import the Active Directory module
Import-Module ActiveDirectory

# Get the date 90 days ago
$90DaysAgo = (Get-Date).AddDays(-90)

# Get all computer objects that haven't logged on for 90 days or more
$StaleComputers = Get-ADComputer -Filter {LastLogonTimeStamp -lt $90DaysAgo} -Properties Name, LastLogonTimeStamp, OperatingSystem, Enabled, IPv4Address, PasswordLastSet

# Create an array to hold the output
$Output = @()

# Loop through each stale computer
foreach ($Computer in $StaleComputers) {
    # Get the computer's last logon date
    $LastLogonDate = [DateTime]::FromFileTime($Computer.LastLogonTimeStamp)

    # Get the computer's password age
    $PasswordAge = (Get-Date) - $Computer.PasswordLastSet

    # Add the computer's information to the output
    $Output += [PSCustomObject]@{
        'Computer Name' = $Computer.Name
        'Last Logon Date' = $LastLogonDate
        'Operating System' = $Computer.OperatingSystem
        'Enabled' = $Computer.Enabled
        'IP Address' = $Computer.IPv4Address
        'Password Age (Days)' = $PasswordAge.Days
   }
}

# Export the output to a CSV file
$Output | Export-Csv -Path "Stale_Computers.csv" -NoTypeInformation

仍在测试阶段

powershell
1个回答
0
投票

找不到“op_Subtraction”的重载和参数计数:“2”。 行:19 字符:5

  • $PasswordAge = (Get-Date) - $Computer.PasswordLastSet
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • 类别信息:未指定:(:) [],MethodException
    • FullyQualifiedErrorId:MethodCountCouldNotFindBest
© www.soinside.com 2019 - 2024. All rights reserved.