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

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

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

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.

 

 

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”

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
}

Is you issue fixed with above replies ?

please use code formatting options to format the code in the forums.
https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did/

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?

$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

}

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.

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