0

I am working on a script which takes Hostnames from a CSV, Files them against Get-ADComputer and then saves certain Objects in certain columns of the original CSV.

While the solution seems like a basic Task (code below), my problem is, that the Output of Get-ADComputer always (you can see I played around with Out-String but also tried other formatting options) contains a lot of NewLine characters or other formatting issues which make the CSV confusing.

Clear-Host
Import-Module ActiveDirectory
$import = Import-Csv 'XXX' -Delimiter ';'


Foreach ($row in $import){
    $hostname = $row.HOSTNAME
    if($hostname.length -gt 3){
        $computer = Get-ADComputer -Filter {Name -like $hostname} -Properties LastLogonDate, LastLogonTimeStamp
        $row.AD_LastLogon.ToString() = $computer | Select-Object LastLogonDate | Select-Object -first 1 | FT -HideTableHeaders | Out-String
        $row.AD_LLTimestamp = $computer | Select LastLogonTimestamp |Select-Object -first 1 | FT -HideTableHeaders | Out-String
    }


}
$import | Export-Csv 'XXX' -Delimiter ';' -NoType

My question now is, if anyone could help with a method to get the bare string result of for example Get-ADComputer's LastLogonDate, without any formatting or headers included.

Thanks in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
phowner
  • 11
  • 2

1 Answers1

2

Use the -ExpandProperty parameter of Select-Object to extract just the parameter you want. You can only specify one parameter to exapand at a time.

$row.AD_LastLogon = $computer | Select-Object -ExpandProperty LastLogonDate -First 1 | Out-String
$row.AD_LLTimestamp = $computer | Select-Object -ExpandProperty LastLogonTimestamp -First 1

I don't believe the -First 1 should be necessary. Get-ADComputer shouldn't be finding multiple computers with the same name.

Also, you shouldn't need to retrieve both LastLogonDate and LastLogonTimestamp. The former is the same value as the latter, just converted to a DateTime from the irritating NT Time Epoch that LastLogonTimestamp uses. Have you got a system that requires both?

Finally, just a note but this:

$row.AD_LastLogon.ToString() = $computer | [...]

It doesn't make sense. You can't assign a value to a method. I would be surprised if that didn't error or otherwise do nothing at all.

Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • The .ToString() as well as the -First 1, Out-String, made it to this post since i copied it together from multiple Versions i tried. Yet again, this wasn't ment to be a working script. – phowner Aug 28 '17 at 15:08
  • The Select-Property Option did some help, i know have an output with only one New-Line before and after the TimeStamp, instead of Multiple. And of course I know what a Date and an Unix-Timestamp is, the Program for which the export is for, needs both – phowner Aug 28 '17 at 15:10
  • Any Ideas for getting Just the String, without any new lines? – phowner Aug 28 '17 at 15:10
  • Try trimming the string to remove whitespace characters (including newlines): `$computer | Select-Object -ExpandProperty LastLogonTimestamp -First 1 | ForEach-Object { $_.Trim() }` – Bacon Bits Aug 28 '17 at 15:57
  • *shouldn't* find any duplicate computernames, we all know how that goes, however – Maximilian Burszley Aug 28 '17 at 16:04
  • @TheIncorrigible1 Yeah, but this is "shouldn't" as in "Active Directory defines those properties as singletons, and computer names are required to be unique across a domain." So, unless he's got child domains or is otherwise searching a forest, they really should be single values. And if they've got child domains, then the data being returned is potentially invalid (i.e., not for the account they think it is). If you've got multiples, you don't want to handle it by just keeping the first one. – Bacon Bits Aug 28 '17 at 16:22