Lo sapevate che è possibile interrogare (ma anche impostare) una classe del .Net Framework da Powershell in modo rapido? Eccone alcuni esempi.
Ottenere informazioni sul sistema operativo:
PS c:\>[Environment]::Osversion
Risultato Platform ServicePack Version VersionString -------- ----------- ------- ------------- Win32NT 6.2.9200.0 Microsoft Windows NT 6.2.9...
Ottenere la lista dei dischi logici del computer:
PS c:\>[Environment]::GetLogicalDrives()
Risultato
C:\ D:\
Ottenere il nome del computer:
PS c:\>[Environment]::MachineName
[NOMECOMPUTER]
Emettere un bip dal PC
PS c:\>[Console]::Beep()
[Suono del Bip del sistema]
Arrotondare un valore:
PS c:\>[Math]::Round(2.555, 2)
2,56
Elevare a potenza un valore:
PS c:\>[Math]::Pow(7,2)
49
E’ possibile anche verificare i Metodi e le Proprietà accessibili per ogni classe usando la seguente sintassi:
PS c:\>[Nome classw]|gm -s
Esempio:
PS c:\>[TimeZone]|gm -s
Name MemberType Definition ---- ---------- ---------- Equals Method static bool Equals(System.Object objA, System.Object objB) IsDaylightSavingTime Method static bool IsDaylightSavingTime(datetime time, System.Globalizatio ReferenceEquals Method static bool ReferenceEquals(System.Object objA, System.Object objB) CurrentTimeZone Property static System.TimeZone CurrentTimeZone {get;} L'elenco delle classi (.Net 4.5) lo potete reperire all'indirizzo http://msdn.microsoft.com/it-it/library/yxcx7skw.aspx
E’ possibile enumerare tutte le proprietà/metodi disponibili per una classe usando il comando:
[NomeClasse] | Get-Member -static | Where-Object {$_.MemberType -eq “Property/Method”} | Select-Object Name
Ad esempio:
[Environment] | Get-Member -static | Where-Object {$_.MemberType -eq “Property”} | Select-Object Name
Oppute vedere i metodi della classe Math:
[Math] | Get-Member -static | Where-Object {$_.MemberType -eq “Method”} | Select-Object Name