Announcement

Collapse
No announcement yet.

Check CSV for hostname problem

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Check CSV for hostname problem

    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?

  • #2
    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
    }
    Last edited by RicklesP; 3rd February 2017, 21:05.
    *RicklesP*
    MSCA (2003/XP), Security+, CCNA

    ** Remember: credit where credit is due, and reputation points as appropriate **

    Comment


    • #3
      I was able to get it to work with this format

      $hostname = hostname
      $csv = "c:\user data\test.Csv"
      $check = (Import-Csv $csv)."Computername"
      $check -contains "$hostname"
      $check2 = $check -contains "$hostname"

      Comment


      • #4
        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.
        *RicklesP*
        MSCA (2003/XP), Security+, CCNA

        ** Remember: credit where credit is due, and reputation points as appropriate **

        Comment


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

          Comment

          Working...
          X