none
Get ( Type *.txt ) and show the text file contents (a Version #) with the computer name. RRS feed

  • Question

  • I have tried 

    1.

    $Computers="\\xxxxx\S$\PowerShellScripts\LR_LAB\xxxxxx.txt
    $computername=$env:computername


    Get-Content $Computers | Foreach-Object {type C:\xxxx\xxxx\txt $computername}

    2.

    $Computers="\\xxxxxx\S$\PowerShellScripts\xxxx\xxxxx.txt"
    $computername=$env:computername


    Foreach (Get-Content $Computers {type C:\xxxxx\xxxxx.txt -computername $Computers | Select @{name="Server";Expression=$server}}),name,version 

    Only a variation of the second one there I got the type command to roll and return the data of the txt file, but no computer name. I'll be having a couple hundred computers returning data and can't seem to get this to work together.

    Also I need to return whatever txt file data is in the folder because originally the way we kept track was just the file name not in the contents, but I haven't seen a way to return the file name. Maybe that would be easier? So to be detailed the version info I need is currently 22_4.txt, I just need

    22_4 - Computename

    on any outfile. I was going to overwrite the .txt with content being the version number because I already knew the type command in cmd prompt and it worked also in powershell.

    • Edited by Jason Amick Monday, August 1, 2016 8:53 PM more details
    Monday, August 1, 2016 8:47 PM

Answers

  • Here's an example of how to read a list of computer names and get the content of a text file on each computer:

    Get-Content "ListOfComputerNames.txt" | ForEach-Object {
      $computerName = $_
      Get-Content "\\$computerName\C$\Temp\test.txt" | Select-Object `
        @{"Name" = "ComputerName"; Expression={$computerName}},
        @{"Name" = "Line";         Expression={$_}}
    }


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Jason Amick Monday, August 1, 2016 9:09 PM
    Monday, August 1, 2016 8:59 PM
    Moderator

All replies

  • Here's an example of how to read a list of computer names and get the content of a text file on each computer:

    Get-Content "ListOfComputerNames.txt" | ForEach-Object {
      $computerName = $_
      Get-Content "\\$computerName\C$\Temp\test.txt" | Select-Object `
        @{"Name" = "ComputerName"; Expression={$computerName}},
        @{"Name" = "Line";         Expression={$_}}
    }


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Jason Amick Monday, August 1, 2016 9:09 PM
    Monday, August 1, 2016 8:59 PM
    Moderator
  • Thanks so MUCH!!!!!!!!!!!

    I have banged my head with my limited knowledge on piecing these commands together for some time today.

    Monday, August 1, 2016 9:24 PM
  • ok so I have used your template and sending to an outfile txt doc. It is working great.

    I wanted to add a if error write error instead of version but what I got was it writing it in the shell not my txt.

    So again to be detailed as I can I get 

    comp1  22.7

    comp2  22.7

    comp4  22.7

    It skipped 3 because either the file I'm reading did not exist, no content in the file or the computer was offline.

    I'd like to see 

    comp1 22.7

    comp2 error

    comp3  22.7

    Tuesday, August 2, 2016 8:54 PM
  • Fortunately I was able to (sort of) understand your last question.

    I am not able to understand your latest post at at all; sorry.

    Please start a new question and try to explain what you are trying to do. You will need to explain as if the person to whom you are explaining it cannot read your mind nor see your screen. (Hint: We can do neither.)


    -- Bill Stewart [Bill_Stewart]

    Tuesday, August 2, 2016 8:58 PM
    Moderator
  • I tried adding this 

    If ((Test-Path $computername) –eq $false) { 
    Write-Output "The file $computername is offline" 
      break 
    }

     but am getting a error

    At \\XXXXXXXXXXXXXXXXXXXXXXXXXXXXX.ps1:7 char:3
    + If ((Test-Path $computername) –eq $false) {
    +   ~
    Missing '=' operator after key in hash literal.
    At \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.ps1:1 char:80
    + ...  "\\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\*.txt" | ForEach-Object {
    +                                                                         ~
    Missing closing '}' in statement block or type definition.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingEqualsInHashLiteral

    Tuesday, August 2, 2016 9:00 PM
  • You have a mismatched curly brace. That's what "Missing closing '}' in statement block" means.

    Edit your code in the ISE to spot errors like this.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, August 2, 2016 9:08 PM
    Moderator
  • I am trying to take a list of computers and a txt file- (contents = version number) read the contents of the txtfile and make a outfile that equals two columns computername  and version.

    The script you suggested yesterday worked flawlessly. except if a computer is offline or the file does not exist the outfile just skips that- looking at a hundred + computers I dont want to count them all I would like to see an error message under the version tab.

    so here with any addresses xxxxx'd is what I most recently tried

         

    Get-Content "\\ms-dfs\s$\powershellscripts\LabMachines\*.txt" | ForEach-Object {
      $computerName = $_

      Get-Content "\\$computerName\C$\xxxxxxx\*.txt" | Select-Object `

        @{"Name" = "ComputerName"; Expression={$computerName}},
        @{"Name" = "Version";         Expression={$_}

    If ((Test-Path $computername) –eq $false)  
    Write-Output  "$computername  offline" 

     }


    } | out-file \\localhost\c$\Users\Public\Desktop\xxxxxxxxx.txt

    Tuesday, August 2, 2016 9:13 PM
  • Ahh ok, I found that, facepalm.....

    So I corrected it to this,

    Get-Content "\\xxxxxxxxxxxxxxxxxxxxxx*.txt" | ForEach-Object {
      $computerName = $_
      Get-Content "\\$computerName\C$\xxxx\*.txt" | Select-Object `
        @{"Name" = "ComputerName"; Expression={$computerName}},
        @{"Name" = "Version";         Expression={$_}}

    {If ((Test-Path $computername) –eq $false)  
    Write-Output  "The file $computername offline"}


    } | out-file \\localhost\c$\Users\Public\Desktop\xxxxxxxx.txt

    The error now is missing statement block after false. 

    But I thought Write-output was the next part of that statement? I got this from

    https://technet.microsoft.com/en-us/magazine/2009.07.windowspowershell

    Tuesday, August 2, 2016 9:24 PM
  • Oh Wait I added 2 curlys one after false and one after offline and lost those errors

    but this is my txt outfile 

    ComputerName Version
    ------------ -------
    AR-LAB-00    22.7   
    If ((Test-Path $computername) –eq $false)  {
    Write-Output  "The file $computername offline"}
    AR-LAB-01    22.7   
    If ((Test-Path $computername) –eq $false)  {
    Write-Output  "The file $computername offline"}
    AR-LAB-02    22.7   
    If ((Test-Path $computername) –eq $false)  {
    Write-Output  "The file $computername offline"}
    AR-LAB-03    22.7   
    If ((Test-Path $computername) –eq $false)  {
    Write-Output  "The file $computername offline"}
    AR-LAB-04    22.7   
    If ((Test-Path $computername) –eq $false)  {
    Write-Output  "The file $computername offline"}
    AR-LAB-05    22.7   
    If ((Test-Path $computername) –eq $false)  {
    Write-Output  "The file $computername offline"}
    AR-LAB-06    22.7   
    If ((Test-Path $comput

    Tuesday, August 2, 2016 9:26 PM
  • I made the if statement 

    If ((Test-Path $computername) –eq $false)  {
    Write-Output  "The file $computername offline"}

    and now I get

    ComputerName Version
    ------------ -------
    AR-LAB-00    22.7   
    The file AR-LAB-00 offline
    AR-LAB-01    22.7   
    The file AR-LAB-01 offline
    AR-LAB-02    22.7   
    The file AR-LAB-02 offline
    AR-LAB-03    22.7   
    The file AR-LAB-03 offline
    AR-LAB-04    22.7   
    The file AR-LAB-04 offline
    AR-LAB-05    22.7   
    The file AR-LAB-05 offline
    AR-LAB-06    22.7   
    The file AR-LAB-06 offline
    AR-LAB-07    22.7   
    The file AR-LAB-07 offline
    AR-LAB-08    22.7   
    The file AR-LAB-08 offline
    AR-LAB-09    22.7   
    The file AR-LAB-09 offline
    AR-LAB-10    22.7   
    The file AR-LAB-10 offline
    AR-LAB-11    22.7   
    The file AR-LAB-11 offline
    AR-LAB-12    22.7   
    The file AR-LAB-12 offline
    The file AR-LAB-13 offline

    I so don't know what I am doing here. :(

    Tuesday, August 2, 2016 9:29 PM
  • Go ahead and start a new question, but keep in mind this forum is really for specific questions rather than "fix my script for me" requests.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, August 2, 2016 9:36 PM
    Moderator