by antonela at 2013-03-19 05:05:37
I have a powershell file (myFile.ps1) with this command (launch a stored procedure):by coderaven at 2013-03-20 12:00:33
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
$Global:LogPath="C:\Logs"
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
The file can be a dynamic valueby MasterOfTheHat at 2013-03-20 13:00:56$LogFileName = "$LogPath\log-$(Get-Date -format 'M-d-yyyy').log"
Just work with the format and Get-Date.
To jump on what Allan has already posted, you would have to escape the brackets if you want them in the file name:by antonela at 2013-03-21 01:38:11$LogPath\log-
[$(Get-Date -format 'yyyy-MM-dd')
].log
ok, but I want to put the name ‘log’ as variable.by coderaven at 2013-03-21 11:14:46
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?
$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.