none
Powershell - Combine CMDLETS RRS feed

  • General discussion

  • I have written the below PowerShell script (new guy) in an effort to query AD and retrieve

    • Computer Name
    • OU
    • OS
    • OS Version and system model

    The script runs but the output does not include the model name. Instead it returns

     

    Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

    In the "Model" column.

     

    Import-Module ActiveDirectory

    # get today's date
    $today = Get-Date

    #Get today - 90 days (2 month old)
    $cutoffdate = $today.AddDays(-90)

    #Get the computer accounts filtered by lastlogondate.
    # Select only required properties of the computer account
    # and export it to a file
    $Computer= Get-ADComputer  -Properties * -Filter {LastLogonDate -gt $cutoffdate} `
    | Select Name,OperatingSystem,OperatingSystemVersion, `
    LastLogonDate,CanonicalName, Model
    $Model= Get-WmiObject -Query “SELECT * FROM Win32_computersystem” | Select Model
    $Computer+$Model.Model | Export-Csv c:\ActiveComputers.csv

    Any assistance would be greatly appreciated.


    DHeinz


    • Edited by DRHeinz Monday, October 2, 2017 7:28 PM
    • Changed type Bill_Stewart Tuesday, November 7, 2017 10:45 PM
    • Moved by Bill_Stewart Tuesday, November 7, 2017 10:45 PM Abandoned
    Monday, October 2, 2017 7:19 PM

All replies

  • The following returns the mode:

    Get-WmiObject Win32_computersystem|select model


    \_(ツ)_/

    Monday, October 2, 2017 7:48 PM
  • The following does not have a model property.

    Get-ADComputer  -Properties * -Filter * |  Select Model

    The command will return multiple objects.  You cannot add a property to an object by using "+"

    To learn PowerShell start here: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276

    Once you learn PowerShell these things will make more sense.

    Also use "AddMonths(-2)" to adjust by month. (90 days is 3 months)


    \_(ツ)_/


    • Edited by jrv Monday, October 2, 2017 7:54 PM
    Monday, October 2, 2017 7:53 PM
  • Example:

    Get-ADComputer -Properties * -Filter { LastLogonDate -gt $cutoffdate } | 
    	Select-Object Name, OperatingSystem, OperatingSystemVersion,
    		LastLogonDate, CanonicalName,
    		@{n='Model';e={(Get-WmiObject Win32_computersystem -ComputerName $_.Name).Model}}
    


    \_(ツ)_/

    Monday, October 2, 2017 7:58 PM
  • Thank you soooo very much for your response. I ran the following but am receiving another error

    Import-Module ActiveDirectory

    # get today's date
    $today = Get-Date

    #Get today - 90 days (3 month old)
    $cutoffdate = $today.AddDays(-90)

    Get-ADComputer -Properties * -Filter { LastLogonDate -gt $cutoffdate } |
     Select-Object Name, OperatingSystem, OperatingSystemVersion,
      LastLogonDate, CanonicalName,
      @{n='Model';e={(Get-WmiObject Win32_computersystem -ComputerName $_.Name).Model}} | Export-Csv .\ListActiveComputers90.csv

    Error:

    Get-ADComputer : The server has returned the following error: invalid enumeration context.
    At C:\Users\MyAccount\Desktop\ListActiveComputers90.ps1:18 char:1
    + Get-ADComputer -Properties * -Filter { LastLogonDate -gt $cutoffdate  ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-ADComputer], ADException
        + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.GetADComputer


    DHeinz

    Monday, October 2, 2017 9:44 PM
  • Sorry.  You cannot use "LastLogonDate" in a filter.  User Where-Object.  Also:

    Search-AdAccount -AccountInactive -TimeSpan 60 -ComputersOnly


    \_(ツ)_/

    Monday, October 2, 2017 9:54 PM
  • I'm sorry JRV but I don't understand :(

    DHeinz

    Tuesday, October 3, 2017 2:29 PM
  • What don't you understand?

    \_(ツ)_/

    Tuesday, October 3, 2017 2:36 PM
  • I modified the script as suggested but no data is returned

    Import-Module ActiveDirectory

    # get today's date

    $today = Get-Date

    #Get today - 90 days (3 month old)

    $cutoffdate = $today.AddDays(-90)

    Get-ADComputer -Properties * -Filter { Where-Object -gt $cutoffdate } |

           Select-Object Name, OperatingSystem, OperatingSystemVersion,

                  LastLogonDate, CanonicalName,

                  @{n='Model';e={(Get-WmiObject Win32_computersystem -ComputerName $_.Name).Model}} | Export-Csv .\ListActiveComputers90.csv


    DHeinz

    Tuesday, October 3, 2017 2:52 PM
  • I think you need to learn basic PowerSHell.

    help where-object -full

    Learn PowerShell: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276<o:p></o:p>



    \_(ツ)_/

    Tuesday, October 3, 2017 4:29 PM
  • So the script is working to a point. It queries AD and gathers the info requested but only captures 250 objects.

    DHeinz

    Tuesday, October 3, 2017 6:19 PM
  • Possible page file size issue I suspect

    DHeinz

    Tuesday, October 3, 2017 7:42 PM
  • So the script is working to a point. It queries AD and gathers the info requested but only captures 250 objects.

    DHeinz


    (Get-ADComputer -Properties * -Filter *).Count

    \_(ツ)_/

    Tuesday, October 3, 2017 10:00 PM