jPowerShell icon indicating copy to clipboard operation
jPowerShell copied to clipboard

Enter-pssession closing issue

Open wgtmpeters opened this issue 8 years ago • 2 comments

I am having a great difficulty getting the following working. Have tried with and without remotemode, and it seems that i need it with otherwise each call takes the maxWait amount of time to finish.

I also need 2 exit statements otherwise the powershell.Close throws a TimeOut. So it seems that my commands somehow open 2 extra powershell shells? I understand 1 extra as the enter-pssession start a new shell remotely.

The first powerShell.configuration(config).executeCommand("exit"); always takes the maxWait time to finish.

	@Test
	public void testConcreteExecute_Remote() {
		PowerShell powerShell = null;
		PowerShellResponse response = null;
		Map<String, String> config = new HashMap<String, String>();
		config.put("remoteMode", "true");
		config.put("maxWait", "50000");
		try {
			powerShell = PowerShell.openSession();
			
			response = powerShell.configuration(config).executeCommand("$password = ConvertTo-SecureString \"password\" -AsPlainText -Force");
			response = powerShell.configuration(config).executeCommand("$cred= New-Object System.Management.Automation.PSCredential (\"Administrator\", $password)");
			response = powerShell.configuration(config).executeCommand("Enter-pssession 192.168.1.1 -Credential $cred");
			System.out.println("PSsession:" + response.getCommandOutput());
			//Execute another command in the same PowerShell session
			response = powerShell.configuration(config).executeCommand("Get-WmiObject Win32_BIOS");
		
			powerShell.configuration(config).executeCommand("exit");
			powerShell.configuration(config).executeCommand("exit");
			System.out.println("BIOS information:" + response.getCommandOutput());
			Assert.assertEquals(false, response.isError());
		} catch (PowerShellNotAvailableException ex) {
			System.err.println("PowerShell not running on remote system" + ex);
		} finally {
			if (powerShell != null) {
				powerShell.close();
			}
		}
	}

wgtmpeters avatar Jan 19 '17 11:01 wgtmpeters

Hello,

Your use case is quite in the edge!

You have no need to force the configuration each time. It should be ok if you perform a:

powerShell = PowerShell.config(config).openSession();

In your example you mix remote and non remote calls which I think it is not a good idea. Do you have the same problem if you call the Get-WmiObject Win32_BIOS with remoteMode=false?

The double exit is really weird, but I do not have right now the means to reproduce it. Could you debug it to see what is happening?

Best regards

profesorfalken avatar Jan 24 '17 19:01 profesorfalken

I am currently trying without opensession but with only direct Invoke-commands .This seems to be much more stable. As in no extra exit statements are needed. Currently i use executeScript with a small fix which i will upload later (handling of output from script without newline ). I am able to execute remotely with arguments and/or parameters. One bug i need to solve though which is remotemode not handling errors correctly. The normal stream exits before the error stream but the errorstream does give output.

wgtmpeters avatar Jan 25 '17 10:01 wgtmpeters