Implicit remoting returned in text

Ask your PowerShell-related questions, including questions on cmdlet development!
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 5 months 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Implicit remoting returned in text

Post by jvierra »

Here is how this needs to be constructed for a whole host of reasons. Also note that we do not use "global" in Param statements and they can cause issues. By usage and definition all params in any language are only local and cannot be made global.

Multiple unnecessary assignments are not useful and will make debugging and management harder.

Code: Select all

#region Load chat room
$RoomData = @{
    Name = 'RoomData'
    JobScript = {
        Param(
            $UserCred,
            $RoomName
        )

        try{				
            $session = New-PSSession -ConnectionUri 'https://sfbpool.domain.com/ocsPowerShell' -Credential $UserCred
            Import-PSSession $session -AllowClobber

            $room = Get-CsPersistentChatRoom -Identity $RoomName
            [PSCustomObject][Ordered]@{
                Description = $room.Description
                Name = $room.Name
                Managers = $room.Managers
                Members = $room.Members
                Check	= 'Success'
                ErrorMessage = $null
            }
        }
        catch{
            [PSCustomObject][Ordered]@{
                Description = $null
                Name = $RoomName
                Managers = $null
                Members = $null
                Check	= 'Fail'
                ErrorMessage = "$_"
            }
        }

        for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
    }
    ArgumentList = $global:UserCred, $global:RoomName
    CompletedScript = {
        Param ($Job)
        $global:RoomData = Receive-Job -Job $Job | select Description, Name, Managers, Members
    }
    UpdateScript = {
        Param ($Job)
    }
}
#endregion Load chat room
	
Add-JobTracker @RoomData
Please run just the JobScript code at a console prompt and inspect the results. All objects returned from a script must have the same structure or the usability of the returned data will be questionable.

Take my word for this. I know from years of experience.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Implicit remoting returned in text

Post by Shelltastic »

Thanks for that response jvierra. I would also agree that creating a blank form with just this specific code would 100% help, I don't dispute that, I just disagree that it's binary like you are implying, it's not one way or the other basically, if you see something you personally would do differently you automatically dismiss the majority of the code and imply to re-write things, which any poster on this website would easily agree with.

Although things "could" work better using different methods, and things "could" be more efficient for debugging, it doesn't make the way I wrote it "wrong", if it's a bit more un-efficient this way then so be it, but that is relative, it's not unusable by any means and still works very well, my business has a need for my program to be designed a certain way, which is why you see me using the $global variable tag.

The reason my variables in the jobscript are assigned the $global tag is because those variables are coming from a different form, so by giving them the variable $global I can call them easily in the next form that is loaded within the program, just a FYI on that one so you can better understand.

If my code makes it impossible for you to assist then that is ok, just say so and I will move on while thanking you for your time. I was simply stating that my code (while still clean and easy to read) works 100% of the time and I was trying to pick it up from there to avoid debugging dozens of other lines, if however that is not an option then I certainly appreciate your time in taking a peek. I was merely wondering if you were familiar with the garbage text being added and if there was a more simple approach to removing only that.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Implicit remoting returned in text

Post by jvierra »

You cannot use "global" in a job or in a Param statement. You are not using a different technique you are just coding it wrong. In order to track down difficult bugs it is necessary to remove all "noise", bad syntax and coding errors to eliminate side effects. I have been building and debugging code for 40+ years. I know how to do this. I have fixed thousands of issues that programmers couldn't fix.
I can help if you listen and follow my suggestions. After we locate your issue you can redo anything in any way you like.
If you want to pursue this issue your way then go ahead. I cannot force you to do anything.

I would also suggest that your usage of "global" shows that you do not have a clear understanding of "scope". A job runs in a complete separate process. There is no way for items in a job to access objects in the main form or for the form to see variables in the job. "Global" does not change this process isolation.

I still suggest that you create a simple form with one control and run the job code. If it stil fails then post the form and I will look at it. The issue is likely a cod9ing error similar to others you have made which is giving you bad results. Without this exercise you wil have to discover the issue in the debugger. That could take a very long time with some subtle coding errors.

Just remember that using "global" in a Param statement serves no purpose and is bad syntax. It can cause issues. It has always surprised me that the PS parser doesn't warn or error on this as it has been an issue since PS2. That is why I always remove that construct when trying to track down an issue. At least removeit in the simple test form and to see if the test form works or fails. If the test form fails then post the PSF and I will look at it for other issues that can cause this kind of behavior.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Implicit remoting returned in text

Post by Shelltastic »

Ok, I will see if I can draw it up in a blank form and post it back here. Couple things to note...

You said: "I would also suggest that your usage of "global" shows that you do not have a clear understanding of "scope". A job runs in a complete separate process. There is no way for items in a job to access objects in the main form or for the form to see variables in the job."

