-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Understanding Remoting | ||
|
||
## The Protocol | ||
|
||
PowerShell uses a protocol that you might have never heard of, the *Web Services for Management(WSMan)*, which is one of the builtin *PSProvider* on Windows. | ||
WSMan is Windows specific feature, so as PowerShell came into the cross-platform world, Microsoft decided to support SSH which is more commonly used over industries. | ||
Microsoft made effort to bring SSH to Windows world, they're maintaining [a fork of SSH in github](https://github.com/PowerShell/openssh-portable) | ||
|
||
> [!NOTE] | ||
> `openssh` is builtin since certain version of Windows 10. | ||
## Connect to Remote | ||
|
||
No matter which protocol you use with PowerShell, it has a unified cmdlet to connect to one. | ||
Of course this requires PowerShell available on both sides. | ||
|
||
- Using SSH | ||
|
||
```ps1 | ||
# login as <user> on <ip> | ||
Enter-PSSession -HostName <host> -UserName <user> -Port <port> | ||
# or use a short syntax | ||
Enter-PSSession -HostName <usr>@<host> | ||
``` | ||
|
||
- Using WSMan | ||
|
||
```ps1 | ||
# -ComputerName used instead for connection by WSMan | ||
Enter-PSSession -ComputerName <host> -UserName <user> | ||
``` | ||
|
||
Optionally, you can store the session as variable instead of entering it by `New-PSSession`. | ||
|
||
```ps1 | ||
$session = New-PSSession -HostName <host> -UserName <usr> | ||
``` | ||
|
||
## Disconnect from Remote | ||
|
||
```ps1 | ||
Exit-PSSession | ||
``` | ||
|
||
## Execute Command on Remote | ||
|
||
- Execute a same command on one or more remotes. | ||
|
||
```ps1 | ||
Invoke-Command -HostName <host1>[,<host2>, ...] -UserName <usr> -ScriptBlock { <cmd> } | ||
``` | ||
|
||
- Execute on a persistent session | ||
|
||
```ps1 | ||
$session = New-PSSession -HostName <host> -UserName <usr> | ||
icm -Session $s -ScriptBlock { <cmd> } | ||
``` | ||
|
||
> [!NOTE] | ||
> `Invoke-Command` returns deserialized object converted from transferred xml from remotes, it reflects data on remote only. | ||
> You should only aggregate format it without mutating the values. | ||
> `Enter-Session` does enter a PowerShell session, so it operates on a real PowerShell instance just like what you can do with SSH. |
46 changes: 46 additions & 0 deletions
46
docs/document/PowerShell/docs/What to Learn for New cmdlet.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# What to Learn from New cmdlet | ||
|
||
## Find Positional Parameter | ||
|
||
Positional parameter matters for understanding the examples listed on documentations since the example might elide parameter name. | ||
We can take advantages of `Get-Help` and `Select-String` to capture those parameters that have a explicit order. | ||
The `-Context` includes 3 lines above and 5 lines below the display the whole info of the parameter. | ||
|
||
```ps1 | ||
help <cmd> | sls 'Position\??\s*\d' -Context 3,5 | ||
``` | ||
|
||
The example usage is exactly what we have used here, the `Select-String`. | ||
It has two positional parameters, `-Pattern` and `-Path`. | ||
So in the previous example, the `-Pattern` was omitted. | ||
|
||
```console | ||
$ help sls | sls 'Position\??\s*\d+' -Context 3,5 | ||
|
||
-Path <string[]> | ||
|
||
Required? true | ||
> Position? 1 | ||
Accept pipeline input? true (ByPropertyName) | ||
Parameter set name File, FileRaw | ||
Aliases None | ||
Dynamic? false | ||
Accept wildcard characters? false | ||
-Pattern <string[]> | ||
|
||
Required? true | ||
> Position? 0 | ||
Accept pipeline input? false | ||
Parameter set name (All) | ||
Aliases None | ||
Dynamic? false | ||
Accept wildcard characters? false | ||
``` | ||
|
||
## Find Parameters Accepts Pipeline | ||
|
||
Similar to finding position parameter. | ||
|
||
```ps1 | ||
help <cmd> | sls 'Accept pipeline input\??\s*true.*$' -Context 5,4 | ||
``` |
Empty file.
Empty file.
Empty file.