Ricerca nel sito web

Come installare e utilizzare PowerShell su Ubuntu 20.04


Su questa pagina

  1. Prerequisiti
  2. Per iniziare
  3. Installa PowerShell utilizzando Snap
  4. Installa PowerShell dal repository Ubuntu
  5. Come utilizzare PowerShell
  6. Conclusione

PowerShell è uno strumento di configurazione basato su attività del sistema operativo Windows. È uno strumento molto potente utilizzato per automatizzare le attività dell'amministratore di sistema. Microsoft ha rilasciato PowerShell Core per i sistemi operativi Linux. Con PowerShell, puoi eseguire diverse attività amministrative su un computer remoto e uno locale.

In questo tutorial, ti mostreremo come installare PowerShell su Ubuntu 20.04.

Prerequisiti

  • Un server che esegue Ubuntu 20.04.
  • Il server è configurato con una password di root.

Iniziare

Prima di iniziare, dovrai aggiornare i tuoi pacchetti di sistema all'ultima versione. Puoi aggiornarli usando il seguente comando:

apt-get update -y

Una volta aggiornato il server, puoi procedere al passaggio successivo.

Installa PowerShell usando Snap

Esistono due modi per installare PowerShell su Ubuntu 20.04. In questa sezione, ti mostreremo come installare PowerShell con snapd.

Innanzitutto, installa il gestore di pacchetti Snap con il seguente comando:

apt-get install snap snapd -y

Una volta installato, puoi installare PowerShell con il seguente comando:

snap install powershell --classic

Una volta installato PowerShell, connettiti a PowerShell con il seguente comando:

pwsh

Dovresti ottenere il seguente output:

PowerShell 7.1.2
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /root> 

Successivamente, esci da PowerShell con il seguente comando:

PS /root> exit

Puoi disinstallare PowerShell dal tuo sistema con il seguente comando:

snap remove powershell

Installa PowerShell dal repository Ubuntu

Puoi anche installare PowerShell dal repository Ubuntu. Innanzitutto, scarica il pacchetto del repository di PowerShell con il seguente comando:

wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

Una volta completato il download, installa il pacchetto scaricato con il seguente comando:

dpkg -i packages-microsoft-prod.deb

Successivamente, aggiorna il repository e installa PowerShell con il seguente comando:

apt-get update -y
apt-get install powershell -y

Una volta completata l'installazione, connettersi a PowerShell con il seguente comando:

pwsh

Dovresti ottenere il seguente output:

PowerShell 7.1.2
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /root> 

Come usare PowerShell

In questa sezione, ti mostreremo come utilizzare la riga di comando di PowerShell su Linux.

Per elencare la directory, eseguire il seguente comando:

PS /root> dir

Dovresti ottenere il seguente output:

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           2/23/2021  1:48 PM                snap
-----            1/7/2021  2:45 AM      162406548 nexus-3.29.2-02-unix.tar.gz
-----           4/23/2020  7:02 PM           3124 packages-microsoft-prod.deb

Per ottenere informazioni dettagliate su PowerShell, eseguire il seguente comando:

PS /root> Get-Host

Dovresti ottenere il seguente output:

Name             : ConsoleHost
Version          : 7.1.2
InstanceId       : 65ee7aa5-320c-478d-9337-d8642073a26a
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

Per stampare tutta la cronologia dei comandi di PowerShell, eseguire il comando seguente:

PS /root> Get-History

Dovresti ottenere il seguente output:

  Id     Duration CommandLine
  --     -------- -----------
   1        0.026 ls -l
   2        0.069 dir
   3        0.027 dir /
   4        0.004 dir
   5        0.139 Get-Aliasias cd
   6        0.006 cd
   7        0.004 Set-Location
   8        0.042 Get-Aliasias cd
   9        0.110 Get-Process
  10        0.050 Get-Host

Per ottenere tutte le informazioni sul processo, eseguire il comando seguente:

PS /root> Get-Process

Dovresti ottenere il seguente output:

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
      0     0.00       3.89       0.00    7605 …03 (sd-pam)
      0     0.00       0.00       0.00     137   0 acpi_thermal_pm
      0     0.00       1.96       0.00     517 517 agetty
      0     0.00       1.79       0.00     520 520 agetty
      0     0.00       0.00       0.00     127   0 ata_sff
      0     0.00       3.76       0.04    7628 …28 bash
      0     0.00       3.88       0.00   10316 …28 bash
      0     0.00       3.85       0.03   10327 …28 bash
      0     0.00       3.69       0.00   10934 …28 bash
      0     0.00       0.00       0.00     125   0 blkcg_punt_bio
      0     0.00       0.00       0.00     175   0 charger_manager
      0     0.00       0.00       0.00      14   0 cpuhp/0
      0     0.00       0.00       0.00      15   0 cpuhp/1
      0     0.00       2.80       0.01     327 327 cron

Per stampare le informazioni della guida, eseguire il seguente comando:

PS /root> help

Puoi anche utilizzare il comando Get-Help per ottenere assistenza sul comando desiderato.

Ad esempio, per ottenere assistenza sul comando Set-Location utilizzare il seguente comando:

PS /root> Get-Help Set-Location

Se desideri rimuovere PowerShell, esegui il seguente comando:

apt-get remove powershell -y

Conclusione

Nella guida sopra, hai imparato come installare e utilizzare PowerShell su Ubuntu 20.04. Ora puoi scrivere lo script di PowerShell per automatizzare le attività amministrative quotidiane. Non esitate a chiedermi se avete domande.