My response: I am not trying to have the job access items in the form, your understanding of my code is wrong. I am passing the $global variables to the background job in the parameters, which can be done in Powershell, that is why the code actually works right now.

You said: I still suggest that you create a simple form with one control and run the job code. If it still fails then post the form and I will look at it.

My response: Nothing is "Failing" here, it's important to note that and I've stated this multiple times in my responses. I just want to be clear on that because there is large difference between my code "failing", and my code just returning some default values that I am having trouble removing, which is the entire purpose of this post. My code actually gives me the expected results, its just a simple one-line sentence that is also returned with it that I am trying to remove.

Thanks again
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Implicit remoting returned in text

Post by Shelltastic »

I was able to re-produce it in a blank form, still getting that default return value of "Implicit remoting https://***** ". Here is the code below..

Code: Select all

$buttonStartJob_Click={
	
	$buttonStartJob.Enabled = $false
	
	$RoomName = $textbox1.Text
	
	#region Load chat room
	$RoomData = @{
		Name	  = 'RoomData'
		JobScript = {
			
			Param ($UserCred,
				$RoomName
			)
			
			try
			{
				$session = New-PSSession -ConnectionUri "https://sfbpool.domain.com/ocsPowerShell" -Credential $UserCred
				Import-PSSession $session -AllowClobber
				
				$Room = Get-CSPersistentChatRoom -Identity $RoomName
				
				$Table = [PSCustomObject]([Ordered]@{
						'Description' = $Room.Description
						'Name'	      = $Room.Name
						'Managers'    = $Room.Managers
						'Members'	  = $Room.Members
					})
			}
			catch
			{
				$Table = [PSCustomObject]([Ordered]@{
						'Check'	       = "Fail"
						'ErrorMessage' = $PSItem
					})
			}
			
			$Table
			
			for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
		}
		ArgumentList = $UserCred, $RoomName
		CompletedScript = {
			Param ($Job)
			$results = Receive-Job -Job $Job
			
			if ($results.Check -eq "Fail")
			{
				[System.Windows.Forms.MessageBox]::Show("Failed to load room data, please try again.", 'Room data')
				return
			}
			
			$RoomData = $results
			$textbox2.Text = $RoomData.Description
		}
		UpdateScript = {
			Param ($Job)
		}
	}
	#endregion Load chat room
	
	Add-JobTracker @RoomData
}
I get the correct description, it just has that random line in front of it about the implicit remoting, same results as in the full program.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Implicit remoting returned in text

Post by jvierra »

PS_Ross wrote: Wed Oct 30, 2019 9:49 am
You said: "I would also suggest that your usage of "global" shows that you do not have a clear understanding of "scope". A job runs in a complete separate process. There is no way for items in a job to access objects in the main form or for the form to see variables in the job."

"global" is never required in a "Param()" statement and the "global" tag serves no purpose as you are using it. It does show that you do not understand "scope". A parameter is always received in the local scope. That cannot be changed. Just don't use that construct.

The "global" scope tag is used to create or assign a variable to thee global scope of a PowerShell session. When any variable is passed in an "Arguments" statement or as a "parameter" to a command the variable can be specified as "global in the argument but it will not be global when it is received. It will be a copy of the passed variable created in the local scope of the command, scriptblock or job. This provides and enforces code isolation by scope restriction. It is the way all languages work. In all compiled languages trying to make a parameter declared global would cause a compiler error.

In any case it is an error and needs to be removed for debugging. If you want to keep using it after the issue has been resolved then you are free to do so. Let's just find the issue in your code that is causing the weird behavior you are seeing. Who know. Perhaps there is a bug in the modules you are using or even a subtle bug in PowerShell.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Implicit remoting returned in text

Post by jvierra »

PS_Ross wrote: Wed Oct 30, 2019 10:06 am I was able to re-produce it in a blank form, still getting that default return value of "Implicit remoting https://***** ". Here is the code below..

Code: Select all


I get the correct description, it just has that random line in front of it about the implicit remoting, same results as in the full program.
[/quote]
Please attach the PSF file.  Just looking at the same code is not much help.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Implicit remoting returned in text

Post by Shelltastic »

PSF file attached
Attachments
MainForm.psf
(24.1 KiB) Downloaded 110 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Implicit remoting returned in text

Post by jvierra »

The form you posted is completely blank. I cannot use a form from a project. Open a new simple form not a project and place all code including the control that displays the bad results in that form. Post that PSF form.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Implicit remoting returned in text

Post by Shelltastic »

PSF file attached again.
Attachments
Example.psf
(23.97 KiB) Downloaded 100 times
This topic is 4 years and 5 months 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