February 2019
Intermediate to advanced
626 pages
15h 51m
English
If an object is completely replaced with a made-up PSCustomObject, the object type is lost; this is important when another command requires an object of a specific type as input. Attempting to override properties and methods on a real instance of the object may be used to work around this problem.
The following snippet creates an instance of an SQL connection object, then overrides the Open method and State properties:
$sqlConnection = [System.Data.SqlClient.SqlConnection]::new()
$sqlConnection | Add-Member State -MemberType NoteProperty -Force -Value 'Closed'
$sqlConnection | Add-Member Open -MemberType ScriptMethod -Force -Value {
$this.State = 'Open'
}
The State property cannot be set by default, so overriding ...
Read now
Unlock full access