level_0_apprentice
level_0_apprentice

Reputation: 17

Why is my string variable not concatenating in my Powershell function?

I'm writing a function to Write-EventLog with the information changing depending on arguments. One of those arguments is the message, but it will not display in the actual event log. Only the string portion that's hard coded.

I've tried assigning it to a variable and outputting it to make sure it's there and it shows that even though the variable type is string it won't concatenate in the function.

$testmsg1 = "The script successfully completed an operation"

Function Log-This($level, $message){

    If ($level -eq "Information"){
        $testmsg1
        $msg = "INFO: $message" 
        $msg
        Write-EventLog -LogName Application `
            -EventId 1099 `
            -EntryType Information `
            -Message $msg `
            -Source "Windows Error Reporting"
        }
... 
}
$testmsg1.GetType()

Log-This "Information", $testmsg1

This produces:

IsPublic IsSerial Name                                     BaseType                                                                                                                        
-------- -------- ----                                     --------                                                                                                                        
True     True     String                                   System.Object                                                                                                                   
The script successfully completed an operation
INFO: 

What I'm going for is the string "INFO: The script successfully completed an operation". What am I doing wrong to not have the string concatenate?

Upvotes: 0

Views: 185

Answers (2)

js2010
js2010

Reputation: 27471

Don't use a comma in the function call either.

Upvotes: 1

level_0_apprentice
level_0_apprentice

Reputation: 17

When evoking the function you need to identify the parameters for it individually.

e.g.

Log-This -level "Information" -message $testmsg1

Thanks @js2010 for pointing that out.

Upvotes: 1

Related Questions