Domanda

PowerShell seems to not want to nest for loops, so I'm guessing this must be piped...

Currently I am pulling from a .csv a list of usersnames (column A) and their updated account expiration dates (Column B). What I would like to do is cross reference each user name in the file to make sure that it is indeed a user rather than a "contact." Sometimes the users are disabled. which is fine considering the account will stay disabled even though the expiration date is current. However it seems to fail when I hit a username that is a contact rather than an actual user. (Why are contacts in the list? Because the list comes from a vendor)

Import-Module ActiveDirectory
$userlist = Import-Csv "\\share\expired.csv" -Header Name,Date


foreach ($line in $userlist) {  

    $line
    Set-ADAccountExpiration -Identity $line.Name -Date $line.Date

}
È stato utile?

Soluzione

Import-Module ActiveDirectory
$userlist = Import-Csv "\\share\expired.csv" -Header Name,Date

foreach ($line in $userlist) {  

    try { 
      Set-ADAccountExpiration -Identity $line.Name -Date $line.Date 
      $line
    }
    catch { "could not expire: $line" }
}

Altri suggerimenti

Pipe to Where-Object and check if the current Name is a user object:

Import-Csv \\share\expired.csv -Header Name,Date | 
Where-Object { Get-ADObject -LDAPFilter "(&(name=$($_.Name))(objectclass=user))" } |
Foreach-Object { Set-ADAccountExpiration ... }

If you don't want a pipeline, continue will move to the next item in the foreach loop.

foreach ($line in $userlist) {
    <Shay Levy's nifty check for user vs contact>
    if ($contact) {continue}
    $line
    Set-ADAccountExpiration -Identity $line.Name -Date $line.Date

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top