Powershell: Difference between revisions
From Leo's Notes
This page was last edited on 17 April 2017, at 20:32.
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
== Quick Overview == | |||
=== Syntax === | |||
==== Comments ==== | |||
{{highlight|lang=powershell|code= | |||
<# | |||
multi-line comments | |||
#> | |||
}} | |||
== Get machine UIUD == | ==== Data Containers ==== | ||
These are things in powershell that can contain data values or other objects. | |||
===== Objects ===== | |||
{{highlight|lang=powershell|code= | |||
$a = New-Object –TypeNamePSObject | |||
$a | Add-Member –MemberTypeNoteProperty –Name property –Value 'hello' | |||
}} | |||
Arrays | |||
{{highlight|lang=powershell|code= | |||
$a = @() | |||
$a += "Item" | |||
}} | |||
Hash Tables (aka. a Dictionary) | |||
{{highlight|lang=powershell|code= | |||
$a = @{} | |||
$a.Add("Key", "Value"); | |||
}} | |||
==== Parameters ==== | |||
{{highlight|lang=powershell|code= | |||
param ( | |||
?? | |||
) | |||
}} | |||
=== Common Tasks === | |||
==== Filtering ==== | |||
Pipe to {{code|Where-Object}} and provide the matching condition. | |||
{{highlight|lang=powershell|code= | |||
Where-Object {$_.RecordData.IPv4Address -like "172.20.1.*" } | |||
}} | |||
== Other Stuff == | |||
=== Get machine UIUD === | |||
get-wmiobject Win32_ComputerSystemProduct | Select-Object -ExpandProperty UUID | get-wmiobject Win32_ComputerSystemProduct | Select-Object -ExpandProperty UUID | ||
Revision as of 20:32, 17 April 2017
Quick Overview
Syntax
Comments
<#
multi-line comments
#>
Data Containers
These are things in powershell that can contain data values or other objects.
Objects
$a = New-Object –TypeNamePSObject
$a
Arrays
$a = @()
$a += "Item"
Hash Tables (aka. a Dictionary)
$a = @{}
$a.Add("Key", "Value");
Parameters
param (
??
)
Common Tasks
Filtering
Pipe to Where-Object and provide the matching condition.
Where-Object {$_.RecordData.IPv4Address -like "172.20.1.*" }
Other Stuff
Get machine UIUD
get-wmiobject Win32_ComputerSystemProduct | Select-Object -ExpandProperty UUID
Troubleshooting
Error: "File cannot be loaded because the execution of scripts is disabled on this system"
To allow untrusted script execution, open a powershell instance (same arch as what you're trying to run the script as. ie: 64bit/32bit) as Administrator and run:
PS> set-executionpolicy unrestricted
You should now be able to see:
PS> get-executionpolicy Unrestricted