Select-Object selecting multiple objects but splitting the results and trimming them export to CSV

六月ゝ 毕业季﹏ 提交于 2019-11-27 08:42:23

问题


I have a script that reads from a list of computers, does a test connection. If it gets a reply from the Test-Connection, it does a if/else and builds a variable from a Get-ChildItem that reads from the suspect system C:\Users to see the last person to log in and select the name and last write time of the last user that logged in, and does a Write-Host of the results. If the Test-Conneciton didn't get a reply, it builds a variable of the system name with a "offline" statement and does a Write-Host to show it. Then, to finish it all off, it exports the results to a CSV.

Several things I am having issues with:

  • The variable for the Get-ChildItem does a Select-Object Name, LastWriteTime and builds the results into a variable. I would like to display those results (and of course export them into the CSV) separately instead of combined.
  • I need to trim the results or modify the script so it doesn't include extra characters in the results.

Here is an example of the results from the Get-ChildItem when the Test-Connection gets a reply from the machine (of course system name, IP address and user name have been changed to protect the innocent):

SYSTEMNAME @{IPV4Address=192.168.0.1} @{Name=John.Doe; LastWriteTime=03/08/2017 08:11:48}

The name and last write time are combined in the results and I need to split them out in the display and the CSV export as well as trim the extra characters. Something like this, where systemname, IP address, name and lastwritetime would all appear in their own cells in the CSV:

SYSTEMNAME 192.168.0.1 Name=John.Doe  LastWriteTime=03/08/2017 08:11:48

Code below:

$computerList = Get-Content "D:\filelocation\LastLogIn.txt"
foreach ($Computername in $computerList) {
  $ipreachable = Test-Connection $computerName -EA SilentlyContinue -Count 1 |
                 select IPV4address
  $output =@()
  if ($ipreachable) {
    $LastUserLoggedIn = Get-ChildItem "\\$computername\c$\Users" -EA SilentlyContinue |
                        Sort-Object LastWriteTime -Descending |
                        Select-Object Name, LastWriteTime -First 1
    $Details = "$LastUserLoggedIn"
    Write-Host $computername $ipreachable $Details
  } else {
    $details = "$computerName Computer does not exisit or is offline"
    Write-Host $Details
  } 
  [PSCustomObject]@{
    SystemName = $Computername
    IPV4Address = $ipreachable
    UserLogInDetails = $details
  } | Export-Csv "D:\filelocation\lastuserreults.csv" -NoType -Append
}

回答1:


Expand the IP address:

... | Select-Object -Expand IPV4Address | Select-Object -Expand IPAddressToString

and don't turn the directory information into a string:

$Details = "$LastUserLoggedIn"

Build your data object like this:

$obj = [PSCustomObject]@{
  SystemName  = $Computername
  IPV4Address = $ipreachable
  UserName    = $LastUserLoggedIn.Name
  Timestamp   = $LastUserLoggedIn.LastWriteTime
}

So that you can display the information on the console as well as export it to a CSV:

$obj | Format-Table -AutoSize
$obj | Export-Csv 'C:\path\to\your.csv' -NoType -Append


来源:https://stackoverflow.com/questions/42673525/select-object-selecting-multiple-objects-but-splitting-the-results-and-trimming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!