$array = 1..10 foreach ($a in $array) {Write-output $a} foreach ($a in $array) { Write-output $a } $sum = 0 foreach ($a in $array) { Write-output $a $sum = $sum + $a } Write-Output "The sum of `$array is $sum." Get-ChildItem | ForEach-Object { $_.name $_.CreationTime } Get-ChildItem -Path ~\Documents\CST1861\Week\14 | ForEach-Object { Write-Output "$($_.name) was created on $($_.CreationTime)" } $array | ForEach-Object { Write-Output $_ } $n=1 While ($n -le 10){Write-Output $n; $n++ } $n=1 While ($n -le 10){ Write-Output $n $n++ } $date = get-date do { Write-Output "Checking if the month is December" $date = $date.AddMonths(1) } while ($date.Month -ne 12 ) $date = get-date $date = $date.AddMonths(1) $date do { Write-Output "Checking if the month is December" $date = $date.AddMonths(1) } while ($date.Month -ne 12 ) $n=1 do { Write-Output $n; $n++ } while ($n -le 10) $items = Get-ChildItem -Path ~\Documents\CST1861\Week\13 foreach ($i in $items){Write-Output "The character length of $i is "($i).Length} start-process notepad $Process = "notepad" do { Write-Host "$Process is open" } while ((get-process).name -contains "notepad") The Big Book of PowerShell Error Handling get-help about_automatic_variables > about_automatic_variables.txt Get-Service -Name Foo,BITS,Nobody,WinRM -ErrorAction Continue Get-Service -Name Foo,BITS,Nobody,WinRM -ErrorAction Stop Get-Service -Name BITS,Foo,Nobody,WinRM -ErrorAction Stop Get-Service -Name BITS,Foo,Nobody,WinRM -ErrorAction SilentlyContinue $computer = 'Srv01' Write-Verbose "Connecting to $computer" $session = New-PSSession -ComputerName $computer -ErrorAction Stop $computer = 'DESKTOP-46AVB2C' $computers = 'localhost','Srv01','ooga' foreach ($Computer in $computers) { Write-Verbose "Connecting to $computer" $session = New-PSSession -ComputerName $Computer -ErrorAction Stop } $session = New-PSSession -ComputerName $computers -ErrorVariable +a try { blahfoo } catch { Write-Warning "Warning: An error occurred." } $a = '' try { $session = New-PSSession -ComputerName $computer -ErrorVariable +a } catch { Write-Warning "Warning: An error occurred."; Write-Output $a } $cmderrors = '' $computers | ForEach-Object { try { Write-Output $_ $session = New-PSSession -ComputerName $_ -ErrorAction SilentlyContinue -ErrorVariable +cmderrors } catch { Write-Warning "Warning: An error occurred, look at `$cmderrors for a list."; } } try { Write-Output $_ $session = New-PSSession -ComputerName $_ -ErrorAction SilentlyContinue -ErrorVariable +cmderrors } catch { Write-Warning "Warning: An error occurred, look at `$cmderrors for a list."; } $cmderrors = '' $session = New-PSSession -ComputerName $computers -ErrorAction SilentlyContinue -ErrorVariable +cmderrors if ( $cmderrors -ne '' ) { Write-Warning "Warning: An error occurred, look at `$cmderrors for a list."; } try { $session = New-PSSession -ComputerName $computer } catch { Write-Warning "Warning: An error occurred." } Function Get-PCUpTime { param ( [string[]]$ComputerName = 'localhost' ) try { foreach ($computer in $computerName) { If ($computer -eq "localhost") { Get-Uptime } Else { Invoke-command -ComputerName $computer -ScriptBlock { Get-Uptime } -ErrorAction Stop} } } catch { Write-Error "Cannot connect To $computer" } }