none
Checking a File has been exported - Please Help! RRS feed

  • Question

  • Hey, second time posting on a Forum, but need some help with some PS script.

    I am expecting this line to return a 'True' value and display the 'Success' message but it isn't, instead returning the 'Unsuccessfull' message, even though both directories that the Test-Path is looking at contain the required files.

    Where am I going wrong?

    foreach ($server in $xml.config.server.name) {$TestPath=Test-Path "\\$server\D$\PowerShell\*.*" | ForEach-Object { If ($TestPath -eq $true) { Write-Host "PowerShell Scripts Exported to $server Successfully" -ForegroundColor Green} else { Write-Host "`rUnable to Export PowerShell Scripts to $server" -ForegroundColor Red}}}
    Unable to Export PowerShell Scripts to TCPP2B1W01
    Unable to Export PowerShell Scripts to TCPP2B1W02


    Tuesday, October 21, 2014 11:02 AM

Answers

  • #----- THIS ---
    
    foreach($server in $xml.config.server.name){
         if(Test-Path "\\$server\D$\PowerShell\*.*"){
              Write-Host "PowerShell Scripts Exported to $server Successfully" -ForegroundColor Green
         }else{ 
              Write-Host "`rUnable to Export PowerShell Scripts to $server" -ForegroundColor Red
         }
    }
    
    
    #---- OR ---
    
    $xml.config.server.name |
         ForEach-Object{
              $server=$_
              if(Test-Path "\\$server\D$\PowerShell\*.*"){
                   Write-Host "PowerShell Scripts Exported to $server Successfully" -ForegroundColor Green
              }else{ 
                   Write-Host "`rUnable to Export PowerShell Scripts to $server" -ForegroundColor Red
              }
         }
    
    
    


    ¯\_(ツ)_/¯

    • Marked as answer by JimAll Tuesday, October 21, 2014 11:28 AM
    Tuesday, October 21, 2014 11:19 AM

All replies

  • Don't put everything on one line.  It serves no purpose except for making it impossible to read.

    foreach($server in $xml.config.server.name){
         $TestPath=Test-Path "\\$server\D$\PowerShell\*.*" | 
              ForEach-Object{
                 If($TestPath -eq $true){
                     Write-Host "PowerShell Scripts Exported to $server Successfully" -ForegroundColor Green
                 }else{ 
                     Write-Host "`rUnable to Export PowerShell Scripts to $server" -ForegroundColor Red
                 }
              }
    }

    Now look at what you wrote.  It doesn't make much sense.  Why are you testing the same thing twice.  Just do it once.

    If(Test-Path <somepath>){ do this...

    Use either foreach() or ForEach-Object.  Do not use both at the same time.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, October 21, 2014 11:15 AM
    Tuesday, October 21, 2014 11:14 AM
  • #----- THIS ---
    
    foreach($server in $xml.config.server.name){
         if(Test-Path "\\$server\D$\PowerShell\*.*"){
              Write-Host "PowerShell Scripts Exported to $server Successfully" -ForegroundColor Green
         }else{ 
              Write-Host "`rUnable to Export PowerShell Scripts to $server" -ForegroundColor Red
         }
    }
    
    
    #---- OR ---
    
    $xml.config.server.name |
         ForEach-Object{
              $server=$_
              if(Test-Path "\\$server\D$\PowerShell\*.*"){
                   Write-Host "PowerShell Scripts Exported to $server Successfully" -ForegroundColor Green
              }else{ 
                   Write-Host "`rUnable to Export PowerShell Scripts to $server" -ForegroundColor Red
              }
         }
    
    
    


    ¯\_(ツ)_/¯

    • Marked as answer by JimAll Tuesday, October 21, 2014 11:28 AM
    Tuesday, October 21, 2014 11:19 AM
  • Thanks. Quite new to PS so it made sense in my head....But yes, I can see what you are saying now. No need to overcomplicate.
    Tuesday, October 21, 2014 11:31 AM