To help you better we need some information from you.
*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***
Product, version and build: Powershell Studio 2017
32 or 64 bit version of product: 64Bit
Operating system: Windows 10
32 or 64 bit OS: 64 Bit
#####################################################################################################
Hi, within a form i've created a button to pull in infomation from the event log on a remote machine and populate a datagridview.
To do this I need to run the query in a job and wait for the results to finish. The problem I'm getting in the script below if that the variables I add to the arguments are not being passed to the the scriptblock and the results are null. If I replace the variables with the values the results are populated in the datagrid view OK. I've tested the comboboxes and textboxes values and they all return the expected results.
I've also posted this in the Powershell Studio forum
#######################################################################################################
#null results
$buttonGetEvents_Click={
$logname = $combobox2.Text
$ComputerName = $textbox22.Text
$Days = $textbox23.Text
Start-Job -Name "EventLog" -ScriptBlock { Get-EventLog -LogName $logname -ComputerName $ComputerName -After (get-date).AddDays(- ($days)) | where { $_.EntryType -eq "Error" } } -ArgumentList $ComputerName, $logname, $Days | Wait-Job
Load-DataGridView -DataGridView $datagridview15 -item (Receive-Job "EventLog") -AutoSizeColumns DisplayedCells
}
####################################################################################################################################
#Error
ERROR: Cannot bind argument to parameter 'LogName' because it is null.
ERROR: + CategoryInfo : InvalidData: (:) [Get-EventLog], ParameterBindingValidationException
ERROR: + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetEventLogCommand
ERROR: + PSComputerName : localhost
ERROR:
ERROR: Load-DataGridView : Cannot validate argument on parameter 'Item'. The argument is null. Provide a valid value for the argument, and then try running the
ERROR: command again.
EUT Search Tool.psf (2240, 56): ERROR: At Line: 2240 char: 56
ERROR: + ... -DataGridView $datagridview15 -item (Receive-Job "EventLog") -AutoSi ...
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidData: (:) [Load-DataGridView], ParameterBindingValidationException
ERROR: + FullyQualifiedErrorId : ParameterArgumentValidationError,Load-DataGridView
ERROR:
####################################################################################################################################
#Results OK
$buttonGetEvents_Click={
Start-Job -Name "EventLog" -ScriptBlock { Get-EventLog -LogName application -ComputerName mymachinename -After (get-date).AddDays(- (1)) | where { $_.EntryType -eq "Error" } } -ArgumentList $ComputerName, $logname, $Days | Wait-Job
Load-DataGridView -DataGridView $datagridview15 -item (Receive-Job "EventLog") -AutoSizeColumns DisplayedCells
}
####################################################################################################################################
Arguments in Jobs
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Arguments in Jobs
You need to add a Param(var1, var2, ...varx) to the scriptjob. It's better if you create the object:
$Scriptjob = {
param(...)
#code below
:
}
Then, send it to the start-job cmdlet: Start-Job -Name "test" -scriptblock $ScriptJob -Argumentlist ...
It should work after adding the param() code.
$Scriptjob = {
param(...)
#code below
:
}
Then, send it to the start-job cmdlet: Start-Job -Name "test" -scriptblock $ScriptJob -Argumentlist ...
It should work after adding the param() code.