Announcement

Collapse
No announcement yet.

Need help with Script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with Script

    Hi ,

    I found the below script on the Internet which claims to

    1. Search for all inactive (over 90 days) Computers accounts in AD . (This works)
    2. Ping each computername and if not reachable disable and move them to an OU.
    3. Update description

    ************************************
    Code:
    $Inactive_computer_OU = "OU=Inactive Computers,DC=domain,DC=local,DC=net"
    $PingMachines = Get-QADComputer -InactiveFor 90
    ForEach($host In $PingMachines){
        $MachineName = $host.name
        $PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$MachineName'" | Select-Object StatusCode
        If ($PingStatus.StatusCode -ne 0){
            Write-Host $MachineName " : offline" -Fore "Red"
            $comp = Get-QADComputer $MachineName |select dn,description
            dsmod computer $comp.dn -disabled yes
            Write-Host "Move the computer account $MachineName to the OU $Inactive_computer_OU"
            Move-QADObject $MachineName -NewParentContainer $Inactive_computer_OU
            Write-Host "Update the computer account description with the source OU name"
            $new_desc = ($comp).dn + " - " + ($comp).description
            Set-QADComputer $MachineName -Description $new_desc
        }
    }
    **************************************

    I modified it as per my environment but it comes up with an error saying
    "$host is read only or constant".

    If I modify it to use '$PingMachines | ForEach-Object{'

    It comes up with the attached scrrenshot. I modified the script as below where I supply the list of computers via a CSV file. It still comes up with the same pop-up.

    *******************************
    Code:
     $Inactive_computer_OU = "OU=Test,OU=DisabledAccounts,DC=mydomain,DC=local"
    $StaleComputers = "d:\Admin.csv"
    $List = Import-Csv $StaleComputers
    $List | ForEach-Object
    {
        $MachineName = $_.Name
        Write-Host $MachineName " : offline" -Fore "Red"
        $comp = Get-QADComputer $MachineName |select dn,description
        dsmod computer $comp.dn -disabled yes
        Write-Host "Move the computer account $MachineName to the OU $Inactive_computer_OU"
        Move-QADObject $MachineName -NewParentContainer $Inactive_computer_OU
        Write-Host "Update the computer account description with the source OU name"
        $new_desc = ($comp).dn + " - " + ($comp).description
        Set-QADComputer $MachineName -Description $new_desc
        
     }
    ***********************************

    I am out of ideas now. I am not very good at scripting or powershell but this one seems very straight forward.

    Any help is greatly appreciated.

    Thanks,
    Chandra
    Attached Files
    Last edited by Rems; 10th October 2012, 10:06. Reason: added [CODE] tags around the code

  • #2
    Re: Need help with Script

    Lazy reply sorry...
    Have you tried oldcmp from here?
    http://www.joeware.net/freetools/tools/oldcmp/
    cheers
    Andy

    Please read this before you post:


    Quis custodiet ipsos custodes?

    Comment


    • #3
      Re: Need help with Script

      Hi Andy,

      I am currently using 'OldCmp' and it has shot out a list of around 1000 stale computers. My primary aim is to read the list from a file and disable and move them to a certain OU.

      I'm being lazy as well..trying to avoid doing that manually..

      Thanks,
      Chandra

      Comment


      • #4
        Re: Need help with Script

        Ah ok no worries!
        I use it to disable the accounts and leave them for 7 days (in the same location) then delete them after the 7 days is up.
        My powershell isn't good enough but you could always run oldcmp to disable them and then run powershell to select all disabled accounts and move them?
        cheers
        Andy

        Please read this before you post:


        Quis custodiet ipsos custodes?

        Comment


        • #5
          Re: Need help with Script

          Hi Andy,

          Thanks for your tip. I think I solved the problem although it's still a 2 step process (actually 3 steps).

          Step1- Run OldCmp to get a list of all stale computers over 90 days.
          Step2- Export it to Excel and copy the 'cn' coloumn to a text file (Without the heading 'cn')
          Step3- Run the below script

          ***********************************
          $names = Get-Content "d:\Computers.txt"
          $Inactive_computer_OU = "OU=Test,OU=DisabledAccounts,DC=medibank,DC=lo cal"

          foreach ($name in $names)
          {
          if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue )
          {
          Write-Host "$name is UP" -ForegroundColor Magenta
          }
          else
          {
          Write-Host "$name is DOWN" -ForegroundColor Red
          $comp = Get-QADComputer $name |select dn,description
          dsmod computer $comp.dn -disabled yes
          Write-Host "Move the computer account" + $name + "to the OU DisabledAccounts OU"
          Move-QADObject $name -NewParentContainer $Inactive_computer_OU
          Write-Host "Update the computer account description with the source OU name"
          $new_desc = ($comp).dn + " - " + ($comp).description
          Set-QADComputer $name -Description $new_desc
          }
          }

          **************************************

          Works like a charm!! I would still love to see all this happen with 1 script, I am happy with this outcome for now.

          Further suggestions are most welcome.

          Regards,
          Chandra

          Comment


          • #6
            Re: Need help with Script

            Oldcmp will disable the accounts for you rather than using powershell and may exclude the need for the text file?
            cheers
            Andy

            Please read this before you post:


            Quis custodiet ipsos custodes?

            Comment


            • #7
              Re: Need help with Script

              Out of the machines that Old CMP found to be stale, several are still plugged in and active on the network.
              Similarly there are some machines which are turned on maybe 3 times in 1 year to do a certain job and then turned off. I need to crop those bits and cannot disable them straight away.
              So the txt file is ok. I remove the known servers from the list and then the script ensures that any system that is still pingable is excluded.

              Regards,
              Chandra

              Comment


              • #8
                Re: Need help with Script

                No worries, each environment is different.

                wrt the ones still online I would check they are participating properly, maybe nltest /sc_query:domain.local
                cheers
                Andy

                Please read this before you post:


                Quis custodiet ipsos custodes?

                Comment

                Working...
                X