Output showing with curlybrackets @{......}

Welcome Forums General PowerShell Q&A Output showing with curlybrackets @{......}

Viewing 7 reply threads
  • Author
    Posts
    • #179898
      Participant
      Topics: 1
      Replies: 2
      Points: 19
      Rank: Member

      Hi Guys

      Fairly new to PS so the answer maybe pretty straight forward. I have created a script to output to a text file. However rather then just showing the name of an object it displays as follow:

      26/09/2019 14:01 | @{DeviceName=TEST-888888} No Devices Removed

      What i would like it to do is get rid of the @{} so it reads

      26/09/2019 14:01 | TEST-OX888888 No Devices Removed.

      Any input would be appreciated

      Thanks in advance

    • #179913
      Participant
      Topics: 2
      Replies: 1707
      Points: 3,428
      Helping Hand
      Rank: Community Hero

      You will have to show a little more of your code. And please format it as code using the code tag button ("pre") right above the edit window of the post editor.
      Thanks

    • #179922
      Participant
      Topics: 4
      Replies: 84
      Points: 368
      Helping Hand
      Rank: Contributor

      Without seeing any code I can tell that you will need to either assign the value of current objects "DeviceName" property to a variable first. Then return the variable in the code you are sending to Out-File

      Otherwise you can use a Subexpression and tease the value of the DeviceName property out using either "Select-Object -ExpandProperty" or a parenthetical object.

      As a newbie the above advice may not be very helpful for you. That is why Olaf suggested posting your code, it can allow folks to provide relevant examples of the concepts they are explaining.

       

       

    • #179931
      Senior Moderator
      Topics: 8
      Replies: 1155
      Points: 4,019
      Helping Hand
      Rank: Community Hero

      its a PSCustomObject, if your variable is $r, you will print it like

      $r.DeviceName | Out-File -FilePAth $FilePath # using member accessor
      
      "Device name is $($r.DeviceName)" | Out-File -FilePAth $FilePath # using sub expressions
      

      What happened is a string conversion as you have wrapped it in double quotes "something is $r"

    • #180108
      Participant
      Topics: 1
      Replies: 2
      Points: 19
      Rank: Member

      Hi Guys,

      Thanks for your replies, i have copied my code below. Thanks again

      Function LogIt {
      param(
      [Parameter(Mandatory=$true)][string]$LogPath,
      [Parameter(Mandatory=$true)][string]$Message
      )
      if (!(Test-Path $LogPath)) {
      New-Item $LogPath
      }
      $timeStamp = Get-Date -Format "dd/MM/yyyy HH:mm"
      $timeStampedMessage = $timeStamp + " | " + $Message
      Add-Content -Path $LogPath -Value $timeStampedMessage
      
      }
      Function CheckPC{
      param(
      [Parameter(Mandatory=$true)][String]$device
      )
      $counter = 0
      
      if (Get-ADComputer -Identity $device)
      {
      Remove-ADComputer -Identity $device -Confirm:$false
      $counter++
      }
      switch($counter){
      0{$result = LogIt -LogPath "C:\AAA\ $folderdate\compsremove.txt" -Message "$Comp - No Devices Removed"}
      1{$result = LogIt -LogPath "C:\AAA\ $folderdate\compsremove.txt" -Message "$Comp - Removed from AD"}
      }
      return $result
      }
    • #180114
      Participant
      Topics: 2
      Replies: 1707
      Points: 3,428
      Helping Hand
      Rank: Community Hero

      Please fix your post and format the code as code using the code tag button (pre). Thanks.

      Where do you actually define the variable $Comp and $Folderdate you use in your second function?

      • #180117
        Participant
        Topics: 1
        Replies: 2
        Points: 19
        Rank: Member
        $Computers = Import-csv "c:\AAA\test.csv"
        New-Item -itemType Directory -Path "C:\AAA\ $folderdate" -force
        $folderdate = (Get-Date).tostring("dd-MM-yyyy")
        
        foreach ($Comp in $Computers)
        {
        CheckPC -device $Comp.DeviceName
        
        }
    • #180120
      Participant
      Topics: 2
      Replies: 1707
      Points: 3,428
      Helping Hand
      Rank: Community Hero

      OK.
      It's always better to use internal variables in functions instead of global ones. This time it might be a simple situation but when it gets more complex you never know what happens to the "outside" variable. So I'd recommend to use the variable you designed for it – $device.

      If you insist to use your $Comp variable you should do the same you do outside your function – use $Comp.Devicename.

    • #181079
      Participant
      Topics: 10
      Replies: 1385
      Points: 1,527
      Helping Hand
      Rank: Community Hero

      You may want to simplify your approach a bit. Log files are not really powershell-y, consider doing a CSV since you can import\export that easily to filter, analysis and leverage other cmdlets for remediation. Placing plain text in a log is human readable, but cannot be leveraged beyond that.

      Import-Module ActiveDirectory
      
      $Computers = Import-csv "c:\AAA\test.csv"
      
      $results = foreach ($Comp in $Computers | Select -ExpandProperty DeviceName) {
          $adComputer = Get-ADComputer -Filter {Name -eq $comp}
      
          if ( $adComputer ) {
              try {
                  Remove-ADComputer -Identity $adComputer -ErrorAction Stop
                  $status = 'Success'
                  $msg = $null
              }
              catch {
                  $status = 'Failed'
                  $msg = $_
              }
          }
          else {
             $status = "NotFound"
             $msg = $null
      
          }
      
          New-Object -TypeName PSObject -Property @{
              ComputerName = $Comp
              Status       = $status
              Message      = $msg
          }
          
      }
      
      $results
      #Show all of the failed
      $results | Where {$_.Status -eq 'Failed'}
      #Export to a csv
      $results | Export-CSV -Path C:\myresults.csv -NoTypeInformation
      
Viewing 7 reply threads
  • The topic ‘Output showing with curlybrackets @{......}’ is closed to new replies.