0

I am writing a function in powershell to use Get-WinEvent of a remote computer to view Event IDs from a textbox in a form.

The function is:

function getSystemEvents {
$EventCode = $EventInputBox.text;
$CompName = $PCInputBox.Text;
$Network = 'myNetwork.net'
$ListEvent = Invoke-Command -ComputerName $CompName$Network -ScriptBlock {Get-WinEvent -FilterHashtable @{logname='system'; id=$EventCode} | Select-Object TimeCreated, ID, Message | ft -auto} | out-string
$outputBox.text=$ListEvent
} 

The id=$EventCode does not work. If I manually replace it with id='12' instead of the variable, I will get a list of EventID=12s from the remote machine.

I think possibly the issue is that I don't know how to put the variable $EventCode in between two ' characters like id='$EventCode' without breaking the script.

Many thanks in advance!

  • 1
    The problem is unrelated to quoting (you need none): The script block is being executed on a _remote_ machine, where the _local_ variables such as `$EventCode` don't exist; use `$using:EventCode` instead - see the linked question. – mklement0 Jun 09 '17 at 15:51
  • 2
    Try: `-FilterHashtable @{logname='system'; id=$Using:EventCode}` –  Jun 09 '17 at 15:53

0 Answers0