Powershell job: you cannot call a method on a null-valued expression..Exception.Message at

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
This topic is 4 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Powershell job: you cannot call a method on a null-valued expression..Exception.Message at

Post by stevens »

Hi,

I' m trying to run a script (which takes about 5 minutes) in a powershell job.
PS1 works but not in the job.
Error I get: You cannot call a method on a null-valued expression..Exception.Message at line 6508

Please advise.

Code: Select all

		Logging-Output "Setting path to script"
		$CMOverviewScript = "$ScriptDirectory\CM.ps1"
		Logging-Output "Done"
		
		#if (! $(test-path $CMOverviewScript)){throw "Cannot reach $CMOverviewScript"}
		<# THIS WORKS FINE
		Logging-Output 'Getting CM Details'
		. $CMOverviewScript #Generates $CMApplicationsOverview as global
		$ProcessResults = $CMApplicationsOverview
		Logging-Output 'Done'
		#>
	
		#region jobtracker
		#$progressbaroverlay1.Visible = $true
		Logging-Output -text "Getting CM details as job"
		
		Add-JobTracker -Name 'CM' `
					   -JobScript {
			. $args[0]  #Generates $CMApplicationsOverview as global
			$CMApplicationsOverview 
		}`
						-CompletedScript {Param ($Job)
									$ProcessResults = Receive-Job -Job $Job
									#$ProcessesOutput = $ProcessResults | Out-String			
									#$progressbaroverlay1.Visible = $false
									}`
					   -UpdateScript {	Param ($Job)
									}`
					   -ArgumentList $CMOverviewScript
		
		Logging-Output -text "Done."
		#endregion
		
		$ProcessResults | Out-GridView -Title "SCCM Overview"
		
	
Error => You cannot call a method on a null-valued expression..Exception.Message at line 6508
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Powershell job: you cannot call a method on a null-valued expression..Exception.Message at

Post by Alexander Riedel »

Which line exactly is 6508?
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Powershell job: you cannot call a method on a null-valued expression..Exception.Message at

Post by stevens »

I have no clue. The line even does not exist.
Is the syntax of my code correct?
User avatar
Nillth
Posts: 34
Last visit: Mon Oct 30, 2023 10:52 pm
Has voted: 2 times

Re: Powershell job: you cannot call a method on a null-valued expression..Exception.Message at

Post by Nillth »

The chances are that it is going to be caused by something in either the .ps1 file or the functions
"$ScriptDirectory\CM.ps1", Logging-Output, Add-JobTracker
are you able to share the full project?
PowerShell_Studio_bFATnZaLLV.png
PowerShell_Studio_bFATnZaLLV.png (15.27 KiB) Viewed 3098 times
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Powershell job: you cannot call a method on a null-valued expression..Exception.Message at

Post by stevens »

Thanks for the input. I'll leave the job for now and come back to it later.
This topic is 4 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked