[18] PowerShell – powłoka linii poleceń

19 marca 2022 Wyłączono przez Adam [zicherka] Nogły

Zainstaluj Microsoft PowerShell dla systemu Linux.

Zapoznaj się ze szczegółami dotyczącymi programu PowerShell dla systemu Linux poniżej. https://github.com/PowerShell/PowerShell.

[1] Zainstaluj Microsoft PowerShell.

[root@vlsr01 ~]# curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/microsoft.repo
[root@vlsr01 ~]# dnf install powershell

[2] Podstawowe użycie i polecenia PowerShell.

# uruchom PowerShell
[root@vlsr01 ~]# pwsh
PowerShell 7.2.2
Copyright (c) Microsoft Corporation.
https://aka.ms/powershell
Type 'help' to get help.
# pokaż polecenia Cmdlet (wyświetla 10 pierwszych)
PS /root> (Get-Command)[0..9]
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        cd..
Function        cd\
Function        Clear-Host
Function        Compress-Archive                                   1.2.5      Microsof…
Function        Expand-Archive                                     1.2.5      Microsof…
Function        Find-Command                                       2.2.5      PowerShe…
Function        Find-DSCResource                                   2.2.5      PowerShe…
Function        Find-Module                                        2.2.5      PowerShe…
Function        Find-RoleCapability                                2.2.5      PowerShe…
Function        Find-Script                                        2.2.5      PowerShe…

# pokaż aktualną ścieżkę
PS /root> pwd
Path
----
/root

# zmień katalog na /home
PS /root> cd /home

# wróć do katalogu domowego
PS /home> cd
PS /root> dir
# wyświetl pliki w bieżącym katalogu (dir równa się Get-ChildItem)
    Directory: /root
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
-rw------- root             root               17.03.2022 19:21           1210 anaconda
                                                                               -ks.cfg
-rw-r--r-- root             root               18.03.2022 18:25        3040533 redmine-
                                                                               4.2.4.ta
                                                                               r.gz

# pokaż pliki/katalogi w /
PS /root> Get-ChildItem /
    Directory: /
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
lrwxrwxrwx root             root               22.06.2021 07:06              7 bin -> u
                                                                               sr/bin
dr-xr-xr-x root             root               17.03.2022 19:19           4096 boot
drwxr-xr-x root             root               18.03.2022 18:56           3020 dev
drwxr-xr-x root             root               18.03.2022 19:45           8192 etc
drwxr-xr-x root             root               17.03.2022 19:20             20 home
lrwxrwxrwx root             root               22.06.2021 07:06              7 lib -> u
                                                                               sr/lib
lrwxrwxrwx root             root               22.06.2021 07:06              9 lib64 ->
                                                                               usr/lib
                                                                               64
drwxr-xr-x root             root               22.06.2021 07:06              6 media
drwxr-xr-x root             root               22.06.2021 07:06              6 mnt
drwxr-xr-x root             root               18.03.2022 19:45             23 opt
dr-xr-xr-x root             root               18.03.2022 18:55              0 proc
dr-xr-x--- root             root               18.03.2022 18:50            233 root
drwxr-xr-x root             root               18.03.2022 18:56            860 run
lrwxrwxrwx root             root               22.06.2021 07:06              8 sbin ->
                                                                               usr/sbin
drwxr-xr-x root             root               22.06.2021 07:06              6 srv
dr-xr-xr-x root             root               18.03.2022 18:55              0 sys
drwxrwxrwt root             root               18.03.2022 19:46           4096 tmp
drwxr-xr-x root             root               17.03.2022 19:15            144 usr
drwxr-xr-x root             root               18.03.2022 18:01           4096 var

# stwórz nowy plik w bieżącym katalogu
PS /root> New-Item -Path test.txt
    Directory: /root
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
-rw-r--r-- root             root               18.03.2022 19:51              0 test.txt
PS /root> dir
    Directory: /root
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
-rw------- root             root               17.03.2022 19:21           1210 anaconda
                                                                               -ks.cfg
-rw-r--r-- root             root               18.03.2022 18:25        3040533 redmine-
                                                                               4.2.4.ta
                                                                               r.gz
-rw-r--r-- root             root               18.03.2022 19:51              0 test.txt

# stwórz nowy katalog w bieżącej lokalizacji
PS /root> New-Item -ItemType Directory -Path testdir
    Directory: /root
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
drwxr-xr-x root             root               18.03.2022 19:51              6 testdir
PS /root> dir
    Directory: /root
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
drwxr-xr-x root             root               18.03.2022 19:51              6 testdir
-rw------- root             root               17.03.2022 19:21           1210 anaconda
                                                                               -ks.cfg
-rw-r--r-- root             root               18.03.2022 18:25        3040533 redmine-
                                                                               4.2.4.ta
                                                                               r.gz
-rw-r--r-- root             root               18.03.2022 19:51              0 test.txt

# przekieruj [echo] do pliku
PS /root> echo "testowy tekst w pliku" >> test.txt
# pokaż zawartość pliku
PS /root> Get-Content test.txt
testowy tekst w pliku

# przenieś/zmień nazwę pliku
PS /root> Move-Item test.txt test1.txt
PS /root> dir
    Directory: /root
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
drwxr-xr-x root             root               18.03.2022 19:51              6 testdir
-rw------- root             root               17.03.2022 19:21           1210 anaconda
                                                                               -ks.cfg
-rw-r--r-- root             root               18.03.2022 18:25        3040533 redmine-
                                                                               4.2.4.ta
                                                                               r.gz
-rw-r--r-- root             root               18.03.2022 19:54             22 test1.tx
                                                                               t
