- The "processing" script block in this example will create new registry key and then add a value and it's data
- read from the CSV file with the columns in the top row treated as the item properties
- loop through each item read and call the Invoke-Command to run a script block in $sb with ComputerName parameter taking on the
value of the ComputerName property for each row.
To output the result in an Interactive Table (grid view)
To define a script block with parameter and execute it passing a paramter
$sb = {
Param ($LogName)
Get-WinEvent -LogName $LogName -MaxEvents 20
}
# run it on the local computer by .. &$sb System
# run it on remote computers by
Invoke-Command -ComputerName Server1,Server2,Server3 $sb -ArgumentList System
To enter a start a remote PowerShell session interactively
Enter-PSSession -ComputerName remoteComputerName
..... perform your tasks here
Exit-PSSession
To overcome restriction of RemoteSigned execution policy for scripts copied from remote location
Either
1. Use windows explorer, explorer the file, right click, select properties, click "Unblock" button
2. run powershell cmdlet
Unblock-File -Path path-to-script-file
To display the object property directy e.g. show IPv4 address on the local computer
Capture the property value of an cmdlet output as a variable
$state = (get-website -name mywebsite).State
use normal bracket ().property
How perform a task in a loop until a specific time using Do .. Until
$TimeStart = Get-Date
$TimeEnd = $timeStart.addminutes(10)
Write-Host "Start Time: $TimeStart"
write-host "End Time: $TimeEnd"
Do
{
$TimeNow = Get-Date
if ($TimeNow -ge $TimeEnd)
{
Write-host "It's time to finish."
}
else
{
Write-Host "Not done yet, it's only $TimeNow"
}
Start-Sleep -Seconds 10
}
Until ($TimeNow -ge $TimeEnd)
How to use new-object cmdlet to create a single data object with properties populated with values from several variables. Allows use of pipelines for that object
index is the index to the element in the array after -f
alignment is - for left justified and unsigned for right justified followed by the number of character spaces to occupy
formatStrings:
:c
Currency format
:e
Scientific (exp) notation
:f
Fixed point
:f5 = fix to 5 places
:g
Most compact format, fixed or sci
:g5 = 5 significant digits
:n
Number (:nP precision=number of decimal places), includes culture separator for thousands 1,000.00
:p
percentage
:r
reversible precision
:x
Hex format
:hh
:mm
:ss
Convert a DateTime to a 2 digit Hour/minute/second
"{0:hh}:{0:mm}"
:ddd
Convert a DateTime to Day of the Week
eg.1
PS C:\> $myarray = 'John','Doe'
PS C:\> "First name is : {1}, Last name is : {0}" -f $myarray
First name is : Doe, Last name is : John
eg. 2 - Show 1 decimal place
PS C:\> "result = {0:N1}" -f 123.456
result = 123.5
e.g. 3 - display hello right justified with 10 character alignment
"{0,10}" -f "hello"
e.g. 4 - display hello left justified with 10 character alignment
"{0,-10}" -f "hello"
How to display all properties and methods of an object
e.g.
$web = New-Object Net.WebClient
$web | Get-Member
How to use the regular expression -match operator to extract maching strings
> $string = "the remote server returned an error: (401) Unauthorized."
> $string -match 'the remote server returned an error: [(](\d+)[)] Unauthorized.'
> $matches[0]
> $matches[1]
True
the remote server returned an error: (401) Unauthorized.
401
if the match is true, $matches[0] returns the entire line that matched and matches[1] etc returns the first () matched etc.