Powershell Out-file not working -
i have powershell script gathers file information on remote servers. when writing console, see results fine, don't seem getting written specified out-file:
out-file -filepath $filepath -inputobject $date -force -encoding ascii -width 50 foreach($server in $serverlists) { $result += $server $result += $break foreach($filepath in $filepaths) { $result += $filepath $result += $break $command = "forfiles /p " + $filepath + " /s /d -1 > c:\temp\result.txt" $cmd = "cmd /c $command" invoke-wmimethod -class win32_process -name create -argumentlist $cmd -computername $server sleep 2 $result += get-content \\$server\c$\temp\result.txt $result += $break $result += $break write-host $result #out-file -filepath $filepath -append -inputobject $result -encoding ascii -width 200 remove-item \\$server\c$\temp\result.txt } $result += $break } out-file -filepath $filepath -append -inputobject $result -encoding ascii -width 200
you use same variable $filepath
in 2 places: loop variable foreach
loop , outside of loop. foreach
loop not restore value of loop variable after loop ends. so, right after loop ends loop variable have last value of loop or value loop interrupted break
command. if not want variable value overwritten foreach
loop, should choose different name loop variable.
Comments
Post a Comment