none
AD Inactive Computers Clean Up RRS feed

  • Question

  • Hello,

    I am trying to get a list of inactive computers this is the current code i have right now. I want to check to see if computer password has been changed in the last 90 days. Once i get this part working i would like to move those computers to a Disable OU in AD.   I have no added the move command. Would I be able to take that csv file  and import it to powershell and have it move all the computers found in that file to a different OU. 

    Import-Module activedirectory
    [int]$ComputerPasswordAgeDays = 90
    IF ((test-path "c:\temp") -eq $False) { md "c:\temp" }
    $ExportFile = "c:\temp\InactiveWorkstations.csv"
    $ComputerStaleDate = (Get-Date).AddDays(-$ComputerPasswordAgeDays)
    $InactiveWorkstations = Get-ADComputer -filter { (passwordLastSet -le $ComputerStaleDate) -and (OperatingSystem -notlike "*Server*") -and (OperatingSystem -like "*Windows*") } -properties Name, DistinguishedName, OperatingSystem,OperatingSystemServicePack, passwordLastSet,LastLogonDate,Description
    $InactiveWorkstations | export-csv $ExportFile


    I am currently getting an error with this script. Can you tell me if it looks correct or if their is a better way of writing this.

    Tuesday, March 29, 2016 2:37 PM

Answers

  • Your Get-ADComputer command is not returning any results, thus $InactiveWorkstations has nothing stored in it to be exported.


    • Marked as answer by Centaur1963 Tuesday, March 29, 2016 5:55 PM
    Tuesday, March 29, 2016 3:20 PM

All replies

  • Would I be able to take that csv file  and import it to powershell and have it move all the computers found in that file to a different OU. 

    Yes:

    http://ss64.com/ps/move-adobject.html

    I am currently getting an error with this script.

    Please post the entire error.


    Tuesday, March 29, 2016 3:07 PM
  • What is the error message?

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, March 29, 2016 3:10 PM
    Moderator
  • Tuesday, March 29, 2016 3:16 PM
  • Thanks I just posted the error i am seeing
    Tuesday, March 29, 2016 3:16 PM
  • Your Get-ADComputer command is not returning any results, thus $InactiveWorkstations has nothing stored in it to be exported.


    • Marked as answer by Centaur1963 Tuesday, March 29, 2016 5:55 PM
    Tuesday, March 29, 2016 3:20 PM
  • Is it because the code is wrong?  I know there has to be computers in our domain who haven't changed their passwords in a while like 2 years because they are disabled.
    Tuesday, March 29, 2016 3:37 PM
  • So I found this on Technet. 

    Do i need to add the domain information and my creds when i am running this script from a DC 

    Parameter Set: Domain
    Add-Computer [-DomainName] <String> -Credential <PSCredential> [-ComputerName <String[]> ] [-Force] [-

    Tuesday, March 29, 2016 3:44 PM
  • I believe your script works fine. I was able to run the script on my machine and I got the list of inactive machines with their details. I think your $Inactiveworkstations does not have anything to export, thus your query returns with error.
    • Proposed as answer by Dejiix Tuesday, March 29, 2016 5:56 PM
    • Unproposed as answer by Dejiix Tuesday, March 29, 2016 5:56 PM
    Tuesday, March 29, 2016 3:52 PM
  • Sorry i am really new at this. Last question.  Do i need to tell it to look into my current AD environment for it to search or is it already doing that and just not returning results.  
    • Proposed as answer by Dejiix Tuesday, March 29, 2016 5:56 PM
    • Unproposed as answer by Dejiix Tuesday, March 29, 2016 5:56 PM
    Tuesday, March 29, 2016 4:03 PM
  • Get-ADComputer by default searches the domain you authenticated to. To test you can just run your Get-ADComputer statement at a PowerShell prompt, without assigning the results to the variable $InactiveWorkstations. You can hard code the number of days. Perhaps change the days to 45, or even 30 to check for results.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, March 29, 2016 5:42 PM
    Moderator