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.
Related
i have two arrays that look like this:
$ht1 = #{
"computer55" = "port33"
“computer1” = “port1”
“computer2” = “port2”
}
and
$ht2 = #{
"A1:B2:C3:D4:E5:F6" = "port1"
"A2:B3:C4:D5:E6:F7" = "port2"
"A3:B4:C5:D6:E7:F8" = "port33"
"A4:B4:C5:D6:E7:F8" = "port45"
}
The first one is one I manually hardcode into the script, I have an actual list of device names and what port they are plugged into on a switch. The second one is generated with a switch script that logs in, gets the mac address table and records it as a hashtable.
My desired outcome is this, if there is a port with an assigned name, replace the port name with the device name.
$ht3(or any name) = #{
"A1:B2:C3:D4:E5:F6" = "computer1"
"A2:B3:C4:D5:E6:F7" = "computer2"
"A3:B4:C5:D6:E7:F8" = "computer55"
"A4:B4:C5:D6:E7:F8" = "port45"
}
I've somehow spent about a day on this(... pretty much the first powershell script I've ever came up with) and my end result is always the same, I end up merging two hashtables and pair the port with the computer name and not the mac address with the computer name. any direction is appreciated
Important note, the .ContainsValue method is case sensitive, if you want a case insensitive search use one of the following:
if($val = [string]$ht1.Keys.Where({$ht1[$_] -eq $ht2[$key]}))
{
#{$key = $val}
continue
}
if($ht1.Values -contains $ht2[$key])
{
...
}
if($ht2[$key] -in $ht1.Values)
{
...
}
Code
$ht1 = #{
computer55 = 'port33'
computer1 = 'port1'
computer2 = 'port2'
}
$ht2 = #{
'A1:B2:C3:D4:E5:F6' = 'port1'
'A2:B3:C4:D5:E6:F7' = 'port2'
'A3:B4:C5:D6:E7:F8' = 'port33'
'A4:B4:C5:D6:E7:F8' = 'port45'
}
$result = foreach($key in $ht2.Keys)
{
if($ht1.ContainsValue($ht2[$key]))
{
#{$key = [string]$ht1.Keys.Where({$ht1[$_] -eq $ht2[$key]})}
continue
}
#{$key = $ht2[$key]}
}
Looking at $result yields:
Name Value
---- -----
A1:B2:C3:D4:E5:F6 computer1
A3:B4:C5:D6:E7:F8 computer55
A4:B4:C5:D6:E7:F8 port45
A2:B3:C4:D5:E6:F7 computer2
[string]$ht1.Keys.Where({$ht1[$_] -eq $ht2[$key]})
Could also be the following, though, I'm not sure which one would be more efficient:
$ht1.GetEnumerator().Where({$_.Value -eq $ht2[$key]}).Key
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
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?
$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?
I need to add new Managed Metadata Property into Sharepoint 2013 using Powershell + Sharepoint Powershell Extensions.
I did this using C#.
To get all Sharepoint Managed properties I made this:
private static string GetAllSPManagedProperties(string searchApplication)
{
RunspaceConfiguration config = RunspaceConfiguration.Create();
PSSnapInException OExSnapIn = null;
PSSnapInInfo pssnap = config.AddPSSnapIn("Microsoft.SharePoint.PowerShell", out OExSnapIn);
//create powershell runspace
Runspace cmdlet = RunspaceFactory.CreateRunspace(config);
cmdlet.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(cmdlet);
// set powershell execution policy to unrestricted
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
// create a pipeline and load it with command object
Pipeline pipeline = cmdlet.CreatePipeline();
Command cmd = new Command("Get-SPEnterpriseSearchMetadataManagedProperty");
pipeline.Commands.Add(cmd);
CommandParameter cmdParam = new CommandParameter("SearchApplication", searchApplication);
cmd.Parameters.Add(cmdParam);
//pipeline.Commands.Add("Out-String");
// this will format the output
IEnumerable<PSObject> output = pipeline.Invoke();
pipeline.Stop();
cmdlet.Close();
// process each object in the output and append to stringbuilder
StringBuilder results = new StringBuilder();
foreach (PSObject obj in output)
{
var typeNames = obj.TypeNames;
var p1 = obj.Properties["ID"].Value; // "AboutMe" object {string}
var p2 = obj.Properties["ManagedType"].Value; // Text object {Microsoft.Office.Server.Search.Administration.ManagedDataType}
var p3 = obj.Properties["PID"].Value; // 26 object {int}
var p4 = obj.Properties["Name"].Value; // "AboutMe" object {string}
var p5 = obj.Properties["HasMultipleValues"].Value; // true object {bool}
string managedTypeName = (string)p2.ToString();
results.AppendLine(obj.ToString());
}
return results.ToString();
}
The problem is that I am trying to set this flag "HasMultipleValues" of the selected Managed Metadata Property programmatically.
obj.Properties["HasMultipleValues"].Value = true;
I do not know how to do that. I was hoping to find some Update method of the PSObject (returned by the pipeline.Invoke() but unfortunately didn't found anything useful.
My question is, Is it possible to set the properties of any ManagedMetadataProperty and how ?
It looks like that I found a solution myself.
I didn't found the way on how to achieve the solution through setting object properties but have found equally acceptable solution through scripting.
public static void UpdateSharepointManagedMetadataProperty(string searchApplication, string propertyName, bool HasMultipleValues)
{
RunspaceConfiguration config = RunspaceConfiguration.Create();
PSSnapInException OExSnapIn = null;
PSSnapInInfo pssnap = config.AddPSSnapIn("Microsoft.SharePoint.PowerShell", out OExSnapIn);
//create powershell runspace
Runspace cmdlet = RunspaceFactory.CreateRunspace(config);
cmdlet.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(cmdlet);
bool set = HasMultipleValues;
string script = "";
if (set)
{
script =
string.Format("$prop = Get-SPEnterpriseSearchMetadataManagedProperty -Identity {0} -SearchApplication \"{1}\" \r\n", propertyName, searchApplication) +
"$prop.HasMultipleValues = $true \r\n" +
"$prop.Queryable = $true \r\n" +
"$prop.Refinable = $true \r\n" +
"$prop.Searchable = $true \r\n" +
"$prop.Retrievable = $true \r\n" +
"$prop.Update() \r\n";
}
else
{
script =
string.Format("$prop = Get-SPEnterpriseSearchMetadataManagedProperty -Identity {0} -SearchApplication \"{1}\" \r\n", propertyName, searchApplication) +
"$prop.HasMultipleValues = $false \r\n" +
"$prop.Queryable = $false \r\n" +
"$prop.Refinable = $false \r\n" +
"$prop.Searchable = $false \r\n" +
"$prop.Retrievable = $false \r\n" +
"$prop.Update() \r\n";
}
var result = scriptInvoker.Invoke(script);
cmdlet.Close();
}