psobject

Powershell - how edit existing property in custom object

时光怂恿深爱的人放手 提交于 2019-12-08 03:51:01
问题 I looking for way how update noteproperty in existing psobject, for example I have system.array of psobjects ($a): Group Assigment ----- --------- Group1 Home Group2 Office Question is how update 'Home' to something other. $a | gm: TypeName: System.Management.Automation.PSCustomObject Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() Assigment

Property passed to Invoke-Command changes type from IDictionary to HashTable

安稳与你 提交于 2019-12-05 04:03:24
问题 I've been getting an error running Invoke-Command where the script block takes a parameter of type dictionary: Cannot process argument transformation on parameter 'dictionary'. Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type "System.Collections.Generic.IDictionary`2[System.String,System.String]". At line:7 char:1 + Invoke-Command -ComputerName . -ArgumentList $dictionary -ScriptBlock ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Why does Get-Date seem to return DateTime objects, but the BinarySerializer indicate it returns a PSObject?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 03:27:06
问题 Take the simple HashTable: $data = @{ First = 'Justin'; Last = 'Dearing'; StartDate = Get-Date '2002-03-23'; } The key StartDate seems to contain a DateTime. C:\Users\zippy\Documents> $data.StartDate.GetType().FullName System.DateTime However, if I attempt to perform binary serialization on it, I get an exception complaining that PSObject is not serializable. $ms = New-Object System.IO.MemoryStream $bf = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter $bf.Serialize(

Property passed to Invoke-Command changes type from IDictionary to HashTable

跟風遠走 提交于 2019-12-03 20:45:52
I've been getting an error running Invoke-Command where the script block takes a parameter of type dictionary: Cannot process argument transformation on parameter 'dictionary'. Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type "System.Collections.Generic.IDictionary`2[System.String,System.String]". At line:7 char:1 + Invoke-Command -ComputerName . -ArgumentList $dictionary -ScriptBlock ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [], ParameterBindin...mationException +

netsh result to powershell object

流过昼夜 提交于 2019-12-02 15:32:34
问题 I am trying to work with netsh from powershell . I want see result from this command such as object, but netsh return string: netsh wlan show hostednetwork | Get-Member TypeName: System.String ... My script must work on system with rather localization, and I can't use -match for parse string to object directly. How I can solve my trouble? 回答1: You got a few alternatives, none of which are nice. 1) Read the netsh output into a string[] and use a custom record parser to create your own object.

Is it possible to attach an event to a PSObject?

允我心安 提交于 2019-12-02 01:59:13
Say I've a psobject like this : $o=New-Object PSObject -Property @{"value"=0} Add-Member -MemberType ScriptMethod -Name "Sqrt" -Value { echo "the square root of $($this.value) is $([Math]::Round([Math]::Sqrt($this.value),2))" } -inputObject $o Is it possible to attach an event so that the method Sqrt() is executed when the value attribute change ? ie : PS>$o.value=9 will produce the square root of 9 is 3 update As per @Richard answer this is the working recipe : $o=New-Object PSObject -property @{"_val"=1} Add-Member -MemberType ScriptMethod -Name "Sqrt" -Value { write-host "the square root of

Why does Get-Date seem to return DateTime objects, but the BinarySerializer indicate it returns a PSObject?

不问归期 提交于 2019-12-01 18:22:14
Take the simple HashTable : $data = @{ First = 'Justin'; Last = 'Dearing'; StartDate = Get-Date '2002-03-23'; } The key StartDate seems to contain a DateTime . C:\Users\zippy\Documents> $data.StartDate.GetType().FullName System.DateTime However, if I attempt to perform binary serialization on it, I get an exception complaining that PSObject is not serializable. $ms = New-Object System.IO.MemoryStream $bf = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter $bf.Serialize($ms, $data) $ms.Close() Throws: DocumentsException calling "Serialize" with "2" argument(s): "Type

Unexected results in Receive-Job

ⅰ亾dé卋堺 提交于 2019-12-01 07:07:49
问题 I noticed weird behavior when using jobs in PowerShell 5.0. Running job that returns PSObject , also returns some hashtable. Jobs that return strings, integers, etc work propertly. Running Start-Job { New-Object PSObject -Property @{ A = 1 } } | Receive-Job -Wait -AutoRemoveJob returns A : 1 RunspaceId : 6921e85f-301e-4e95-8e4b-c0882fc2085f PSSourceJobInstanceId : 2992ef77-5642-4eac-8617-f26449a87801 Running Start-Job { New-Object PSObject } | Receive-Job -Wait -AutoRemoveJob returns @

Difference between PSObject, Hashtable, and PSCustomObject

冷暖自知 提交于 2019-11-30 11:24:13
问题 Can anybody explain the details? If I create an object using $var = [PSObject]@{a=1;b=2;c=3} and then I look for its type using getType() PowerShell tells me it's of type Hashtable . When using Get-Member (alias gm ) to inspect the object it's obvious that a hashtable has been created, since it has a keys and a values property. So what's the difference to a "normal" hashtable? Also, what's the advantage of using a PSCustomObject? When creating one using something like this $var =

Difference between PSObject, Hashtable, and PSCustomObject

房东的猫 提交于 2019-11-30 01:34:39
Can anybody explain the details? If I create an object using $var = [PSObject]@{a=1;b=2;c=3} and then I look for its type using getType() PowerShell tells me it's of type Hashtable . When using Get-Member (alias gm ) to inspect the object it's obvious that a hashtable has been created, since it has a keys and a values property. So what's the difference to a "normal" hashtable? Also, what's the advantage of using a PSCustomObject? When creating one using something like this $var = [PSCustomObject]@{a=1;b=2;c=3} the only visible difference to me is the different datatype of PSCustomObject . Also