locked
Require a script RRS feed

  • Question

  • Hi peeps

    Are there any powershell scripting gurus that can help me generate/find one that does:-

    1. Find stale computer records that are older than 3 months

    2. Record WHERE these computer accounts are in AD.

    3. Move them to a designated OU in AD after finding the results

    4. Have steps 1 & 2 outputted to a CSV file also incase I need to move any computer accounts back to the correct OU for whatever reason.

    Thanks

    • Moved by Bill_Stewart Friday, July 27, 2018 7:48 PM This is not "scripts on demand"
    Thursday, May 10, 2018 11:28 AM

All replies

  • No, but please show what you have now and what the issues you are experiencing, what you have tried and what the results are

    No one is gonna develop a script for you.

    Thursday, May 10, 2018 11:34 AM
  • I have a script that does :-

    1. Find stale computer records for me.

    2.  Outputs the computer names to a CSV file.

    I would like to have the CSV also:-

    1. include a column which records the found computer accounts location in AD

    2. And provide me the option to move them to a OU (Disabled Computers)

    The script I am using for this is:-

    import-module activedirectory 
    $domain = "domain.mydom.com" 
    $DaysInactive = 90 
    $time = (Get-Date).Adddays(-($DaysInactive))
     
    # Get all AD computers with lastLogonTimestamp less than our time
    Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp |
     
    # Output hostname and lastLogonTimestamp into CSV
    select-object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv OLD_Computer.csv -notypeinformation

    Thanks

    Thursday, May 10, 2018 11:41 AM
  • Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp |
        select-object Name,DistinguishedName, @{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} |
        export-csv OLD_Computer.csv -NoTypeInformation

    Just added DistinguishedName.

    For moving objects, please refer to the article :

    So, basically, you can do this:

    $computers = Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp
    $computers | Select-object Name,DistinguishedName, @{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} |
        export-csv OLD_Computer.csv -notypeinformation
    $computers | Move-ADObject -TargetPath 'ou=disabledComputers,dc=domain,dc=mydom,dc=com'

    Thursday, May 10, 2018 12:05 PM
  • Thanks Evgeny.  That's very useful.  I'm not a powershell expert or novice in anyway.

    I'm happy with the CSV output file now.

    Just to confirm your code below moves the machines that have been outputted to the CSV will all be moved and disabled? :-

    $computers | Move-ADObject -TargetPath 'ou=disabledComputers,dc=domain,dc=mydom,dc=com'

    Thursday, May 10, 2018 12:16 PM
  • Thanks.

    The script depository will help massively.

    Thursday, May 10, 2018 1:31 PM
  • The machines will be moved to 
    'ou=disabledComputers,dc=domain,dc=mydom,dc=com'
    Thursday, May 10, 2018 7:24 PM