powershell - writing output file as .txt or .csv to script containing multiple conditions -
i have written script checks service status, tests 2 paths , tests registry value. getting output on power shell console (that because using write-output command). there way write single 1 page output file? struggling find way out-file entire output file.
below script.
$testpath = test-path "c:\test" $testpath2 = test-path "c:\test" $mcshieldk = get-service -name mcshield | select name $internet = (get-itemproperty -path "hklm:\software\microsoft\internet explorer").mkenabled $hostname = hostname write-host "hostname of comuter is" $hostname if (test-path $machinetype) {} else { write-host "internet is" $internet } if ($testpath -eq $true -and $testpath2 -eq $true) { write-host "test , test1 folder exists" -foregroundcolor green } else{ write-host "folder not exists" -foregroundcolor red } if($mcshield.name -eq "mcshield") { write-host "mcshield service exists" } else { write-host "mcshield not exists" }
below console output
hostname of comuter server1 internet yes test , test1 folder exists mcshield not exists
swap out write-host
cmdlets or add in line following:
"your output text $yourvariable" | out-file -filepath "c:\log.txt" -append -encoding utf8
this append string end of log file c:\log.txt
. note, missing -append
parameter cause file overwritten.
you can use follow give same affect:
"your output text $yourvariable" >> "c:\log.txt"
but not mix 2 methods might encoding errors in text file. if wish overwrite file second method use >
instead of >>
.
Comments
Post a Comment