Double quotes added therefore cannot ping pcnames in $pclist file

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Darren Darren 6 months, 2 weeks ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #36137
    Profile photo of Darren
    Darren
    Participant

    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"
    }
    }

    #36139
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    You have to import-csv not get-content

    #36168
    Profile photo of Darren
    Darren
    Participant

    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"

    }

    }

    #36172
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

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

    $ADComputer = Get-ADComputer $($pc.Name) -Properties Description
    
    #36173
    Profile photo of Jeremy Murrah
    Jeremy Murrah
    Participant

    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.

    #36478
    Profile photo of Darren
    Darren
    Participant

    Fantastic! Thanks to all

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.