New PSObject - Cant return one of the objects

Welcome Forums General PowerShell Q&A New PSObject - Cant return one of the objects

Viewing 3 reply threads
  • Author
    Posts
    • #190240
      Participant
      Topics: 1
      Replies: 1
      Points: 18
      Rank: Member

      I am struggling with trying to create output.  I am iterating through the c:\users folder and pulling the last user logged in, I am then using get-printer to find a specifically named Printer.

      In my output I want to return the following:

      UserName (from above iteration)
      Computer Name
      Printer Name
      Printer IP

      I created a new PSObject for the above fields, all of them will populate
      except for username.

      Here is the snippet of my code.
      Sorry, i didnt see the "insert code" option anywhere...

      foreach ($computer in $computers)
         {
      if (test-path \\$computer\c$)
             {
      $users = (get-childitem \\$computer\c$\users).name

                             $user = $users | select-object -last 1
                         $NewObject = New-Object PSObject -Property @{
      'UserName' = $user

                                 'ComputerName' = $computer
                                 'Name' = $name
                                'IPAddress' = $Portname

                         }

      get-printer -cn $computer -name 'Xerox_Eval' | Select-Object UserName, ComputerName, Name, Portname

      SAMPLE OUTPUT
      UserName   ComputerName   Name     PortName
      (blank)         ABC123XYZ        XEROX    192.168.1.100

      I get no output from the Username object!!!
      I also tried to separate the Username object from the others, and
      then somehow append them together, but this did not work either.

      Thanks!

       

       

    • #190390
      Participant
      Topics: 10
      Replies: 116
      Points: 451
      Helping Hand
      Rank: Contributor

      The reason UserName is empty is because UserName is not a property that exists in the Get-Printer object, hence Select-Object returns nothing for it in the output. The only thing Get-Printer is leveraging from your code is the $computer name because you're still within the foreach loop. You can add UserName as a custom property in the Get-Printer output per the code below.

      Just to confirm, are you aware that your code is set to get the last user (name) folder alphabetically on a given computer? Is this the intent? Please help us better understand more context of what you're needing to do; sometimes providing more code helps too.

      To format your code, select your code and change from Paragraph to Preformatted (left of bold).

      In the meantime, you could try a couple of approaches:

      foreach ($computer in $computers)
      
          $basePath = "\\$computer\C$\"
      
          if (Test-Path -Path $basePath) {
              $usersPath          = Join-Path -Path $basePath -ChildPath Users
              $lastUserFolderName = (Get-ChildItem -Path $usersPath -Directory | Select-Object -Last 1).Name
      
              Get-Printer -ComputerName $computer -Name 'Xerox_Eval' |
                  Select-Object -Property @{ Name = 'UserName'; Expression = $lastUserFolderName }},
                      ComputerName, Name, PortName
          }#if
      }#foreach

      or use Invoke-Command:

      $computers = computers.txt
      # Could also use CimSession
      
      Invoke-Command -ComputerName $computers -ScriptBlock {
          $printerName = 'Xerox_Eval'
          $userName    = (Get-ChildItem -Path 'C:\Users' -Directory | Select-Object -Last 1).Name
      
              Get-Printer -Name $printerName |
                  Select-Object -Property @{ Name = 'UserName'; Expression = { $userName }},
                      @{ Name = 'ComputerName'; Expression = { $env:COMPUTERNAME }},
                      Name, PortName
      } | Select-Object -Property UserName, ComputerName, Name, PortName
    • #190411
      Participant
      Topics: 1
      Replies: 1
      Points: 18
      Rank: Member

      Thanks, for you knowledgeable replies.   I used the part of your code that was supposed to "inject" the Username data into the Output, but I am getting
      the same results.  (Username = null)

      Basically, the data is coming in from two different pipelines ($users from the gci c:\users\ and the second set of data is coming from
      the pipeline Get-Printer.....I am going to have to come up with something else.  This has been a head-banger to say the least.

      Thanks again!

       

       

    • #190435
      Participant
      Topics: 10
      Replies: 116
      Points: 451
      Helping Hand
      Rank: Contributor

      Perhaps if you could clearly explain what the business requirements are (rather than using code to do so), it may be quite simple to pull off. For example, what's the correlation between each of the users and a printer's name, IP, and port on different computers? Don't give up, we're here to help 🙂

Viewing 3 reply threads
  • You must be logged in to reply to this topic.