Powershell help - tombstoned computer script

I've created a script for disabling and moving a specific computer account to the disable workstations OU.  Now I'm trying to adapt it to disable/move many results at once.  Here's the single user script:

 
$Computer = Read-Host "Enter the host name"
$time = Read-Host "Enter InactiveFor time in days"
# Disabling and moving computer accounts to Disabled Workstation OU
Get-QADComputer $Computer -InactiveFor $time

Read-Host "Ok to continue?"
$x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Set-QADComputer $Computer -Description "Disabled on $(Get-Date)"
   
# Disable the account
Disable-QADComputer $Computer

# Move the account
Move-QADObject $Computer -NewParentContainer "domain.name/disabled workstations"

"Accounts disabled, moved and password changed"
"Have a nice day"

Open in new window


Now here's what I've got for the multiple:

 
# Establish AD session as Admin
connect-QADService -credential $cred
#Disabling and moving ALL computer accounts older than X days
#In the MS workstation OU

# $Computer = Read-Host "Enter the host name"
$time = Read-Host "Enter InactiveFor time in days"
# Disabling and moving computer accounts to Disabled Workstation OU
Get-QADComputer -SearchRoot 'domain.name/machines/workstations/ms' -InactiveFor $time

Read-Host "Ok to continue?"
$x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Set-QADComputer -SearchRoot 'domain.name/machines/workstations/ms' -Inactivefor $time -Description "Disabled on $(Get-Date)"
   
# Disable the account
Disable-QADComputer $Computer

# Move the account
Move-QADObject $Computer -NewParentContainer "domain.name/disabled workstations"

"Accounts disabled, moved and password changed"
"Have a nice day"

Open in new window


Now my hangup is line 14, it seems that I cannot '-Searchroot' and '-InactiveFor' in the same command.  How can I search a specific OU by the InactiveFor variable?
LVL 14
Ben HartAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale HarrisProfessional Services EngineerCommented:
Are you sure you're using -Searchroot correctly?

Try:
-searchroot "ou=ms,ou=workstations,ou=machines,dc=domain,dc=name"

I haven't tested this since I'm not at work.

HTH,

Dale Harris
Ben HartAuthor Commented:
Well I had assumed it was the correct syntax since the full command minus the "-Inactivefor $time" returns computer names from the right OU.  Lets see what your suggestion does.
Ben HartAuthor Commented:
No go..
PS C:\Users\bhart.DIFC\Desktop> set-QADComputer -SearchRoot 'domain.name/machines/workstations/ms' -inactivefor 120 -Description "Disabled on $(Get-Date)"
Set-QADComputer : A parameter cannot be found that matches parameter name 'SearchRoot'.
At line:1 char:28
+ set-QADComputer -SearchRoot <<<<  'domain.name/machines/workstations/ms' -inactivefor 120 -Description "Disabled on $(Get-Date)"
    + CategoryInfo          : InvalidArgument: (:) [Set-QADComputer], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.SetComputerCmdlet
 

Open in new window


Your suggestion:
Set-QADComputer : A parameter cannot be found that matches parameter name 'SearchRoot'.
At line:1 char:28
+ set-QADComputer -SearchRoot <<<<  "ou=ms,ou=workstations,ou=machines,dc=extension,dc=sub,dc=domain" -inactivefor 120 -Description "Disabled on $(Get-D
ate)"
    + CategoryInfo          : InvalidArgument: (:) [Set-QADComputer], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.SetComputerCmdlet

Open in new window


Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Ben HartAuthor Commented:
Damn I wish there was an edit button
PS C:\Users\bhart.DIFC\Desktop> set-QADComputer -SearchRoot "ou=ms,ou=workstations,ou=machines,dc=org,dc=root01,dc=difc" -inactivefor 120 -Description "Disabled on $(Get-Date)"
Set-QADComputer : A parameter cannot be found that matches parameter name 'SearchRoot'.
At line:1 char:28
+ set-QADComputer -SearchRoot <<<<  "ou=ms,ou=workstations,ou=machines,dc=org,dc=root01,dc=difc" -inactivefor 120 -Description "Disabled on $(Get-D
ate)"
    + CategoryInfo          : InvalidArgument: (:) [Set-QADComputer], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.SetComputerCmdlet

Open in new window

Ben HartAuthor Commented:
I may have discovered my problem.. apparently '-InactiveFor' is not a valid option for 'Set-QADComputer' but only for 'Get-QADComputer'.  Hmm now my idea in its present form is shot.  I know the results from a Get command would be saved in memory but how can I access and run a Set command against the contents in memory?  Like if I 'get-qadcomputer -searchroot "blah/blah"' then how could I run another filtering command like 'get-qadcomputer -inactivefor' against the results of the previous command?
Dale HarrisProfessional Services EngineerCommented:
Oh it won't work for "Set-QADComputer"... Only "Get".

