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.
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”
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.