# skopiuj plik
PS /root> Copy-Item test1.txt test2.txt
PS /root> dir
    Directory: /root
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
drwxr-xr-x root             root               18.03.2022 19:51              6 testdir
-rw------- root             root               17.03.2022 19:21           1210 anaconda
                                                                               -ks.cfg
-rw-r--r-- root             root               18.03.2022 18:25        3040533 redmine-
                                                                               4.2.4.ta
                                                                               r.gz
-rw-r--r-- root             root               18.03.2022 19:54             22 test1.tx
                                                                               t
-rw-r--r-- root             root               18.03.2022 19:54             22 test2.tx
                                                                               t

# skopiuj katalog rekurencyjnie
PS /root> Copy-Item testdir testdir2 -Recurse
PS /root> dir
    Directory: /root
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
drwxr-xr-x root             root               18.03.2022 19:51              6 testdir
drwxr-xr-x root             root               18.03.2022 19:55              6 testdir2
-rw------- root             root               17.03.2022 19:21           1210 anaconda
                                                                               -ks.cfg
-rw-r--r-- root             root               18.03.2022 18:25        3040533 redmine-
                                                                               4.2.4.ta
                                                                               r.gz
-rw-r--r-- root             root               18.03.2022 19:54             22 test1.tx
                                                                               t
-rw-r--r-- root             root               18.03.2022 19:54             22 test2.tx
                                                                               t

# skasuj plik
PS /root> Remove-Item test2.txt
PS /root> dir
    Directory: /root
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
drwxr-xr-x root             root               18.03.2022 19:51              6 testdir
drwxr-xr-x root             root               18.03.2022 19:55              6 testdir2
-rw------- root             root               17.03.2022 19:21           1210 anaconda-ks
                                                                               .cfg
-rw-r--r-- root             root               18.03.2022 18:25        3040533 redmine-4.2
                                                                               .4.tar.gz
-rw-r--r-- root             root               18.03.2022 19:54             22 test1.txt

# skasuj katalog rekurencyjnie
PS /root> Remove-Item testdir2 -Recurse
PS /root> dir
    Directory: /root
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
drwxr-xr-x root             root               18.03.2022 19:51              6 testdir
-rw------- root             root               17.03.2022 19:21           1210 anaconda-ks
                                                                               .cfg
-rw-r--r-- root             root               18.03.2022 18:25        3040533 redmine-4.2
                                                                               .4.tar.gz
-rw-r--r-- root             root               18.03.2022 19:54             22 test1.txt

# wyszukaj pliki zawierające [.txt] w swojej nazwie w bieżącym katalogu
PS /root> Get-ChildItem "*.txt" -Recurse
    Directory: /root
UnixMode   User             Group                 LastWriteTime           Size Name
--------   ----             -----                 -------------           ---- ----
-rw-r--r-- root             root               18.03.2022 19:54             22 test1.txt

# wyszukaj słowo [tekst] w pliku [test1.txt]
PS /root> Select-String -Pattern "tekst" test1.txt
test1.txt:1:testowy tekst w pliku

# pokaż pomoc na temat Cmdlet
PS /root> Get-Help Get-Content
NAME
    Get-Content
SYNTAX
    Get-Content [-Path] <string[]> [-ReadCount <long>] [-TotalCount <long>] [-Tail <int>]
    [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential <
    pscredential>] [-Delimiter <string>] [-Wait] [-Raw] [-Encoding <Encoding>] [-AsByteStr
    eam] [<CommonParameters>]
    Get-Content -LiteralPath <string[]> [-ReadCount <long>] [-TotalCount <long>] [-Tail <i
    nt>] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credent
    ial <pscredential>] [-Delimiter <string>] [-Wait] [-Raw] [-Encoding <Encoding>] [-AsBy
    teStream] [<CommonParameters>]
. . . . .

# podłącz do innego hosta za pomocą SSH
# Windows 10 – skonfiguruj SSH
PS /root> ssh user01@192.168.100.182
The authenticity of host '192.168.100.182 (192.168.100.182)' can't be established.
ECDSA key fingerprint is SHA256:HW47SIBGIxYRjyFDd43EWzAkC/ZP0KNFccS+NA6JtH8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.100.182' (ECDSA) to the list of known hosts.
user01@192.168.100.182's password:
Microsoft Windows [Version 10.0.19044.1526]
(c) Microsoft Corporation. Wszelkie prawa zastrzeżone.
user01@DESKTOP-B4MC8V5 C:\Users\user01>dir
 Volume in drive C has no label.
 Volume Serial Number is 525A-E620
 Directory of C:\Users\user01
06.02.2022  11:08    <DIR>          .
06.02.2022  11:08    <DIR>          ..
28.02.2021  18:17    <DIR>          3D Objects
28.02.2021  18:17    <DIR>          Contacts
02.02.2022  15:44    <DIR>          Desktop
30.01.2022  17:07    <DIR>          Documents
22.02.2022  16:46    <DIR>          Downloads
28.02.2021  18:17    <DIR>          Favorites
28.02.2021  18:17    <DIR>          Links
28.02.2021  18:17    <DIR>          Music
28.02.2021  18:20    <DIR>          OneDrive
28.02.2021  18:19    <DIR>          Pictures
28.02.2021  18:17    <DIR>          Saved Games
28.02.2021  18:19    <DIR>          Searches
30.01.2022  13:22    <DIR>          Videos
               0 File(s)              0 bytes
              15 Dir(s)  22 378 889 216 bytes free
user01@DESKTOP-B4MC8V5 C:\Users\user01>