As in any development, logging plays a very important role, especially when it comes to debugging. And if you have just started using PowerShell script you will appreciate how important it is to log activities and results of the Powershell cmdlets you are running..
Here is basic code which helps you to log all the activities and their results.
Define the log file details – This part defines the file name and file path for the logging. For the log file you can choose any name. I| have chosen to assign the date variable as file name. If your script is running more than once a day, you can add hours,minutes and second variable also to make the file name unique.
$FilePath = "C:\Users\powershelluser\Documents\scripts\Log"
$FileName = Get-Date -Format yyyy-MM-dd
$File = "$FilePath\$FileName.txt"
Create a folder – This part creates a folder at the location given in the $FilePath variable if the filepath does not exist already.
if ((Test-Path -Path $FilePath) -ne $True)
{New-Item $FilePath -type directory}
Start logging – This part starts logging the details in the file mentioned above. You can start writing your script logic after this
Start-Transcript -Path $File –Force
Stop logging
Stop-Transcript
Here is basic code which helps you to log all the activities and their results.
Define the log file details – This part defines the file name and file path for the logging. For the log file you can choose any name. I| have chosen to assign the date variable as file name. If your script is running more than once a day, you can add hours,minutes and second variable also to make the file name unique.
$FilePath = "C:\Users\powershelluser\Documents\scripts\Log"
$FileName = Get-Date -Format yyyy-MM-dd
$File = "$FilePath\$FileName.txt"
Create a folder – This part creates a folder at the location given in the $FilePath variable if the filepath does not exist already.
if ((Test-Path -Path $FilePath) -ne $True)
{New-Item $FilePath -type directory}
Start logging – This part starts logging the details in the file mentioned above. You can start writing your script logic after this
Start-Transcript -Path $File –Force
Stop logging
Stop-Transcript
Get the opportunity to excel in your academics. Use our assistance to improve your writing skills and build skillset that would help you in the long run.
ReplyDeletehttps://academicswritingservice.co.uk/
By seeking our assistance of power point presentation help in Pakistan you not only get the professional supervision, but academics help that can make your presentation appear robust and rich.
ReplyDeletehttps://assignmentshelp.com.pk/