How to do a remote PSSession and run a script in the remote desktop?

rogeliofca :

When I try to connect and run a PowerShell script manually from CMD I can do it, but when I try to do the same in my Java code an error is displayed, my code for the connection is this:

String command = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell C:\\scr.ps1";

Process powerShellProcess = Runtime.getRuntime().exec(command);

powerShellProcess.getOutputStream().close();
String line;
System.out.println("Standard Output:");
BufferedReader stdout = new BufferedReader(new InputStreamReader(
    powerShellProcess.getInputStream()));
while ((line = stdout.readLine()) != null) {
    System.out.println(line);
}
stdout.close();
//En caso de error se obtiene
System.out.println("Standard Error:");
BufferedReader stderr = new BufferedReader(new InputStreamReader(
    powerShellProcess.getErrorStream()));
while ((line = stderr.readLine()) != null) {
    System.out.println(line);
}
stderr.close();
System.out.println("Done");

Before I tried to put all the commands in a the command string, but that didn't work, so I decided to put it in a script. The script contains this:

$pass = ConvertTo-SecureString "pass" -AsPlainText -Force
$cred = New-Object System.management.Automation.PSCredential("user", $pass)
Enter-PSSession -ComputerName User-Pc -Credential $cred ;

dir

but the last command dir is done in mi local desktop not in the remote one, I debug and I don't find any error the script works, the connection was done but I don't understand why the dir command doesn't execute in the remote desktop, somebody have any idea of what is happening?

Ansgar Wiechers :

Enter-PSSession is for interactive use. For non-interactive command execution use Invoke-Command:

Invoke-Command -Computer 'User-Pc' -Scriptblock {
    Get-ChildItem
} -Credential $cred

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Enter-PSSession unable to remote into a remote desktop

How to run script on remote pc

How do I run a local Python script on a remote Spark cluster?

How do I run a local fish script on a remote server?

How do I enable remote desktop of the GUI?

How to make a remote computer run PowerShell script on the remote computer itself?

How to run remote script on multiple host simultaneously

How to run a complex awk script on a remote machine?

how to run script on remote machine with delay

How to run mongo db script on remote server?

How to run appium script in remote machine?

How to run PowerShell script on a remote computer?

How can I disable incoming remote desktop connections while I run an "outgoing" remote desktop session?

how to enable remote desktop?

How can I run Remote Desktop Sharing on Ubuntu Bionic with Unity?

How can I run xmonad on a nomachine remote desktop?

How to let Chrome Remote Desktop run with encrypted /home before login

How to script to give back remote desktop to the console? (with tscon and qwinsta)

Windows Remote Desktop -- how to not have remote screen on

How to use a PowerShell script to run another PowerShell Script on a Remote Computer

How do I enable reboot in Windows 7 for Remote Desktop sessions?

How do you install Chrome Remote Desktop from .deb?

How do I open a remote desktop session on a secondary monitor?

How do I pin a remote desktop connection to the taskbar?

how do I terminate a remote desktop session in windows 2012?

How do I make a shortcut of a remote desktop connection and include the password?

How do I use my current credentials with Remote Desktop?

How do you leave Windows Remote Desktop Connection with keyboard shortcut?

How to Configure a vm on the same machine to do remote desktop