Firstly I want to point out that I'm a PowerShell ScrapBooker and not really terribly knowledgeable about PowerShell.
I've been working on a script that installs BGInfo ... I have the actual install and uninstall working perfectly, and now I'm moving on to getting the logging sorted out nicely.
I found this article "Create Log File in Powershell" which was wonderful and have incorporated this Function into my script.
Function Write-Log {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
[ValidateSet("LABEL","INFO","WARN","ERROR","FATAL","DEBUG")]
[String]
$Level = "INFO",
[Parameter(Mandatory=$True)]
[string]
$Message,
[Parameter(Mandatory=$False)]
[string]
$logfile
)
$Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
IF ($Level -eq "LABEL") {
$Line = "$Message"
}
ELSE {
$Line = "$Stamp $Level $Message"
}
If($logfile) {
Add-Content $logfile -Value $Line
}
Else {
Write-Output $Line
}
}
What I need to know is how to use this to log output from commands.
For Example:
In My script I have this command:
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name BgInfo -Value """$InstPath\Bginfo.exe"" $InstPath\$BGTemplateFile $InstOptions" -PropertyType 'String' -Force
or this one:
Copy $SourcePath\Bginfo.exe $InstPath
What I would like to know is how I can use my function to capture ANY output from that command and log it in my log file.
I guess that I would also like to use this information and apply it to any other commands where I want to log something.
Hopefully that's all clear and makes sense and that someone can help me out.
Cheers,
Dave.
:)