Che cos'è Powershell? Come dovrei utilizzarlo? Perchè scrivere quando posso usare la GUI?
Sessione di supporto psico-pratico per amministratori di sistema dipendenti da GUI.
By Andrea Vai
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
CCI2019 - I've got the Power! I've got the Shell!
1.
2.
3. • Introduced in 2006
• Implemented as an engine that can be embedded
in a GUI or used directly as a CLI
• Works with modules.
• PowerShell is not just a shell; it is a complete
scripting environment.
• PowerShell Core is the new generation of
PowerShell, it is open source and cross-platform
meaning it could now run on Linux, MacOS, and
Windows!
4. The ISE enables you to run commands and design, write, test,
and debug scripts in a Windows GUI
Windows
PowerShell
tabs
Script pane
Console pane Add-in
Tools pane
IntelliSense
suggestions
5. • Verb is the action the cmdlet performs:
• Get
• Set
• New
• Add
• Remove
• Noun is the resource the cmdlet affects:
• Service
• Process
• Prefixes are used to group related nouns:
• AD (Active Directory), SP (SharePoint), AzureAD
6. • Update-Help utilizes downloadable help content
to update your local Help
• No Help content is distributed with PowerShell
• Get-Help provides quick access to syntax help for
PowerShell commands
• Use –ShowWindow to display help in a floating
window
• Use –Example to quickly jump to usage examples
• Use –Online to display web-based version of help
in your system’s default web browser
7. • A PSProvider is basically a Windows PowerShell
adapter that makes some form of storage
resemble a disk drive.
• A PSDrive is an actual connection to a form of
storage.
• Run Get-PSDrive for a list
• Run New-PSDrive to map a new drive
8. • Windows PowerShell runs commands in a pipeline
• In the console, each complete command line is a
pipeline
• Pipelines can contain one or more commands,
with multiple commands separated by a vertical
pipe character (|)
• Commands execute from left to right, with the
output of each command being piped (passed) to
the command after it
• The output of the last command in the pipeline is
what appears on your screen
9. Get-ADUser –Filter * | Enable-ADAccount Enable
ADAccount
Cmdlet
Get-ADUser
Cmdlet
• The pipeline connects the output from one cmdlet to the
input of another cmdlet
• The combination of the first cmdlet, pipe, and second cmdlet
makes a pipeline
Process
Object
Pipe
11. FOREACH example
Get-ChildItem -Path C:TEMP -file | ForEach-Object LastAccessTime
SET CONTENT example
"One", "Two", "Three", "Four" | Set-Content -Path C:Temptmp.txt
MEASURE OBJECT example
Get-Content C:Temptmp.txt | Measure-Object -Character -Line –Word
List Network connections and categories
Get-NetConnectionProfile
Set network connection to “Private”
Set-NetConnectionProfile -InterfaceIndex <index number> -NetworkCategory
Private
12. List all services with dependencies on local computer
Get-Service | Where-Object {$_.dependentServices} | Format-List -Property
Name, DependentServices, @{Label="NotDependentServices";
Expression={$_.DependentServices.count}}
List last 20 Events in EventLog
Get-EventLog -Newest 20 -logname "<log name>"
List all Errors in a specified Log
Get-EventLog -LogName <logname> -EntryType Error
Get EventLog from multiple computers
Get-EventLog -LogName "Windows PowerShell" -ComputerName "localhost",
"Server01", "Server02"
List all Events containing a specified word in event description
Get-EventLog -LogName "Application" -Message "*failed*"
14. <#
-> Dettagli OU Disabilitate
-> OU Vuote
-> OU Totale
-> Utenti con Nessuna Scadenza Password
-> Links GPO
-> Dettagli GPO
-> Account Inattivi
-> Gruppi con Privilegi Elevati
-> SchemaHistory
-> Stato w32tm2
-> Configurazione w32tm1
-> DC daig
-> Oggetti Computer
#>
15. # Caricamento Modulo Powershell per Active Directory
try{
Import-Module ActiveDirectory
$Inactivedays = "60" #Inserire il numero di giorni
desiderato
#date
$date = $((Get-Date).ToString('MM-dd-yyyy_hh-mm'))
#path
$path = ".AD_Reports_$date"
if (!(Test-path $path))
{
md $path | Out-Null
}}
catch{
Write-Host “Problema Rilevato durante la creazione della
cartella: $_.Exception.Message "}
Show PS Version info
$PSVersionTable
Rilasciato inizialmente per gestire Exchange 2007 – exchange management shell
PowerShell è una vera e propria shell a riga di comando pensata e strutturata per lo scripting e l’amministrazione di sistema basata su .Net Framework....
Note:
Microsoft intendeva in origine rilasciare PowerShell insieme con Vista, ma in seguito ha definito delle date di rilascio diverse per PowerShell.
La versione 1.0 è stata rilasciata il 14 novembre 2006.
La versione 2.0 è presente di serie su Windows 7.
La versione 3.0 è di serie su Windows 8 e Windows Server 2012.
La versione 4.0 è di serie su Windows 8.1 e Windows Server 2012 R2
La versione 5.0 è di serie su Windows 10 e Windows Server 2016 TP
La versione 5.1 Windows Management Framework (WMF) 5.1 and is included in Windows Server 2016 and Windows 10 Anniversary Edition (1607) You can install Windows PowerShell 5.1 on all the operating systems that you can install Windows PowerShell 5.0 on. To do so, you can download and install the Windows Management Framework 5.1 from the Windows Download Center
Descrivere la slide ANIMATA...
Note:
I dati in una pipeline sono assegnati ad una speciale variabile denominata $PSItem (nelle versioni vecchie $_ ) che esiste unicamente fintantoche la pipeline è in esecuzione.
-Filter è un parametro obbligatorio che filtra i risultati (con * visualizza tutto)
Script per verificare forwarders email
PowerShell uses the ! character as an alias for the logical -not operator:
PowerShell interprets everything that is empty, $Null, or 0 to the Boolean $False. Bool can only have $True or $False.
By casting the value to a Boolean you can see what PowerShell interprets for each value:
[bool]0 # False [bool]1 # True [bool]"" # False [bool]"test" # True [bool]$null # False
The locical NOT operation turns each Boolean into its opposite:
!$True # Is $False !$False # Is $True ![bool]0 # True ![bool]1 # False ![bool]"" # True ![bool]"test" # False ![bool]$null # True