Tuesday, October 3, 2017

Lecture 2:Objects in PowerShell

This lecture / video is about objects and we run through a sample script


Script to work tthrough


Get-PSDrive | ?{$_.Free -gt 1} | %{$Count = 0; Write-Host "";} { $_.Name + ": Used: " + "{0:N2}" -f ($_.Used/1gb) + " Free: " + "{0:N2}" -f ($_.free/1gb) + " Total: " + "{0:N2}" -f (($_.Used/1gb)+($_.Free/1gb)); $Count = $Count + $_.Free;}{Write-Host"";Write-Host "Total Free Space " ("{0:N2}" -f ($Count/1gb)) -backgroundcolor magenta}


1. get-psdrives
*all the drives on local

Note: some drives are irrelevant

2. break apart script ?
*ask a question

3. Get-PSDrive | where-object {$_.Free -gt 1}
*(verb)(command)(noun)(pipe)(where object){(current object in the pipe)(greater than)(1)}
**it is passing the variable property (amount of space) into .Free then returning if greater than one

4. Get-PSDrive | where-object {$_.Free -gt 1} | select-object Root, Used, Free
**Select Object Properties


5. Get-PSDrive | where-object {$_.Free -gt 1} | ForEach-Object {"zebra"}
*runs though each object and assigns console.writeline("zebra")


6. Get-PSDrive | where-object {$_.Free -gt 1} | ForEach-Object {write-host " Free space for" $_.Free " is" $_.Root -foregroundcolor red }
* note, all text must contain "" and no commas after varibales

7.Get-PSDrive | where-object {$_.Free -gt 1} | ForEach-Object {write-host " Free space for"( $_.Free/1gb) " is" $_.Root -foregroundcolor red }
* Can divide by 1gb or 1mb

8."{0:N0}" -f 1000000000
**Note it is zero's in the formatting equation


9. "{0:N2}" -f 1000000000
*returns .00

10. "{0:c2}" -f 1000000000
*returns currency

11. "{0:p2}" -f 1000000000
*percent

12.Get-PSDrive | where-object {$_.Free -gt 1} | ForEach-Object {write-host " Free space for"("{0:N0}" -f ($_.Free/1gb)) " is" $_.Root -foregroundcolor red }
*added into statement

13. ?
*is an alias for where-object

14. %
*foreach alias

15. foreach [19:48]
foreach{$c=0; write-host "blah hblah"}{$c=$c+1;write-host"blah, blah"}{$c







No comments:

Post a Comment