none
Set-Adcomputer "CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADComputer" RRS feed

  • Question

  • Hello,

    I've set a script to disable inactive computers older than nDays.
    this script should be scheduled.
    It adds a comment to the existing comments (if they exist).
    And then exports results to a CSV and and sends results by email.
    It works like a charm on my test OU (with 2 machines)

    But I get an error when I test it on larger OU.

    I cant figure out what I'm missing?
    Empty object (description)? Invalid syntax?
    So why is it working on test OU?
    Can you guys help me out?

    Here is the error I get:
    (Sorry I'm working on a FR Workstation)

    Set-ADComputer : Impossible de convertir «System.Object[]» en type «Microsoft.ActiveDirectory.Management.ADComputer», requis par le paramètre «Identity». La méthode spécifiée n'est pas prise 
    en charge.
    Au caractère C:\Script\PowerShell\disable.ps1:43 : 16
    + Set-ADComputer $ADComputer -Description "$($ADComputer.description)-Disabled for...
    +                ~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument : (:) [Set-ADComputer], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADComputer


    Here is the script:

    Import-Module ActiveDirectory
    
    #Time threshold
    $age = (Get-Date).AddDays(-183) 
    $OU = "My-Target-OU"
    
    #Get specific attributes for inactive comps
    $Computers = Get-ADComputer -SearchBase $OU -Properties * -Filter {lastLogonTimestamp -lt $age -and enabled -eq $true} | select Name, description
    
    #Get description to append
    $ADComputer = Get-ADComputer -SearchBase $OU -Properties description -Filter {lastLogonTimestamp -lt $age -and enabled -eq $true}
    
    #Append description with comments
    ForEach ($Name in $Computers){
    
    Set-ADComputer $ADComputer -Description "$($ADComputer.description)-Disabled for inactivity $(Get-Date)-" -Enabled $false 
    
    }
    
    $Computers | Export-Csv C:\Script\Machines_Disabled\Disabled_Computers7_$((Get-Date).ToString('MM-dd-yyyy')).csv -NoTypeInformation
    
    #Sending email
     $encodingMail = [System.Text.Encoding]::UTF8
    
    
     $body = "<HTML><HEAD><META http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" /><TITLE></TITLE></HEAD>"
     $body += "<BODY bgcolor=""#FFFFFF"" style=""font-size: Small; font-family: TAHOMA; color: #000000""><P>"
     $body += "Hello,<br><br>"
     $body += "World"
     Send-MailMessage -From 'DC-01 <SVRDC-01@domain.com>' -To 'Doe, John <jdoe@domain.com>' -Subject 'Disabled Comps' -bodyashtml -Body $body -Encoding $encodingMail -Attachments  C:\Script\Machines_Disabled\Disabled_Computers7_$((Get-Date).ToString('MM-dd-yyyy')).csv
     -SmtpServer 'mail.domain.com'
    


    • Edited by Fahben Tuesday, June 25, 2019 8:11 AM code block
    Friday, June 21, 2019 4:22 PM

Answers

  • You are trying to use an object array where an ADobject is required.  Jst read the error. It tells you the exact command and location of the issue.

    You are using $ADComputer  which is a collection of computers. YOu cannot use  a collection on the "Set" command.

    Please edit you post and post the code with the code posting tool provided as it is completely unreadable in most browser.  That is why we have the tool.


    \_(ツ)_/


    • Edited by jrv Friday, June 21, 2019 5:08 PM
    • Marked as answer by Fahben Tuesday, June 25, 2019 8:31 AM
    Friday, June 21, 2019 5:06 PM
  • This is how you need to do this in PowerShell.

    $body = @'
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title></title>
        </head>"
            <body bgcolor="#FFFFFF" style="font-size: Small; font-family: TAHOMA; color: #000000">
            <p>
            Hello,<br><br>"
            World
        </body>
    </html>
    '@
    
    $csvfile = "C:\Script\Machines_Disabled\Disabled_Computers7_$((Get-Date).ToString('MM-dd-yyyy')).csv"
    $mailprops = @{
        From = 'DC-01 <SVRDC-01@domain.com>'
        To = 'Doe, John <jdoe@domain.com>'
        Subject = 'Disabled Comps'
        BodyAsHtml = $true
        Body = $body
        Encoding = 'UTF8'
        Attachments = $csvfile
        SmtpServer = 'mail.domain.com'
    }
    
    $age = [datetime]::Today.AddDays(-183)
    $OU = 'My-Target-OU'
    Get-ADComputer -SearchBase $OU -Properties description -Filter {LastLogonDate -lt $age -and Enabled -eq $true} |
        ForEach-Object{
            $desc = $_.description + '-Disabled for inactivity' + [datetime]::Now
    $_ | Set-ADComputer -Description $desc -Enabled $false $_ # pass to pipeline } | Select-Object Name, Enabled, Description | Export-Csv $csvfile -NoTypeInformation Send-MailMessage @mailprops
    html tagnames are required to be in lowercase only.


    \_(ツ)_/





    • Edited by jrv Friday, June 21, 2019 5:30 PM
    • Marked as answer by Fahben Tuesday, June 25, 2019 8:31 AM
    Friday, June 21, 2019 5:26 PM

All replies

  • You are trying to use an object array where an ADobject is required.  Jst read the error. It tells you the exact command and location of the issue.

    You are using $ADComputer  which is a collection of computers. YOu cannot use  a collection on the "Set" command.

    Please edit you post and post the code with the code posting tool provided as it is completely unreadable in most browser.  That is why we have the tool.


    \_(ツ)_/


    • Edited by jrv Friday, June 21, 2019 5:08 PM
    • Marked as answer by Fahben Tuesday, June 25, 2019 8:31 AM
    Friday, June 21, 2019 5:06 PM
  • This is how you need to do this in PowerShell.

    $body = @'
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <title></title>
        </head>"
            <body bgcolor="#FFFFFF" style="font-size: Small; font-family: TAHOMA; color: #000000">
            <p>
            Hello,<br><br>"
            World
        </body>
    </html>
    '@
    
    $csvfile = "C:\Script\Machines_Disabled\Disabled_Computers7_$((Get-Date).ToString('MM-dd-yyyy')).csv"
    $mailprops = @{
        From = 'DC-01 <SVRDC-01@domain.com>'
        To = 'Doe, John <jdoe@domain.com>'
        Subject = 'Disabled Comps'
        BodyAsHtml = $true
        Body = $body
        Encoding = 'UTF8'
        Attachments = $csvfile
        SmtpServer = 'mail.domain.com'
    }
    
    $age = [datetime]::Today.AddDays(-183)
    $OU = 'My-Target-OU'
    Get-ADComputer -SearchBase $OU -Properties description -Filter {LastLogonDate -lt $age -and Enabled -eq $true} |
        ForEach-Object{
            $desc = $_.description + '-Disabled for inactivity' + [datetime]::Now
    $_ | Set-ADComputer -Description $desc -Enabled $false $_ # pass to pipeline } | Select-Object Name, Enabled, Description | Export-Csv $csvfile -NoTypeInformation Send-MailMessage @mailprops
    html tagnames are required to be in lowercase only.


    \_(ツ)_/





    • Edited by jrv Friday, June 21, 2019 5:30 PM
    • Marked as answer by Fahben Tuesday, June 25, 2019 8:31 AM
    Friday, June 21, 2019 5:26 PM
  • Hi Jrv,

    I tried to edit my post but spam prevention system didnt allow me to do so.
    I was sick during the weekend so I couldnt answer before.
    Maybe I should've used $ADComputer.description?
    Anyway, thank you for explanation.

    And thank you for your code. which is way simpler.
    It works like a charm.
    Sometimes I try to get complicated code while I could use simpler way. :)
    Thank you very much for your explanation!
    Best


    Tuesday, June 25, 2019 8:10 AM