Local User Account Report help

This topic contains 3 replies, has 3 voices, and was last updated by  Dr3w 3 months, 3 weeks ago.

  • Author
    Posts
  • #73648

    Dr3w
    Participant

    Hi All,

    Working on a script to report the local user account information from a system and display them in a HTML report or CSV.

    I'm studying For-Each loops and want to loop through my Various Commands and append them to a table.

    Command 1) Hostname

    Command 2) (net localgroup administrators).where({$_ -match '-{79}'},'skipuntil') -notmatch '-{79}|The command completed'

    Command 3) Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | Select name -expandproperty name

    Command 4) Get-ChildItem -Path C:\Users\ | select-object Name

  • Q1. Is anyone able to add my commands to an example for me to play with?
  • Q2. I have to run this against computers (200+) within a domain. There are only a few systems with WSMan enabled, I will need to look at that on a separate note. If WS Man was enabled across all systems, would invoke-command would be the best option?
  • As my script stands, I can Enter-PSSession and manually RUN the script against a remote PC, but I want to do this against ALL my computers.

    $A = hostname

    $B = (net localgroup administrators).where({$_ -match '-{79}'},'skipuntil') -notmatch '-{79}|The command completed'

    $C = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | Select name -expandproperty name

    $D = Get-ChildItem -Path C:\Users\ | select-object Name

    $out = new-object psobject
    $out | add-member noteproperty Node $a
    $out | add-member noteproperty Administrators $b
    $out | add-member noteproperty LocalAccounts $c
    $out | add-member noteproperty Profiles $d
    write-output $outOutput

  • #73651

    Chris
    Participant

    Hi Drew,

    Whenever i am looking to grab information from several systems, i usually do it one of 2 ways, either i create a csv,txt file with the list of systems i want to check against, i then load that information into a variable and run it through a Foreach loop with the code you want in the middle.

    The other way, is still using a foreach, but harcode the Computers into a variable, then run the loop against this. I find that CSV/Txt is easier for me personally.

    Hope that gives you an idea,
    Chris

  • #73729

    RShambo
    Participant
    $computername = "Computer1","Computer2"
    
    $opt = New-CimSessionOption -Protocol Dcom
    $session = New-CimSession -SessionOption $opt -ComputerName $ComputerName 
     
    $profile = Get-CimInstance -CimSession $session -ClassName Win32_userprofile -ErrorAction SilentlyContinue
    

    I like using CIM sessions when working with WMI. The computers can be a list like above or you can use a Get-ADGroupMember or any number of options to populate this variable. Using the protocol DCOM will allow you to connect to computers that do not have the most current PowerShell installed.

    CIM Sessions are made concurrently so the collection of the profile data is quicker. If you run Get-CIMSession after the third line you will see a listing of the sessions. Plenty of information on the web on this – I think Don Jones has a few great videos on YouTube or the PowerShell videos on Microsoft Video Academy (free).

    All of that data gets stored into the $profile variable. From there you can pull out what you need.

    Hope this helps.

    Good Luck!

    • #74032

      Dr3w
      Participant

      @rshambo .. thank you – this pointed me in the right direction. I have split my script into two:

      Local User Report – SCRIPT #1

      $computers = get-content "onlinecomputerreport_29_06.txt"
      $computers | foreach {
      $computername = $_
      [ADSI]$S = "WinNT://$computername"
      $S.children.where({$_.class -eq 'group'}) |
      Select @{Name="Computername";Expression={$_.Parent.split("/")[-1] }},
      @{Name="Name";Expression={$_.name.value}},
      @{Name="Members";Expression={
      [ADSI]$group = "$($_.Parent)/$($_.Name),group"
      $members = $Group.psbase.Invoke("Members")
      ($members | ForEach-Object {
      $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
      }) -join ";"
      }}
      } | Export-CSV -path "Report.csv" –notypeinformation

      Get User Directories – SCRIPT #2

      $computers = get-content "test.txt"
      $localusers = foreach ($computer in $computers) {
      Get-ChildItem "\\$computer\C$\Users\*" | Select-Object -Property @{Name="Computers"; Expression = {$computer}}, FullName}
      $localusers | ConvertTo-Html -Property Computers, FullName -CssUri "Style.css" | Out-File "Report.html"
      #-ExpandProperty FullName broke script (removed)

  • You must be logged in to reply to this topic.