I have a deployment part of a PowerShell 2.0 script that copies a potential robots.dev.txt file to a robots.txt file, if it does not exist, do nothing.
My source code:
$RobotFilesToOverWrite= Get-ChildItem -Path $tempExtractionDirectory -Recurse -Include "robots.$Environment.txt" foreach($file in $RobotFilesToOverWrite) { $origin=$file $destination=$file -replace ".$Environment.","."
But, unlike C #, even if $ RobotFilesToOverWrite is null, the code is entered in foreach.
So I had to surround everything:
if($RobotFilesToOverWrite) { ... }
This is the final code:
$RobotFilesToOverWrite= Get-ChildItem -Path $tempExtractionDirectory -Recurse -Include "robots.$Environment.txt" if($RobotFilesToOverWrite) { foreach($file in $RobotFilesToOverWrite) { $origin=$file $destination=$file -replace ".$Environment.","."
I was wondering if there is a better way to achieve this?
EDIT: This issue seems to be fixed in PowerShell 3.0
source share