locked
Advice on where script has gone wrong RRS feed

  • Question

  • Hi all,

    I have the script below which is intended to do the following

    Ping a client taken from a list. If the client responds, search for a folder and report whether it exists or not to a log file. If the client does not respond, log that also to the file also. It its current state it reads the host.csv and finishes almost instantly having fiilled the log with (clientname) was switched off. I'm guessing i've got a curly brace in the wrong place. I'ce tried a few things and it didnt make it work. I have very basic skills as you can see, please can someone advise me how to fix/improve.

    Thanks

    James

    $computername = Get-Content -Path C:\test\hosts.csv
    $test = Test-connection $computer -Count 1 -quiet
    $folderpath = "\\$computer\c$\program files\lightspeed systems\lma"
    foreach ($computer in $computername) {
    #1. check if respnding
    if ($test -eq "True")
    {
     #1.1 Check path if pc responds
     Test-Path -Path $folderpath
     if (Test-Path -Path $folderpath -IsValid)
      #1.3 if it is then do this
     {Add-Content "$folder path was found" -path C:\test\log.txt}
      else {Add-Content "$folder path was NOT found" -path c:\test\log.txt}
      }
     else {Add-Content "$computer was switched off" -path c:\test\log.txt}
     }
    

    Wednesday, December 6, 2017 11:29 AM

All replies

  • help import-csv -online

    You cannot use Get-Content on a CSV file.


    \_(ツ)_/

    Wednesday, December 6, 2017 11:48 AM
  • Thanks jrv.

    I  have now ammended the code to the below, however the log still fills up with this @{ADMIN-01=ADMIN-02} was switched off. Its reading the csv as its got the correct client names. Any ideas? Thanks

    Clear-Host
    $filepath = "C:\test\hosts.csv"
    $computername = Import-Csv $filepath
    $folderpath = "\\$computer\c$\program files\lightspeed systems\lma"
    foreach ($computer in $computername) {
    #1. check if respnding
    $test = Test-connection $computer -Count 1 -quiet
    if ($test -eq "True")
    {
     #1.1 Check path if pc responds
     Test-Path -Path $folderpath
     if (Test-Path -Path $folderpath -IsValid)
      #1.3 if it is then do this
     {Add-Content "$computer Path was found" -path C:\test\log.txt}
      else {Add-Content "$computer Path was NOT found" -path c:\test\log.txt}
      }
     else {Add-Content "$computer was switched off" -path c:\test\log.txt}
     }
    

    Wednesday, December 6, 2017 12:59 PM
  • You cannot test a Boolean against a string.  You are testing too many times.  Just test once.

    Start here to learn basic PowerShell.

    https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276

    You are just copying things with no understanding of what you are copying or why.


    \_(ツ)_/


    • Edited by jrv Wednesday, December 6, 2017 1:07 PM
    Wednesday, December 6, 2017 1:06 PM
  • Thats the exact reason why I am asking. I said in my original post that I was basic, so thank you for pointing it out to me again. It did work before when I had just a single client specified but ever since I added in the taking clients from a file it went wrong. I wish I had the time to sit there and learn it all but I dont. If anyone can tell me where I've gone wrong and suggest where to change it would be appreciated.
    Wednesday, December 6, 2017 1:43 PM
  • Start over and think about what you are doing.  You still haven't read about CSV files and how to use them.

    Almost every line in your code is wrong.  Go to the tutorial and learn PowerShell. We do not do custom rewrites of scripts.  We do not provide custom individual training.  That is beyond the scope of all technical forums.


    \_(ツ)_/

    Wednesday, December 6, 2017 1:54 PM
  • These lines are just stupid:

    $test = Test-connection $computer -Count 1 -quiet
    if ($test -eq "True")
    {

    This is how we code  this:

    if(Test-connection $computer -Count 1 -quiet){

    the "Test-" CmdLets are designed to work this way.  That is why I harp on taking the time to actually learn PowerShell from a valid source.  Copying code you have seen on the net will not teach you PowerShell.

    It is also necessary that you know how to think logically.  Programming is not "arts and crafts"  It is science/engineering.

    Again!!!!  A CSV requires a property name.


    \_(ツ)_/

    Wednesday, December 6, 2017 2:04 PM
  • Thank you both for your answers, the rejigged script works perfectly. I also got rid of the csv and worked from a txt file instead, dont really know what possessed me to use the csv (as you said jrv) in the first place for what I needed. Your help is greatly appreciated!

    Wednesday, December 6, 2017 2:56 PM
  • Clear-Host
    $computername = get-content file.txt
    foreach ($computer in $computername)
    {
    $folderpath = "\\$computer\c$\program files\lightspeed systems\lma"
    if(Test-connection $computer -Count 1 -quiet)
     {
     $Path = Test-Path -Path $folderpath
     if ($Path -eq "True")
       {
       Add-Content "$computer Path was found" -path C:\test\log.txt
       }
      else
       {
       Add-Content "$computer Path was NOT found" -path c:\test\log.txt
       }
     }
    else
     {
      Add-Content "$computer was switched off" -path c:\test\log.txt
     }
    }

    GD


    • Proposed as answer by Gudakesh (GD) Wednesday, December 6, 2017 3:13 PM
    • Edited by Gudakesh (GD) Wednesday, December 6, 2017 3:14 PM
    Wednesday, December 6, 2017 3:13 PM