Persist console with winrm and powershell - powershell

I just started working with powershell to automate a process, and now I'm stuck with win remoting.
I enabled remoting on the machine I want to connect to and can execute a singleline command easily, e.g.
winrs -r:http://$servername:5985 -u:$user "dir c:\"
And after that command is executed the remote shell is done and i need to write winrs -r... again everytime I need to execute a command.
Is there any way to stay in the remote computer's console, like when I'm telneting or connecting with ssh?

In PowerShell try:
Enter-PSSession -ComputerName $servername

Related

How to run a powershell script as admin using a batch file on remote computer?

I am trying to run a PowerShell script as an admin using a batch file on a remote server. For this I use the following
invoke-command -computername <computername> {"<path-for-batch-file-on-remote>"}
The batch file runs the PowerShell script as administrator and works perfectly when executing from the remote pc directly.
Is there a way to make sure that it is being executed as admin while invoking using invoke-command

How to Remotely Invoke Applications (.exe file) with PowerShell with remote pc on windows?

I'm trying to execute following command on remote server:
Import.exe. file resides on remote server and this command is completely working on remote server's command line. But when I try to execute it from local system its not showing any error but still not producing any output.
Here is the command :
Start-Process -NoNewWindow -FilePath "C:\\V11\\IMPORT.EXE" -ArgumentList "cutrite1620626276329","/AUTO","/OVERWRITE"
I have connected to remote pc using ssh command.
Note : Both pc, mine and remote pc, are windows.
Start-Process command will execute process on localhost. (More on that here:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-5.1)
What you want to do is invoke this command in PSRemoting session like:
Invoke-Command -ComputerName "COMPUTERNAME" -ScriptBlock {
#This command will be executed on machine COMPUTERNAME
Start-Process -NoNewWindow -FilePath "C:\\V11\\IMPORT.EXE" -ArgumentList "cutrite1620626276329","/AUTO","/OVERWRITE"
};
There are several ways to access psremoting (you can read it here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote?view=powershell-5.1)

Running a PowerShell script on a remote Windows server using WinRM

I have written a script that allows to connect to a windows server machine using WinRM in order to run a script that exists on the Windows Server machine PS_Block_Access_Internet_GPO.ps1 but the script is not executed despite that the session was well created.
Besides the script needs administrator privileges to be executed, so how can I provide the needed privileges for script using PowerShell.
Enable-PSRemoting
Enter-PSSession -ComputerName Server.Admin.6NLG-AD
.\PS_Block_Internet_Access_GPO.ps1
To run a local script against a remote computer I would use use Invoke-Command, this doesn't need the script to be present on the remote computer.
Invoke-Command -ComputerName 'Server.Admin.6NLG-AD' -FilePath C:\Folder\myScript.ps1
As your script looks to create a GPO, you may likely need to use an alternative user account with appropriate permissions on your domain...
You can use the Credential param to specify an account like this:
Invoke-Command -ComputerName 'Server.Admin.6NLG-AD' -FilePath C:\Folder\myScript.ps1 -Credential Domain\Username

Enter-Pssession won't connect to server claiming a non-existent parameter (sessionDetails) is null

I am attempting to connect to a server using a remote PSsession and run a JEA configuration. But when I run the command
Enter-PSsession -ComputerName $server -ConfigurationName $config
It throws the following error:
Enter-PSSession : Value cannot be null.
Parameter name: sessionDetails
After examining the PSsession documentation I can't find a parameter named sessionDetails? What is going on?
FURTHER DETAILS:
Enter-PSession $server
Connects to the server without error. And when I'm on the server it's self, the command:
Enter-PSession -ComputerName localhost -ConfigurationName $config
Also enters a PSsession without error. So I'm definitely able to access the server and the configuration definitely works (at least on local host).
Add in your .pssc file a line with TranscriptDirectory = 'xxx'
With xxx the folder where audit trail logging will be stored. This will log on the server where you register the JEA script who connected.
In such logging file you can see a line like:
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.2.0
This indicates the powershell versions that are supported for the CLIENT side.
In case you are using a client version that is not listed you get that error.
Enter-PSSession may not be used correctly by you. Try the below syntax.
Command to start an interactive session with Server01, a remote computer. When the session starts, the command prompt changes to include the computer name.
PS C:\> Enter-PSSession -ComputerName Server01
[Server01]: PS C:\>
Command to get the PowerShell process and redirects the output to the Process.txt file. The command is submitted to the remote computer, and the file is saved on the remote computer.
[Server01]: PS C:\> Get-Process Powershell > C:\test\Process.txt
Exit command to end the interactive session and close the connection.
[Server01]: PS C:\> exit
PS C:\>

Can you run remote commands without psexec?

I am attempting to run a batch file on several remote machines. It has some registry changes and other commands that I am able to run remotely. I have one command I have not been able to figure out an alternative:
net user USERNAME /PASSWORDREQ:yes
Is there a way to run this command remotely without psexec? I'd rather not distribute my batch file with a dependancy.
Yes, you can enable powershell remoting on the remote computers, and then use the Invoke-Command cmdlet. Example:
Invoke-Command -ComputerName RemoteComputer -Script { param($userName) net use $userName /PASSWORDREQ:yes } -Args "UserNameArgumentValue"