none
Read textfile entry in which computername and put it in variable? RRS feed

  • Question

  • Hi,

    We have a textfile for each username on a share. This records the computer to which the user logged on. It contains a entry per date: username, pcname, date, time, mac and ip address.

    So f.e. \\share\usernames\user1 user2 etc

    user1.txt  contains

    user1 computer1 09/01/'15 mac: ip:
    user1 computer1 09/02/'15 mac: ip:
    user1 computer1 09/03/'15 mac: ip:

    What I like to do is read the textfile for user1 and then detect which computername it has so I can execute a command to $computernameofuser.

    Please advise.
    J.


    Jan Hoedt

    Friday, September 18, 2015 10:07 AM

Answers

  • $str = 'Logon : muyser computername date'
    $share = '\\server\shareonwhichlogfiles\'
    
    $path = Join-Path -Path $share -ChildPath "$(($str -split ' ')[2]).txt"
    $computerName = ($str -split ' ')[3]
    
    $path
    $computerName


    • Marked as answer by janhoedt Wednesday, September 23, 2015 10:07 AM
    Tuesday, September 22, 2015 12:24 PM

All replies

  • Split lines on the space and take first two elements of the array.


    \_(ツ)_/

    • Proposed as answer by Mike Laughlin Friday, September 18, 2015 11:44 AM
    Friday, September 18, 2015 10:24 AM
  • Thanks but that doesn't work for me. I probably was not clear.

    This is what I'm looking for:
    $user = "myuser"
    $share  = "\\server\shareonwhichlogfiles\"

    Then  $logfile should be $share + $user + txt or
    \\server\shareonwhichlogfiles\myuser.txt (didn't make that one work yet).

    Get-Content $logfile | select -Last 1 | Select-String

    This command shows:
    Logon : muyser computername date

    I then would like to filter the computername of this line so I would like to have the content of what is behind the 3rd and before the 4th space.


    Please advise.
    J.


    Jan Hoedt

    Monday, September 21, 2015 11:56 AM
  • $str = 'Logon : muyser computername date'
    $share = '\\server\shareonwhichlogfiles\'
    
    $path = Join-Path -Path $share -ChildPath "$(($str -split ' ')[2]).txt"
    $computerName = ($str -split ' ')[3]
    
    $path
    $computerName


    • Marked as answer by janhoedt Wednesday, September 23, 2015 10:07 AM
    Tuesday, September 22, 2015 12:24 PM
  • works perfectly, thanks!

    function get-pcname
        {
         Param(
            [string]$user)
        $share  = "\\server\share"
        $path = Join-Path -Path $share -ChildPath "$user.txt"
        $LastLine = Get-Content $path | Select-Object -last 1
        $ComputerName = ($LastLine -split ' ')[4]
        write-host $ComputerName
        }


    Jan Hoedt

    Wednesday, September 23, 2015 10:06 AM
  • Cheers, you're welcome.

    Wednesday, September 23, 2015 11:44 AM