none
powershell while else loop, run command if not true RRS feed

  • Question

  • Hi guys. trying to do the following:

    check if file path is true (exists) , if yes, wait 5 second and try again, if not, run a command bat file. For some reason instead of running the bat file (could also be ps or plain command) is just printed out :) The command is not run.

    Script:

    $i =  (Test-Path \\domain.com\test\test\)

    While ($i -eq "true") {

    Start-Sleep -s 5
    get-date
    Test-Path \\domain.com\test\test\}

    {
    D:\tmp\test001.bat
    }

    Thursday, April 16, 2015 7:09 AM

Answers

  • It doesn't need to be in the script block, try it this way - 

    do{$i = Test-Path \\domain.com\test\test\
    
    sleep 5
    get-date
    
    } until ($i -eq $False)
    
    D:\tmp\test001.bat
    
    

    • Marked as answer by niklasrene Thursday, April 16, 2015 8:18 AM
    Thursday, April 16, 2015 7:53 AM

All replies

  • It doesn't need to be in the script block, try it this way - 

    do{$i = Test-Path \\domain.com\test\test\
    
    sleep 5
    get-date
    
    } until ($i -eq $False)
    
    D:\tmp\test001.bat
    
    

    • Marked as answer by niklasrene Thursday, April 16, 2015 8:18 AM
    Thursday, April 16, 2015 7:53 AM
  • grazie :)
    Thursday, April 16, 2015 8:18 AM