Hostname Learning

Welcome Forums General PowerShell Q&A Hostname Learning

This topic contains 4 replies, has 4 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #105149

    Participant
    Points: 1
    Rank: Member

    Why when I query something like a csv or use get-content on a text file with a list of hostnames does the subsequent command choke on headers and such?

    Example:
    I wanted to run something like #invoke-command -computername (Get-content C:\somefile.txt) -scriptblock {get-process}

    Instead I had to take the variable that I had make "somefile.txt" with and do the following top it

    foreach($i in $pclist) {$tryme = $tryme + $i.Hostname}

    Now #invoke-command -computername $tryme -scriptblock {get-process} is working fine

    What's up with that?

  • #105155

    Participant
    Points: 144
    Helping Hand
    Rank: Participant

    I'd say it depends pretty much on what's in your C:\somefile.txt. When I try

    invoke-command -computername (Get-content C:\somefile.txt) -scriptblock {get-process}

    it works just as expected.

  • #105158

    Participant
    Points: 0
    Rank: Member

    Any chance you could share how the data in your textfile is formatted?
    Like Olaf says it may have more to do with the data than the invoke command.

  • #105176

    Keymaster
    Points: 1,625
    Helping HandTeam Member
    Rank: Community Hero

    If you're using Import-CSV to read a CSV, it'll handle the headers for you.

    If not, it's easier to use something like...

    Get-Content data.txt | Select -Skip 1

    To skip the first line.

  • #105523

    Participant
    Points: 1
    Rank: Member

    I have found that if I run something like

    I typically work in an environments between 30-100 workstations per domain.

    Get-ADComputer -filter * | Out-file C:\something.txt

    I then need to strip out (or skip, forgot about that – thanks Don!) the top two lines or export it as a csv.

    This certainly isn't show stopped just an oddity I have noticed.

    I'll try to make a sample file and post it later!

    Thank-you for taking the time to look at this, everybody!

The topic ‘Hostname Learning’ is closed to new replies.