How to add data row to object - powershell

Below is the result of an API call via Invoke-Restmethod
And output of $test.result.organizationContext is as follows
How can I add an line item to this "organizationContext" object with values for the different attributes like " name", "id" ?

If we assume that you already have the values you want to add defined in variables, you can create a new custom object and then effectively, yet inefficiently, add it to the array.
$newOrganizationContext = [pscustomobject]#{
classificationId = $classificationId
group = $group
id = $id
isGroupSeparator = $isGroupSeparator
name = $name
objectId = $objectId
path = $path
subClass = $subClass
synchronized = $synchronized
type = $type
}
$test.result.organizationContext += $newOrganizationContext

Related

Store map objects inside of array

Currently I have an array of strings.
$vals = #()
$vals = $vals + $string
How can I store objects instead? F.e. just like in javascript:
{string: 'string', path: 'path'}
Is there an equivalent in powershell to store such objects?
As of PowerShell 3.0 you can initialize a custom object with the following syntax:
[pscustomobject]#{string = 'string';path = 'path'}
So to add to a dynamically sized array, simply do:
$vals = #()
$vals += [pscustomobject]#{string = 'string';path = 'path'}

How can I get the presentation details/rendering properties when using Find-Item command?

I want to get the renderings (presentation details) of each items.
I have tried using Get-Rendering, but it doesn't work.
$criteria = #(
#{ Filter = "Equals"; Field = "_template"; Value = "{9A43A639-4209-49B9-8024-766A9E1AB03E}"; },
#{ Filter = "DescendantOf"; Value = (Get-Item "master:/content/"); }
)
$props = #{
Index = "sitecore_master_index"
Criteria = $criteria
}
Find-Item #props | Get-Rendering -FinalLayout
It throws the following error:
The input object cannot be bound to any parameters for the command
either because the command does not take pipeline input or the input
and its properties do not match any of the parameters that take
pipeline input.
What am I missing?

Why does $DeploymentProcess.Steps.Name[0] return first if many, but first character of first if only one?

$ReleaseBody = #{
Id = $Project.Id
ProjectId = $Project.Id
ChannelId = $Channels.Items.Id
Version = $VERSION
SelectedPackages = #( #{
StepName = $DeploymentProcess.Steps.Name[0]
Version = $dt.Items.Version
})
} | ConvertTo-Json
I am querying an API that returns one or more StepName via $DeploymentProcess, I want to always select the first for my POST. The above works fine if there are multiple steps but not if there is only one. Thoughts?

PowerShell Export-Csv repeating last value in output - solution

