Remove item from Import-csv collection RRS feed

  • Question

  • Hi

    I'm writing a script to deploy VM's into our environment. I'm going to import-csv that will have information like servername, IP. datastore etc...

    The first thing I'd like to do is check whether the IP is being used and if it is remove that item from the collection.  I know I could just create an exclusion list, but this is going to be a long script and it would be a more graceful solution to modify the list so that it only contains Servers with valid details.

    So I thought it would be something like

    $servers = import-csv c:\build\serverlist.csv

    foreach ($server in $servers){

    if (Test-Connection -quiet -computer $server.servername -count 1){Remove-item from $servers}

    }Any ideas or is it the exclusion list for each foreach loop?



    • Moved by Bill_Stewart Tuesday, November 7, 2017 10:27 PM Abandoned
    Friday, September 29, 2017 10:42 AM

All replies

  • Why do you think you need to remove it from the list?  Just skip it if it exists.


    Friday, September 29, 2017 10:45 AM
  • For the reasons I gave in the original post.

    It's going to be a long script with a lot of loops to perform various pieces of configuration. Rather than have the exclusion on each loop it seems more graceful to me to create a list of only valid servers.

    It's an aesthetic thing really. If it can't be done it's no biggie, but I always think it's better to do things a in a simple tidy way if possible.

    Friday, September 29, 2017 10:50 AM

  • $servers = import-csv c:\build\serverlist.csv | Where{ -not (Test-Connection $_.servername -quiet)}


    Friday, September 29, 2017 10:54 AM
  • That is beautiful :-)

    Thanks JRV

    Friday, September 29, 2017 12:11 PM
  • That is beautiful :-)

    Thanks JRV

    No.  That is how PowerShell works and many other things once you learn PowerShell.


    Friday, September 29, 2017 12:15 PM