Monday 4 May 2015

powershell write to log file

Batch File
My,bat

--------------------------------------
@echo off
SET Today=%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,5%.log
echo %today%
echo
(
PowerShell.exe Set-ExecutionPolicy RemoteSigned; "C:\my.PS1"
)
echo save all log in to file- start
echo
echo save all log in to file - end
PowerShell.exe -Command "& {C:\my.PS1}" > C:\%today%

echo save all log in to file - end
echo
PAUSE


-------------------------------------------
PowereShell
my.PS1
----------------------------------------------
Param
(
[string]$fileName = $(get-date -f yyyy-MM-dd-hh-mm-ss)
)
write-host "Hello" `n
write-host "Start print date" `n
write-host $fileName".log" `n
write-host "End print date" `n


Another way of saving log data in to a file.
------------------------------------------

Batch File
My,bat

--------------------------------------
@echo off
SET Today=%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,5%.log
echo %today%
echo
(
PowerShell.exe Set-ExecutionPolicy RemoteSigned; "C:\my.PS1"
)
echo save all log in to file- start
echo
echo save all log in to file - end

> C:\%today%

echo save all log in to file - end
echo
PAUSE


-------------------------------------------
PowereShell
my.PS1
----------------------------------------------
Param
(
[string]$fileName = $(get-date -f yyyy-MM-dd-hh-mm-ss)
)
write-host "Hello" `n
write-host "Start print date" `n
write-host $fileName".log" `n
write-host "End print date" `n