Author Posts

March 6, 2016 at 2:07 pm

hi,
For some reason I cannot search for each names in my $pclist file . Found out export-csv adds double quotes "" to pc names in each row in the pclist file therefore cannot ping PC names which is expected and only way I found around this issue is use the -replace "" removes double quotes . But have another issue cannot use my $pclist in a variable , see # I commented it out below .... Also the fist row it add "names" which I cannot figure out how to remove from pclist file . I just need the actual pc names in the pclist file.. hope this is not all to confusing what I need ..:)

$DayofWeek = Get-Date -Format ddd
$4m = (Get-Date).AddDays(-120)

# Specify the varibles where the accounts are located
$OUdn = "OU= Computers,DC=domain,DC=com"
$OUdisable = "ou=disabled accts,DC=domain,DC=com"
$logdetail = "C:\complog.txt"
$pclist = "C:\pcs.csv"
$logpcsnotping = "C:\$DayofWeek-pcsnotping.txt"
Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt $4m} -SearchBase $OUdn | select name | Export-csv $pclist -NoTypeInformation

${c:\pcs.csv} = ${c:\pcs.csv} -replace '"'
#$pclist = $pclist -replace '"'

$names = Get-Content $pclist

foreach ($name in $names){
if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
# Write-Host "$name,up"
Add-Content $logdetail "$name,up"
}
else{
Add-Content $logpcsnotping "$name"
}
}

March 6, 2016 at 6:04 pm

You have to import-csv not get-content

March 7, 2016 at 8:14 am

ok.. I tried import-csv but am having a problem $pc variable when do GET-ADComputer .. Get-ADComputer : Cannot bind parameter 'Identity'. Cannot convert value "@{name=Desktop-60851}" to type
"Microsoft.ActiveDirectory.Management.ADComputer". Error: "Cannot convert the "@{name=Desktop-60851}" value of type
"System.Management.Automation.PSCustomObject" to type "Microsoft.ActiveDirectory.Management.ADComputer"."
At line:11 char:30
+ $ADComputer = Get-ADComputer $pc -Properties Description....

*****************************************
>>>>>>>>>>>
$pcs = import-csv "C:\pcsnotping.csv"
ForEach ($pc in $pcs)

{ $ADComputer = $null
$ADComputer = Get-ADComputer $pc -Properties Description

If ($ADComputer)

{ Add-Content $logdetail -Value "Found $pc , disabled and moved to Disabled Computers OU"

Set-ADComputer $ADComputer -Description "Computer Disabled on $(Get-Date)" -Enabled $false

Move-ADObject $ADcomputer -targetpath $OUdisable

}

Else

{ Add-Content $logdetail -Value "$pc not in Active Directory"

}

}

March 7, 2016 at 9:22 am

I am assuming Name is the column header, yes? In that case ...

$ADComputer = Get-ADComputer $($pc.Name) -Properties Description

March 7, 2016 at 9:25 am

if you truly only want a single list of computer names, then try changing your output code from

select name | Export-csv $pclist -NoTypeInformation

to something like this

select -expandproperty Name | out-file $pclist

that will give you a text file with no headers or any other formatting data. You shouldn't need to replace any quotes, and you'll want to use get-content to pull them into a list.

March 13, 2016 at 1:50 pm

Fantastic! Thanks to all