SCENARIO:
In my script, I create an object, add the common info, and then updated the sub info for each object in a loop and add that object to an array of objects.
My verbose shows that the values are correct in the object added to the array (reporting the last item in the array), but Export-Csv repeats the last object values.
I am able to resolve this if I create a new object each time. Reusing the same object repeats the last value, even though I can see it correct when I select * on the objects in the array. There must be something about the array object, such as a guid that is duplicated for the same object.
PowerShell 5.1 verified on Windows 7, Windows 2008R2, Windows 2010R2
SOLUTION:
Do not reuse an object when adding it to an array of objects.
OUTPUT:
#TYPE MyGpoSetting
"SetName","SetCategory","SetType","SetState","SetValue","SetData","SetNote","SubName","SubState","SubValue","GpoDomain","GpoName","GpoLinks","GpoGuid"
"SetName","SetCategory","SetType","SetState","SetValue","SetData","SetNote","SubName3","SubState3","SubValue3","GpoDomain","GpoName","GpoLinks","GpoGuid"
"SetName","SetCategory","SetType","SetState","SetValue","SetData","SetNote","SubName3","SubState3","SubValue3","GpoDomain","GpoName","GpoLinks","GpoGuid"
"SetName","SetCategory","SetType","SetState","SetValue","SetData","SetNote","SubName3","SubState3","SubValue3","GpoDomain","GpoName","GpoLinks","GpoGuid"
SCRIPT:
Add-Type -TypeDefinition #"
public struct MyGpoSetting
{
public string SetName;
public string SetCategory;
public string SetType;
public string SetState;
public string SetValue;
public string SetData;
public string SetNote;
public string SubName;
public string SubState;
public string SubValue;
public string GpoDomain;
public string GpoName;
public string GpoLinks;
public string GpoGuid;
}
"#
$aoMyGpoSetting = #();
$oMyGpoSetting = New-Object -TypeName 'MyGpoSetting';
$oMyGpoSetting.SetName = 'SetName';
$oMyGpoSetting.SetCategory = 'SetCategory';
$oMyGpoSetting.SetType = 'SetType';
$oMyGpoSetting.SetState = 'SetState';
$oMyGpoSetting.SetValue = 'SetValue';
$oMyGpoSetting.SetData = 'SetData';
$oMyGpoSetting.SetNote = 'SetNote';
$oMyGpoSetting.SubName = 'SubName1';
$oMyGpoSetting.SubState = 'SubState1';
$oMyGpoSetting.SubValue = 'SubValue1';
$oMyGpoSetting.GpoDomain = 'GpoDomain';
$oMyGpoSetting.GpoName = 'GpoName';
$oMyGpoSetting.GpoLinks = 'GpoLinks';
$oMyGpoSetting.GpoGuid = 'GpoGuid';
$aoMyGpoSetting += $oMyGpoSetting;
#--- $oMyGpoSetting = New-Object -TypeName 'MyGpoSetting';
$oMyGpoSetting.SetName = 'SetName';
$oMyGpoSetting.SetCategory = 'SetCategory';
$oMyGpoSetting.SetType = 'SetType';
$oMyGpoSetting.SetState = 'SetState';
$oMyGpoSetting.SetValue = 'SetValue';
$oMyGpoSetting.SetData = 'SetData';
$oMyGpoSetting.SetNote = 'SetNote';
$oMyGpoSetting.SubName = 'SubName2';
$oMyGpoSetting.SubState = 'SubState2';
$oMyGpoSetting.SubValue = 'SubValue2';
$oMyGpoSetting.GpoDomain = 'GpoDomain';
$oMyGpoSetting.GpoName = 'GpoName';
$oMyGpoSetting.GpoLinks = 'GpoLinks';
$oMyGpoSetting.GpoGuid = 'GpoGuid';
$aoMyGpoSetting += $oMyGpoSetting;
#--- $oMyGpoSetting = New-Object -TypeName 'MyGpoSetting';
$oMyGpoSetting.SetName = 'SetName';
$oMyGpoSetting.SetCategory = 'SetCategory';
$oMyGpoSetting.SetType = 'SetType';
$oMyGpoSetting.SetState = 'SetState';
$oMyGpoSetting.SetValue = 'SetValue';
$oMyGpoSetting.SetData = 'SetData';
$oMyGpoSetting.SetNote = 'SetNote';
$oMyGpoSetting.SubName = 'SubName3';
$oMyGpoSetting.SubState = 'SubState3';
$oMyGpoSetting.SubValue = 'SubValue3';
$oMyGpoSetting.GpoDomain = 'GpoDomain';
$oMyGpoSetting.GpoName = 'GpoName';
$oMyGpoSetting.GpoLinks = 'GpoLinks';
$oMyGpoSetting.GpoGuid = 'GpoGuid';
$aoMyGpoSetting += $oMyGpoSetting;
$aoMyGpoSetting | Export-Csv -Path 'c:\temp\export.csv' -Encoding 'ASCII';
This is working as expected. Adding the object to the array (+=) doesn't copy the object, but instead adds a reference/pointer to it in the next 'slot'. So in effect you are adding three references to the same object. It's like having 3 entries in your phonebook for your best friend:
John Smith - 01234 5678
Jonnie - 01234 5678
Smith, John - 01234 5678
Whichever one you call, gets you through to the exact same person.
Similarly, each time PowerShell displays an object from your array, it is actually going back to the same source object and showing it to you. That is why all of them have the same properties as the last one you added - they are in fact all that same one.
As you've discovered, creating a new object each time is the way to proceed.

Immediate lookup of value in a hash table defined as a literal

What is the shortest and nicest way to have a hash table literal and instantly look up a value in it?
e.g. I was expecting something like
$city = #{"30328" = "Atlanta"; "60608" = "Chicago"} [$zipCode]
but that ends with:
Unexpected token '[$zipCode]' in expression or statement.
Just remove the space:
$city = #{"30328" = "Atlanta"; "60608" = "Chicago"}[$zipCode]
or
$city = #{"30328" = "Atlanta"; "60608" = "Chicago"}.$zipCode