Sunteți pe pagina 1din 8

TOPIC

about_PSSession_Details

SHORT DESCRIPTION
Provides detailed information about Windows Pow
erShell sessions and the
role they play in remote commands.

LONG DESCRIPTION
A session is an environment in which Windows Po
werShell runs. A session is
created for you whenever you start Windows Powe
rShell. You can create
additional sessions, called "Windows PowerShell
sessions" or "PSSessions"
on your computer or another computer.

Unlike the sessions that Windows PowerShell cre


ates for you, you control
and manage the PSSessions that you create.

PSSessions play an important role in remote com


puting. When you create a
PSSession that is connected to a remote compute
r, Windows PowerShell
establishes a persistent connection to the remo
te computer to support the
PSSession. You can use the PSSession to run a s
eries of commands,
functions, and scripts that share data.

This topic provides detailed information about


sessions and PSSessions
in Windows PowerShell. For basic information ab
out the tasks that you
can perform with sessions, see about_PSSessions
.

ABOUT SESSIONS

Technically, a session is an execution environm


ent in which Windows
PowerShell runs. Each session includes an insta
nce of the
System.Management.Automation engine and a host
program in which Windows
PowerShell runs. The host can be the familiar W
indows PowerShell console
or another program that runs commands, such as
Cmd.exe, or a program built
to host Windows PowerShell, such as Windows Pow
erShell Integrated Scripting
Environment (ISE). From a Windows perspective,
a session is a Windows
process on the target computer.

Each session is configured independently. It in


cludes its own properties,
its own execution policy, and its own profiles.
The environment that exists
when the session is created persists for its li
fetime even if you change
the environment on the computer. All sessions a
re created in a global
scope, even sessions that you create in a scrip
t.

You can run only one command (or command pipeli


ne) in a session at one
time. A second command run synchronously (one a
t a time) waits up to four
minutes for the first command to be completed.
A second command run
asynchronously (concurrently) fails.

ABOUT PSSESSIONS

A session is created each time that you start W


indows PowerShell. And,
Windows PowerShell creates temporary sessions t
o run individual commands.
However, you can also create sessions (called "
Windows PowerShell sessions"
or "PSSessions") that you control and manage.
PSSessions are critical to remote commands. If
you use the ComputerName
parameter of the Invoke-Command or Enter-PSSess
ion cmdlets, Windows
PowerShell establishes a temporary session to r
un the command and then
closes the session as soon as the command or th
e interactive session
is complete.

However, if you use the New-PSSession cmdlet to


create a PSSession, Windows
PowerShell establishes a persistent session on
the remote computer in which
you can run multiple commands or interactive se
ssions. The PSSessions that
you create remain open and available for use un
til you delete them or until
you close the session in which they were create
d.

When you create a PSSession on a remote compute


r, the system creates a
PowerShell process on the remote computer and e
stablishes a connection
from the local computer to the process on the r
emote computer. When you
create a PSSession on the local computer, both
the new process and the
connections are created on the local computer.

WHEN DO I NEED A PSSESSION?

The Invoke-Command and Enter-PSSession cmdlets


have both ComputerName and
Session parameters. You can use either to run a
remote command.

Use the ComputerName parameter to run a single


command or a series of
unrelated commands on one or many computers.

To run commands that share data, you need a per


sistent connection to the
remote computer. In that case, create a PSSessi
on, and then use the Session
parameter to run commands in the PSSession.

Many other cmdlets that get data from remote co


mputers, such as
Get-Process, Get-Service, Get-EventLog, and Get
-WmiObject have only a
ComputerName parameter. They use technologies o
ther than Windows PowerShell
remoting to gather data remotely. These cmdlets
do not have a Session
parameter, but you can use the Invoke-Command c
mdlet to run these commands
in a PSSession.

HOW DO I CREATE A PSSESSION?

To create a PSSession, use the New-PSSession cm


