Quali sono gli usi comuni di Windows PowerShell?

Il mio pollice in su: Powershell è uno dei miei linguaggi di scripting di shell preferiti. Probabilmente riceverò un sacco di critiche da quelli di Linux/bash/perl/python, ma Powershell ottiene punti per:

  • Leggibilità: si può avere un codice conciso che sia leggibile e usare parole come Where, Select, Replace contro parole come sed, awk, tr
  • Librerie: PowerShell Gallery permette di installare moduli
  • Dati: Tratta nativamente i tipi di dati JSON e XML (YAML disponibile come modulo)
  • Crea funzioni ben definite

Prima di tutto per rispondere alla tua domanda. L'uso principale di powershell è quello di applicare modifiche al vostro sistema senza una GUI. Questo permette di eseguire l'automazione da script, in modo da non dover ricordare la sequenza di point & click per creare lo stato desiderato.

Degno di nota è anche l'ultimo gusto, Powershell DSC (Desired State Configuration). This is picking up on configuration management systems like Chef / Puppet / Ansible by using a configuration file to set the state of the machine.

Some Powershell 101:

  1. # if you are running configuration scripts on your system, 
  2. # you usually want to Run as Administrator 
  3.  
  4. # by default, your system doesn't allow you to run Powershell scripts, 
  5. # this command opens that up 
  6. Set-ExecutionPolicy Bypass ; 
  7.  
  8. # this command sets it back 
  9. Set-ExecutionPolicy Restricted ; 
  10.  
  11. # my favourite command, installing the package manager called Chocolatey, 
  12. # you can pretty much find every popular utility available for Windows here 
  13. # https://chocolatey.org/install 
  14.  
  15. # Install Chocolatey: Don't have Windows 10? (Powershell 1/2) 
  16. iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) ; 
  17.  
  18. # Install Chocolatey: Windows 10 (Powershell 3+) 
  19. iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex ; 
  20.  
  21. # my favourite chocolatey installs 
  22. # find more packages here: https://chocolatey.org/packages 
  23.  
  24. choco install -y powershell ; # make sure powershell is up to date 
  25. choco install -y @( 
  26. "PSWindowsUpdate", 
  27. "visualstudiocode", 
  28. "googlechrome", 
  29. "openssh", 
  30. "putty", 
  31. "vagrant", 
  32. "virtualbox" 
  33. ) ; # or install more than one using an array 
  34.  
  35. # Windows Update from the command-line 
  36. Import-Module PSWindowsUpdate ; # notice above we've installed the package 
  37. Get-WUInstall -AcceptAll ; 
  38.  
  39. # need to do a simple curl? 
  40. # e.g. Save etsy's Code as Craft logo as a file. 
  41. # https://codeascraft.com/wp-content/uploads/2014/12/codeascraft_black_on_white.png 
  42. iwr "https://codeascraft.com/wp-content/uploads/2014/12/codeascraft_black_on_white.png" -outfile "codeascraft.png" ; 
  43.  
  44. # read some JSON data? 
  45. # e.g. People in Space right now 
  46. # https://github.com/jdorfman/awesome-json-datasets#nasa 
  47. $url = "http://api.open-notify.org/astros.json" 
  48.  
  49. # get entire JSON and convert to Object[] 
  50. $data = iwr $url | ConvertFrom-JSON ;  
  51.  
  52. # displays all the people on the ISS right now 
  53. $data.people | Where { $_.craft -eq "ISS" } ; 

Another plus is having access to anything in .NET, which makes Powershell extremely powerful. Additionally, to vex the Linux folks even more, PowerShell is open sourced and is available on Linux 🙂