locked
How can I edit this function to skip a certain OU RRS feed

  • Question

  • Hello, 

    The function in the code box works great , but I can't figure out how to have it skip a specific OU. I tried using the version below, but I get a missing operator error: 

    [PS] C:\source>.\test4.ps1
    Missing expression after unary operator '-'.
    At C:\source\test4.ps1:6 char:96
    + Get-AdUser  -Filter * | ? { ($_.distinguishedname -notmatch "OU=badOU,DC=mydomain,DC=com")} | - <<<< Properties * |
        + CategoryInfo          : ParserError: (-:String) [], ParseException
        + FullyQualifiedErrorId : MissingExpressionAfterOperator

    function LastLogonConvert ($ftDate) {
        $date = [DateTime]::FromFileTime($ftDate)
        if ($date -lt (Get-Date '1/1/1900') -or $date -eq 0 -or $date -eq $null) {"Never"} else {$date}
        } # End function LastLogonConvert

    Get-AdUser  -Filter * | ? { ($_.distinguishedname -notmatch "OU=badOU,DC=mydomain,DC=com")} | -Properties * |
    Select Name,SamAccountName,EmailAddress,AccountExpirationDate,@{Name='lastlogon'; Expression={LastLogonConvert $_.lastlogon}},DistinguishedName | 
    Export-Csv -Path c:\source\data6.csv -NoTypeInformation

    function LastLogonConvert ($ftDate) {
        $date = [DateTime]::FromFileTime($ftDate)
        if ($date -lt (Get-Date '1/1/1900') -or $date -eq 0 -or $date -eq $null) {"Never"} else 
    
    {$date}
        } # End function LastLogonConvert
    
    Get-AdUser  -Filter * -Properties * |
    Select Name,SamAccountName,EmailAddress,AccountExpirationDate,@{Name='lastlogon'; 
    
    Expression={LastLogonConvert $_.lastlogon}},DistinguishedName | 
    Export-Csv -Path c:\source\data7.csv -NoTypeInformation


     

    Thursday, November 9, 2017 8:34 PM

Answers

  • Hey, replace get aduser command with the one below.

    Get-AdUser  -Filter {Enabled -eq $true} -Properties Emailaddress, AccountExpirationDate, lastlogon, DistinguishedName | 
    Where{$_.distinguishedname -notlike "OU=badOU,DC=mydomain,DC=com"} | 
    Select Name,SamAccountName,EmailAddress,AccountExpirationDate,@{Name='lastlogon'; Expression={LastLogonConvert $_.lastlogon}},DistinguishedName | 
    Export-Csv -Path c:\source\data6.csv -NoTypeInformation

    Remember Get-AdUser -filter * -properties * can cripple your domain controller if you have thousands of ad objects.


    • Edited by Naw Thursday, November 9, 2017 9:59 PM
    • Proposed as answer by Richard MuellerMVP Thursday, November 9, 2017 10:12 PM
    • Marked as answer by wedqwdqwedwd Wednesday, November 15, 2017 2:54 AM
    Thursday, November 9, 2017 9:58 PM

All replies

  • Hey, replace get aduser command with the one below.

    Get-AdUser  -Filter {Enabled -eq $true} -Properties Emailaddress, AccountExpirationDate, lastlogon, DistinguishedName | 
    Where{$_.distinguishedname -notlike "OU=badOU,DC=mydomain,DC=com"} | 
    Select Name,SamAccountName,EmailAddress,AccountExpirationDate,@{Name='lastlogon'; Expression={LastLogonConvert $_.lastlogon}},DistinguishedName | 
    Export-Csv -Path c:\source\data6.csv -NoTypeInformation

    Remember Get-AdUser -filter * -properties * can cripple your domain controller if you have thousands of ad objects.


    • Edited by Naw Thursday, November 9, 2017 9:59 PM
    • Proposed as answer by Richard MuellerMVP Thursday, November 9, 2017 10:12 PM
    • Marked as answer by wedqwdqwedwd Wednesday, November 15, 2017 2:54 AM
    Thursday, November 9, 2017 9:58 PM
  • Why all of the converting?  It is totally unnecessary.

    $properties = 'Name', 'SamAccountName', 'EmailAddress', 'AccountExpirationDate', 'LastLogonDate'
    Get-AdUser -Filter * -Properties $properties | Select-Object $properties

    All dates are already converted by PowerShell


    \_(ツ)_/


    Friday, November 10, 2017 12:52 AM
  • If we want my suggestion as a complete answer then:

    $properties = 'Name', 'SamAccountName', 'EmailAddress', 'AccountExpirationDate', 'LastLogonDate'
    Get-AdUser -Filter * -Properties $properties |
        Where-Object{$_.DistinguishedName -notmatch 'OU=badOU,DC=mydomain,DC=com$'} |
        Select-Object $properties |
        Export-Csv C:\source\data6.csv -NoType


    \_(ツ)_/


    • Edited by jrv Friday, November 10, 2017 7:07 AM
    • Proposed as answer by jrv Friday, November 10, 2017 7:08 AM
    Friday, November 10, 2017 7:07 AM