Check CSV for hostname problem

Home Forums Scripting PowerShell Check CSV for hostname problem

This topic contains 4 replies, has 3 voices, and was last updated by Avatar weirdo 2 years, 8 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • Avatar
    jason0923
    Participant
    #166835

    I’m obviously not a powershell expert so I’m wondering if there is a way to do this. I have a CSV with thousands of PC names. I want to run a script against if from the various computers so if that PC’s hostename is in the then Do something so somthing to the extent of

    Import-Csv C:test.Csv
    If hostname is in CSV then (for example reboot)

    I can import the CSV but not sure the syntax for something like if hostname is there =true

    Is this possible?

    Avatar
    Anonymous
    #372011

    Assuming one of the columns of your CSV is ‘Name’ for the names of the PCs, set up a loop to read thru each line and compare against the hostname the file is being processed on. When you import, put that in an array object, then read thru it in a ForEach loop:
    $BigList = import-csv c:test.csv
    ForEach ($PC in $BigList)
    {
    If $PC.Name = $env:computername
    restart-computer
    }

    Avatar
    weirdo
    Member
    #390569

    I was able to get it to work with this format

    $hostname = hostname
    $csv = “c:user datatest.Csv”
    $check = (Import-Csv $csv).”Computername”
    $check -contains “$hostname”
    $check2 = $check -contains “$hostname”

    Avatar
    Anonymous
    #372012

    Not sure why you have to have $check and $check2, but if it does what you want, more power to ya! Thanks for posting your solution.

    Avatar
    weirdo
    Member
    #390571

    Yea I saw that later and removed the duplicate, I’m still learning Powershell just trying to get things to work.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.