return filename

by antonela at 2013-03-19 05:05:37

I have a powershell file (myFile.ps1) with this command (launch a stored procedure):

sqlcmd -S myserver -d myDB -b -Q “EXEC pr_prova” >"$LogPath\log-$mydate.log"

1.I want to write a log file with the name myFile-2013.03.18.log (In this moment I have log-2013.03.18.log)
How could I put in a variable the name of my file .ps1 (myFile)?

2.Is it possible to create myFile-[2013.03.18].log ? I have an error for the character ‘[’

$LogPath is a variable that I put in another file Variables.ps1

In myFile.ps1 I use . .\Variables.ps1

$LogPath returns “C:\Logs” only f I launch firstly the file Variables.ps1.If not I have an error…How could I fix this?

Thank you everybody
by coderaven at 2013-03-20 12:00:33
The file can be a dynamic value

$LogFileName = "$LogPath\log-$(Get-Date -format ‘M-d-yyyy’).log"

Just work with the format and Get-Date.
by MasterOfTheHat at 2013-03-20 13:00:56
To jump on what Allan has already posted, you would have to escape the brackets if you want them in the file name:
$LogPath\log-[$(Get-Date -format 'yyyy-MM-dd')].log
by antonela at 2013-03-21 01:38:11
ok, but I want to put the name ‘log’ as variable.

for example
1. if I have a command c:\windows\system32\powershell.exe c:\my\myfile.ps1
I want to have a log file with this name: c].log
2. if I have a command c:\windows\system32\powershell.exe c:\my\myfile2.ps1
I want to have a log file with this name: c].log

Is it possible to put in the file log the name of the argument?
by coderaven at 2013-03-21 11:14:46
$FillLogpath = "$LogPath-[$(Get-Date -format ‘yyyy-MM-dd’)]"

if just running powershell.exe and calling the file, you can pass the values in as normal and inside the script use $args[0] or $args[1] to get the first and second argument respectively. You could also have a param() block as the first line of your file that will receive the arguments.