0

Hi I made powershell script in which I am taking three arguements(in param block) and passing them as parameters to -FilterXPath. But I am not able to get the result.

$eventID=7036
$ServiceName=PW
$Status=stopped

It gives me the correct result if I put in the values instead of variables.

Please help. It is very urgent.

param($eventID,$ServiceName,$Status)

$eventID | add-content "path\hello.txt"
$ServiceName | add-content "path\hello.txt"
$Status | add-content "path\hello.txt"

sleep 5
$ErrorTime=Get-WinEvent -LogName 'System' -FilterXPath 'Event[System[EventID=$(eventID)] and EventData[Data[@Name="param1"]="$(ServiceName)" and Data[@Name="param2"]="$(Status)" ]]' -MaxEvents 1 | select -expand timecreated
$ErrorTime | add-content "path\hello.txt"
Akshay
  • 21
  • 1
  • 3

1 Answers1

1

Contents within single quotes are literal, variables are not expanded. Use double quotes on things that contain vars and need expansion. Replace the single quotes on the outside of your filter with double quotes. Used escaped double quotes (backtick double quote) on the inside for that parts with vars. You can use single quotes on the inside for params that are not vars.

PS C:> help about_quoting_rules

PS C:\> $var = "quick brown fox"
PS C:\> write-host "$var"
quick brown fox
PS C:\> write-host '$var'
$var
PS C:\> write-host "`"$var`""
"quick brown fox"
Clayton
  • 4,523
  • 17
  • 24