Answered by:
create a sufolder inside a directory if it doesnt exist

Question
-
hi,
am having a create a subfolder inside a directory, if doesnt exist. its called LOGS folder. And if it exists , i need to create .log files for each and every actions that's happening in the PS code.
am a newbie in PS.
how to implement this. i checked this link : https://community.spiceworks.com/topic/473565-powershell-script-to-create-subfolder-in-all-folders-but-not-create-if-existing
am not sure whether this is correct code or not.
Das
- Edited by DADasRF Monday, July 13, 2020 9:09 AM
Monday, July 13, 2020 9:08 AM
Answers
-
sorry but i cannot get what you have and what you want
for executing 1 script from another you ca use this construction
. "c:\Some\Path\script.ps1" -param1 "value1" -param2 "value2"
if you need to start some exe you can use this construction (as i mentioned before)
& "c:\windows\system32\ping.exe" 127.0.0.1 -a
what parameters and in which format received your exe/ps1, you need to check in a documentation
The opinion expressed by me is not an official position of Microsoft
- Edited by Vector BCO Monday, July 13, 2020 1:36 PM
- Marked as answer by DADasRF Thursday, July 23, 2020 9:47 AM
Monday, July 13, 2020 1:34 PM
All replies
-
hi,
am having a create a subfolder inside a directory, if doesnt exist. its called LOGS folder. And if it exists , i need to create .log files for each and every actions that's happening in the PS code.
am a newbie in PS.
how to implement this. i checked this link : https://community.spiceworks.com/topic/473565-powershell-script-to-create-subfolder-in-all-folders-but-not-create-if-existing
am not sure whether this is correct code or not.
Das
Hi DADasRF
did you try that code? or we should do that for you?
you already have construction, and if you do not know what exact command do you can simply help commandname -full or you can just google it
The opinion expressed by me is not an official position of Microsoft
Monday, July 13, 2020 9:44 AM -
i tried it now, with few changes and its working now. but now, my question is how to create a .log file within this LOGS folder and write the activities into the .log file whenever something happens.
this is to track the happenings in the ps code ,here my business rule is , to trigger another exe( say MiddlewareProcess.exe, and which will in turn, that will consume another process-informatica-job.
hence would like to perform logging in all aspects of this process/ ps code.
do you have any sample code that writes into a .log file whenever something happens - line by line execution
Das
Monday, July 13, 2020 10:03 AM -
line by line
'some message' | out-file "c:\Some\Folder\some.log" -append
everything
start-transcript "c:\Some\Folder\Some.log" "some message" & c:\tools\3rdpartytool.exe
The opinion expressed by me is not an official position of Microsoft
Monday, July 13, 2020 10:12 AM -
if i say , "... LOGS\some.log -append", will the .log file creates automatically or i need to the existence of that .log file again ? because if there are multiple log files, how can i differentiate ? how can i put the name if the log file with curentdate-time - say - myogfile1_20200713_2210am.log file.. , wanna add the curentdatetime suffixed to the log file.
is it psosible
thanks for your help
Das
Monday, July 13, 2020 10:43 AM -
if i say , "... LOGS\some.log -append", will the .log file creates automatically or i need to the existence of that .log file again ? because if there are multiple log files, how can i differentiate ? how can i put the name if the log file with curentdate-time - say - myogfile1_20200713_2210am.log file.. , wanna add the curentdatetime suffixed to the log file.
is it psosible
thanks for your help
Das
did you check will log file be created? It takes 5 min, and you will know for sure
for working with datetime you have cmdlet get-date with parameters format and uformat
you can get help for this command with help get-date -full
Example:
$DateTime = Get-Date -Format "yyyyMMdd_HHmm" $LogName = "C:\Some\Folder\Some_$DateTime.log" 'Some message' | out-file $logname -append
The opinion expressed by me is not an official position of Microsoft
Monday, July 13, 2020 11:56 AM -
thanks again! am checking along with my businessrule .
also, i need to pass few parameters to the EXE(called the middlewareprocess), which my powershell consumes/calls.
Das
Monday, July 13, 2020 12:12 PM -
thanks again! am checking along with my businessrule .
also, i need to pass few parameters to the EXE(called the middlewareprocess), which my powershell consumes/calls.
Das
for that you need to check you middlewareprocess documentation
if you have working parameters, then you can try provide those parameters directly to the exe file
The opinion expressed by me is not an official position of Microsoft
Monday, July 13, 2020 12:25 PM -
there was similar ps file, which has hardcoded params, now, as per the new business rule its changed .
its based on the 4 different alphanumeric values- AB-XY1 , CD-LM2 , etc ..
"if you have working parameters, then you can try provide those parameters directly to the exe file" -->
regarding this, can you please elaborate a little bit.
please let me know , how to provide these params directly to the exe file.
Das
Monday, July 13, 2020 1:14 PM -
sorry but i cannot get what you have and what you want
for executing 1 script from another you ca use this construction
. "c:\Some\Path\script.ps1" -param1 "value1" -param2 "value2"
if you need to start some exe you can use this construction (as i mentioned before)
& "c:\windows\system32\ping.exe" 127.0.0.1 -a
what parameters and in which format received your exe/ps1, you need to check in a documentation
The opinion expressed by me is not an official position of Microsoft
- Edited by Vector BCO Monday, July 13, 2020 1:36 PM
- Marked as answer by DADasRF Thursday, July 23, 2020 9:47 AM
Monday, July 13, 2020 1:34 PM -
"did you check will log file be created? It takes 5 min, and you will know for sure
for working with datetime you have cmdlet get-date with parameters format and uformat "
yes, Vector, the log file is created successfully. before passing the params to the midlewareEXE file, i would like to create this ((-appending messages to the .log file ))- as a function call.
**small change in the requirement**
so, say, when , initially when the ps triggers, it chekcs for the LOGS folder exists or not, if not , it will create a LOGS folder and inside that, a new .log file has to be created and start logging all activities.
so, till now, am able to create a LOGS folder and create a .log file, now. but am confused , what are the parameters to be passed to this custom funcation call to append messages to the newly created .log file
Das
Tuesday, July 14, 2020 12:02 PM -
"did you check will log file be created? It takes 5 min, and you will know for sure
for working with datetime you have cmdlet get-date with parameters format and uformat "
yes, Vector, the log file is created successfully. before passing the params to the midlewareEXE file, i would like to create this ((-appending messages to the .log file ))- as a function call.
**small change in the requirement**
so, say, when , initially when the ps triggers, it chekcs for the LOGS folder exists or not, if not , it will create a LOGS folder and inside that, a new .log file has to be created and start logging all activities.
so, till now, am able to create a LOGS folder and create a .log file, now. but am confused , what are the parameters to be passed to this custom funcation call to append messages to the newly created .log file
Das
what do you mean by function call this is 2 lines of code and you have both of them test-path/new-item + start-transcript (once) or test-path/new-item + out-file (for each logged line)
if you do not know what to do in a script try write down all manual steps you do for your needs. Like:
1 open folder A1
2 create folder B1
3 execute command C
4 copy/paste output to the log D
...then if needed you can draw working diagram, and search exactly what you need, and ask us question with a code & error
for now your questions is too general for giving you what you need because (as 4 me) your use case is unclear: some program, some parameters, some function doing som thing
The opinion expressed by me is not an official position of Microsoft
Tuesday, July 14, 2020 12:57 PM -
am sorry for the confusion created, will detail out the steps, what i have achieved / where am stuck it.
Thanks for your time.
Das
Tuesday, July 14, 2020 6:02 PM -
Hi,
As this thread has been quiet for a while, so here is a quick question.Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
Yang YangPlease remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.Thursday, July 23, 2020 5:37 AM