Reputation: 1
I'm brand new to PowerShell and am working on modifying a script to combine 4 functions into one. I am having a little trouble understanding how all the pieces of the individual functions fit together. For example, it has a $msg variable that doesn't seem to be declared anywhere else in the script. So essentially i'm looking for any advice on how to make these fit.
##LogSuccess function
##Display provided message as a SUCCESS in green, with SUCCESS: prefix
##If logging mode is set to true, also write SUCCESS message to $logfileSS
Function global:LogSuccess($msg){
Write-Host "SUCCESS: " $msg -ForegroundColor "Green"
$timestamp = Get-Date
$msg = $timestamp.ToString() + ": " + $msg
if ($global:loggingmode){
Write-Output "SUCCESS: " $msg | Out-File -filepath $global:logfile -Append
}
}
##LogError function
##Display provided message as an error in red, with ERROR: prefix
##If logging mode is set to true, also write ERROR message to $logfile
Function global:LogError($msg){
Write-Host "ERROR: " $msg -ForegroundColor "Red"
$timestamp = Get-Date
$msg = $timestamp.ToString() + ": " + $msg
if ($global:loggingmode){
Write-Output "ERROR: " $msg | Out-File -filepath $global:logfile -Append
}
}
##LogWarning function
##Display provided message as a WARNING in yellow, with WARNING: prefix
##If logging mode is set to true, also write WARNING message to $logfile
Function global:LogWarning($msg){
Write-Host "WARNING: " $msg -ForegroundColor "Yellow"
$timestamp = Get-Date
$msg = $timestamp.ToString() + ": " + $msg
if ($global:loggingmode){
Write-Output "WARNING: " $msg | Out-File -filepath $global:logfile -Append
}
}
##Logging function
##Display provided message as a general information message in cyan
##If logging mode is set to true, also write information message to $logfile
Function global:Logging($msg){
Write-Host $msg -ForegroundColor "Cyan"
$timestamp = Get-Date
$msg = $timestamp.ToString() + ": " + $msg
if ($global:loggingmode){
Write-Output $msg | Out-File -filepath $global:logfile -Append
}
}
Upvotes: 0
Views: 69
Reputation: 1816
from my point of view those functions are not designed as intended, e.g.:
Function global:Logging($msg){
Write-Host $msg -ForegroundColor "Cyan"
$timestamp = Get-Date
$msg = $timestamp.ToString() + ": " + $msg
if ($global:loggingmode){
Write-Output $msg | Out-File -filepath $global:logfile -Append
}
}
PowersShell functions accept named input parameters and are outputting objects in general. In simple words this is the concept. Currently those functions do not return objects they do update/use variables with the scope global. This is a dangerous approach and not needed.
About scopes: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.2
About functions: https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/09-functions?view=powershell-7.2
Back to the one example you should do something like this:
Function write-LogFile {
<#
.Description
Enter your description here
.Parameter Message
Enter your description of the parameter here
.Parameter LogToTextFile
Enter the desscription of the parameter here
.Parameter Path
Enter the desscription of the parameter here
.Example
Enter example calls here
#>
param (
[parameter(Mandatory=$true,Position=1)]
[ValidateNotNullOrEmpty()]
[string]$Message,
[parameter(Mandatory=$true,Position=2)]
[switch]$LogToTextFile,
[parameter(Mandatory=$true,Position=3)]
[ValidateNotNullOrEmpty()]
[string]$Path
)
Begin {
}
Process {
try {
Write-Host $msg -ForegroundColor "Cyan"
$timestamp = Get-Date
$msg = $timestamp.ToString() + ": " + $msg
if ($LogToTextFile){
Write-Output $msg | Out-File -filepath $path -Append
}
Else {
$msg
}
}
Catch {
write-error $_
}
}
End{
}
}
So I think before you start to merge those function you need first to understand the concept how to write a function. The provided links should help you to find the right path...
Upvotes: 1