You can do this:
$Computers = Get-QADComputer -SearchRoot 'domain.name/machines/workstations/ms' -InactiveFor $time
"Found $(Computers.count) to disable..."
Read-Host "Ok to continue?"
$x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

foreach ($Computer in $Computers){
Set-QADComputer $Computer -Description "Disabled on $(Get-Date)"

# Disable the account
Disable-QADComputer $Computer

# Move the account
Move-QADObject $Computer -NewParentContainer "domain.name/disabled workstations"
}

"Computers disabled, moved and description changed"
"Have a nice day"
Pause
Ben HartAuthor Commented:
Ran this, first error noted in console below:

The term 'Computers.count' is not recognized as the name of a cmdlet, function, script file, or operable program
. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\bhart.DIFC\AppData\Local\Temp\fedf1302-e1f7-4ece-9666-0c29c845fdb8.ps1:11 char:16
+ Computers.count <<<< 
    + CategoryInfo          : ObjectNotFound: (Computers.count:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Open in new window


Then it appears to not like the OK to continue box, which is probably my choice of bad script.
Read-Host : An error of type "System.Management.Automation.Host.PromptingException" has occurred.
At C:\Users\bhart.DIFC\AppData\Local\Temp\fedf1302-e1f7-4ece-9666-0c29c845fdb8.ps1:12 char:10
+ Read-Host <<<<  "Ok to continue?"
    + CategoryInfo          : ResourceUnavailable: (:) [Read-Host], PromptingException
    + FullyQualifiedErrorId : System.Management.Automation.Host.PromptingException,Microsoft.PowerShell.Command 
   s.ReadHostCommand
 
Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented."
At C:\Users\bhart.DIFC\AppData\Local\Temp\fedf1302-e1f7-4ece-9666-0c29c845fdb8.ps1:13 char:28
+ $x = $Host.UI.RawUI.ReadKey <<<< ("NoEcho,IncludeKeyDown")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Open in new window



soostibiCommented:
You should change your line 14 to:
Get-QADComputer -SearchRoot 'domain.name/machines/workstations/ms' -Inactivefor $time | Set-QADComputer -Description "Disabled on $(Get-Date)"

Open in new window

soostibiCommented:
Your whole script should look like this:
# Establish AD session as Admin  
connect-QADService -credential $cred  
#Disabling and moving ALL computer accounts older than X days  
#In the MS workstation OU  
  
# $Computer = Read-Host "Enter the host name"  
$time = Read-Host "Enter InactiveFor time in days"  
# Disabling and moving computer accounts to Disabled Workstation OU  
Get-QADComputer -SearchRoot 'domain.name/machines/workstations/ms' -InactiveFor $time  
  
Read-Host "Ok to continue?"  
$x = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")  

Get-QADComputer -SearchRoot 'domain.name/machines/workstations/ms' -Inactivefor $time | 
    ForEach-Object {
        Set-QADComputer -Description "Disabled on $(Get-Date)" -Identity $_
        Disable-QADComputer -Identity $_
        Move-QADObject -Identity $_ -NewParentContainer "domain.name/disabled workstations"  
    }
  
"Accounts disabled, moved and password changed"  
"Have a nice day"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale HarrisProfessional Services EngineerCommented:
Yes, the above script by Soostibi would save a few more lines of code.  Except I would say that line 9 is no longer needed.

And regarding the error you're getting, I forgot to include the $ in front of computers

"Found $($Computers.count) to disable..."

If you want to also do the Read-Host thing that you're getting an error with, I would do the following:

$Pause = Read-Host "Press enter to continue..."

And take out line 12.

Here's the new script with both Soostibi's suggestions (because it's more efficient than mine) and some additional small changes:

 
# Establish AD session as Admin  
connect-QADService -credential $cred  
#Disabling and moving ALL computer accounts older than X days  
#In the MS workstation OU
 
$time = Read-Host "Enter InactiveFor time in days"  
#Disabling and moving computer accounts to Disabled Workstation OU  
  
$Pause = Read-Host "Press Enter to continue..."  

Get-QADComputer -SearchRoot 'domain.name/machines/workstations/ms' -Inactivefor $time | 
    ForEach-Object {
        "Disabling $_.dnshostname"
        Set-QADComputer -Description "Disabled on $(Get-Date)" -Identity $_
        Disable-QADComputer -Identity $_
        Move-QADObject -Identity $_ -NewParentContainer "domain.name/disabled workstations"  
    }
 
"Script has completed."  
"Have a nice day."

Open in new window


HTH,

DH
Ben HartAuthor Commented:
Exactly what I needed.. you guys rock!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.