Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell structure - Disable inactive computer multiple OUs

$
0
0

A follow up from another thread which I somehow fixed, different question and different answer now. Again, I'm extremely new to this and I'd appreciate some assistance. I don't know how I would structure what I'm trying to do. I have the function below and would like to run the same thing to another different OU:

Function Disable-InactiveComputer {

$iComputers = Get-ADComputer -Filter {(LastLogonDate -le $disableD) -and (PasswordLastSet -le $disableD) -and (Name -like "PC-*") -or (Name -like "LT-*")} -SearchBase $desktopOU -Properties * | ?{$_.LastLogonDate -ne $null}
$filePath2 = Get-Date -uformat ($folderPath + "Computer_Account_Disabled_on_%Y%m%d.csv")

if ($iComputers){

Foreach($iComputer in $iComputers){
Set-ADComputer $iComputer -Enabled $false -PassThru -Description "Auto Disabled on $(Get-Date -format 'D')" | Move-ADObject -TargetPath $disabledOU

$cpt = Get-ADComputer $($iComputer.Name) -properties * | Sort-Object LastLogonDate -descending

$props = @{
‘ComputerName’=$cpt.Name;
‘Description’=$cpt.Description;
‘OSVersion’=$cpt.OperatingSystem;
‘SPVersion’=$cpt.OperatingSystemServicePack;
‘LastLogonDate’=$cpt.LastLogonDate}
$obj = New-Object -TypeName PSObject -Property $props
$tobj += $obj
}
$tobj | Select-Object ComputerName, Description, OSVersion, SPVersion, LastLogonDate | Sort-Object Name | Export-Csv $filePath2 -NoTypeInformation
}
}

Do I need to create a variable with multiple values? ie.

$ComputerOUs = @(“OU=DistinguishedName1,DC=test,DC=com", “OU=DistinguishedName2,DC=test,DC=com")

And from there do a Foreach $ComputerOU and obviously remove my -SearchBase from the Set-ADComputer line?



Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>