dlet. You can use
New-PSSession to create a PSSession on a local
or remote computer.

CAN I CREATE A PSSESSION ON ANY COMPUTER?

To create a PSSession that is connected to a re


mote computer, the computer
must be configured for remoting in Windows Powe
rShell. The current user
must be a member of the Administrators group on
the remote computer, or
the current user must be able to supply the cre
dentials of a member of
the Administrators group. For more information,

see about_Remote_Requirements.
CAN I SEE THE PSSESSIONS THAT OTHERS HAVE CREATED
ON MY COMPUTER?

No. You can get and manage only the PSSessions


that you have created in the
current session. You cannot see PSSessions that
others have created, even
if they run commands on the local computer.

CAN I RUN A BACKGROUND JOB IN A PSSESSION?

Yes. A background job is a command that runs as


ynchronously in the
background without interacting with the current
session. When you submit
a command to start a job, the command returns a
job object, but the job
continues to run in the background until it is
complete.

To start a background job on a local computer,


use the Start-Job command.
You can run the background job in a temporary c
onnection (by using the
ComputerName parameter) or in a PSSession (by u
sing the Session parameter).

To start a background job on a remote computer,


use the Invoke-Command
cmdlet with its AsJob parameter, or use the Inv
oke-Command cmdlet to run a
Start-Job command on a remote computer. When us
ing the AsJob parameter,
you can use the ComputerName or Session paramet
ers.

When using Invoke-Command to run a Start-Job co


mmand, you must run the
command in a PSSession. If you use the Computer
Name parameter, Windows
PowerShell ends the connection when the job obj
ect returns, and the job is
interrupted.

For more information, see about_Jobs.

CAN I RUN AN INTERACTIVE SESSION?

Yes. To start an interactive session with a rem


ote computer, use the
Enter-PSSession cmdlet. In an interactive sessi
on, the commands that you
type run on the remote computer, just as if you
typed them directly on the
remote computer.

You can run an interactive session in a tempora


ry session (by using the
ComputerName parameter) or in a PSSession (by u
sing the Session parameter).
If you use a PSSession, the PSSession retains t
he data from previous
commands, and the PSSession retains any data ge
nerated during the
interactive session for use in later commands.

When you end the interactive session, the PSSes


sion remains open and
available for use.

For more information, see Enter-PSSession and E


xit-PSSession.

MUST I DELETE THE PSSESSIONS?

Yes. A PSSession is a process, which is a self-


contained environment that
uses memory and other resources even when you a
re not using it. When you are
finished with a PSSession, delete it. If you cr
eate multiple PSSessions,
close the ones that you are not using, and main
tain only the ones currently
in use.

To delete PSSessions, use the Remove-PSSession


cmdlet. It deletes the
PSSessions and releases all of the resources th
at they were using.

You can also use the TimeOut parameter of New-P


SSession to close an idle
PSSession after an interval that you specify. F
or more information,
see New-PSSession.

If you do not delete the PSSession or set a tim


e-out, the PSSession remains
open and available for use until you close it,
until you close the session
in which it was created, or until you exit Wind
ows PowerShell. However, a
PSSession on a remote computer will be disconne
cted if the remote computer
does not respond for four minutes. (The remote
computer is configured to
send a heartbeat pulse every three minutes.)

If you save a PSSession object in a variable an


d then delete the PSSession
or let it time out, the variable still contains
the PSSession object, but
the PSSession is not active and cannot be used
or repaired.

ARE ALL SESSIONS AND PSSESSIONS ALIKE?

No. Developers can create custom sessions that


include only selected
providers and cmdlets. If a command works in on
e session but not in
another, it might be because the session is res
tricted.

SEE ALSO
about_Jobs
about_PSSessions
about_Remote
about_Remote_Requirements
Invoke-Command
New-PSSession
Get-PSSession
Remove-PSSession
Enter-PSSession
Exit-PSSession

S-ar putea să vă placă și