Powershell - Display value of an object's properties, where the property names are like '*quota*' - powershell

As per the subject, I'm trying to get the name of a property and the value assocaited with that property, for a specific mailbox.
So, the line below gets me a nice list of the available object properties, and a default column displayed in the output has the heading 'Name'
Get-Mailbox -Identity "Person Name" | gm
I then want to say something like:
For the object: "Mailbox of Person Name"
Where the property of "Mailbox of Person Name" has a name like 'quota'
List both the actual property name and it's value for "Mailbox of Person Name"
I've tried a number of things using -ExpandProperty/Select-Object/Where-Object but they're all failing. I'm sure this is pretty basic, but Powershell is definitely not my strength. Can anyone show me how to structure this pipeline correctly?

You do not need to use Where-Object, only Select-Object:
Get-Mailbox -Identity "Person Name" | Select-Object -Property *quota*

You seem to have used the correct commandlets. Where-Object filters. Select-Object selects specific properties.
From my experience, sometimes what you see on the console doesn't match the actual property name because there is a formatter that can even change the column name. If you you drive the Where-Object and Select-Object with that virtual property name then they do fail. Also sometimes, the output is not really a recordset that works well with these cmdlets.
My advice is to always check the type of an object when things go strange. Starting from $items=Get-Mailbox -Identity "Person Name".
Then $items.GetType() reveals the actual .net type.
Then $items.Count reveals if it is actually an array or a single object.
Then $items|ForEach-Object {$_.GetType()} reveals the type of each object.
Also the $items|Get-Member is very helpful to figure out the property names. If necessary use it also within your loop.
That is how I troubleshoot strange behaviors and if you can post your findings and the code you tried with Where-Object and Select-Object that would be a great help.

Related

Add An AD Attribute To All Users In an AD Group

We have a few groups that we are playing with. We'll call it Group1 Group2 Group3. We then have a custom AD Attribute called "team". We need to take all the users of Group1 and change their AD Attribute to "Group1" etc etc. I've looked at a few ways to do this but am drawing up a blank. Any suggestion is greatly appreciated.
There are a few functions to work with here, and this will require that you have the AD module installed for PowerShell.
First, you'll need to get all of the members of the group, and you likely want to do it recursively. So that's:
Get-ADGroupMember -Identity "Group A" -Recursive
Running that on its own should give you all the members. But now you want to do something with what you got back from that function, so you want to loop over them
Get-ADGroupMember | ForEach-Object {
# You'll do something with each member here.
}
And what you want to do it set the AD attribute, which you can do with Set-ADUser. While most attributes can be set easily as they're all properties of the function, yours appears to be custom so you need to use -replace. That looks like this:
Get-ADGroupMember | ForEach-Object {
Set-ADUser -Identity $_ -Replace #{"Team"="Group A"} -WhatIf
}
The -WhatIf on the end makes the function tell you what it would do, but it doesn't actually do it. I've left it there so you don't accidentally run the code without testing it out first. When you want this to actually do something, remove that text.
You should try this on a small group with one or two people to make sure it works the way you want, and then when you're ready, hit the larger group

Get AD user by providing fullName and manager full name

It might look silly but I'm struggling with finding user with Powershell by providing his full name and his manager full name. Purpose of script is to get SamAccountName and Email Address by using mentioned values which are provided by other team (these are the only unique values I get - getting user by Full Name is not any kind of problem, but it's possible that it'll return multiple results, and that's why Manager Full Name would determine appropriate result).
First I was using simple command
Get-ADUser -server $gc -Filter { (CN -eq $uFullName) -and (extensionAttribute4 -eq $mFullName) }
It worked great, but unfortunately I noticed that not all accounts use extensionAttribute4 to hold manager full name. I thought of using Filter on manager property but when I tried to use (Manager -like "*value*") it returned that like operator isn't supported by this attribute.
I'm still trying to find solution for this but maybe someone will have some solution to this situation.
Thank you in advance.

Sitecore Powershell: items with multiple fields with the same name

I am trying to retrieve the "Languages" field using the following syntax:
Get-Item master: -Query "<my query>" |
Show-ListView -property `
"First Name",
#{Label="Languages"; Expression={$_.Languages}}
Which returns, quite correctly, the Sitecore field "Languages" recording the item's language.
The field that I care about, however, is a Multilist which is also called Languages and is listed under a specific Template Section (called "Background").
Question
Is there a way to retrieve an Item's field when another field has the same name?
SPE Drive provider will attempt to remedy this situation with prepending the field name with an underscore. It will do it as many times as needed to achieve uniqueness for each field. in your case to get to the Languages field you need to address the field as _Languages so the following should yield results you're expecting
Get-Item master: -Query "<my query>" |
Show-ListView -property `
"First Name",
#{Label="Languages"; Expression={$_._Languages}}
I wasn't sure as well, but it seems like you can't use the same field's name.
A small test that I've ran to make sure it's true:
Get-Process | select name,cpu,#{n='cpu';e={'lala'}}
The error message I'm getting:
select : The property cannot be processed because the property "cpu" already
exists.
Try this:
Get-Item master: -Query "<my query>" |
Show-ListView -property
"First Name",
#{Label="Languages"; Expression={$_.Fields["<ID of the field you really want>"].Value}}

Get specific objects from Win32_PnpAllocatedResource

So I want to get those Pnp Devices, which use a given memory area (for example starting address = 655360). I'm using CIM/WMI and and the following command will get back resource and PnpEntity associations:
Get-CimInstance -ClassName Win32_PnpAllocatedResource
But after that how can I get the Win32_PnpEntity associated to Win32_DeviceMemoryAddress which has the starting address 655360?
So the field that you want is the Antecedent property. You only want the ones that refer to memory allocation, and of those you only want the ones that start at 655360. This is really basic stuff, and if you need to ask how to use a Where statement you probably need to go look on the internet on how to filter an array or something for a more detailed explanation to walk you through things.
Get-CimInstance -ClassName Win32_PnpAllocatedResource | Where{$_.Antecedent -like 'Win32_DeviceMemoryAddress (StartingAddress = 655360)'}
That will only return the entries where the starting address is 655360. You should, in theory, be able to use -eq but for this case it doesn't seem to work as expected so the value may have hidden characters, or may be a value type other than [String] so we have to use -Like or -Match and in this case -Like works fine, and is less complicated.

Find and replace custom attribute values in AD using Powershell

So I have an interesting script I am trying to figure out, basically I need to change a custom attribute value to a new one. The problem is its for both users and computers and not specific to the groups. So for instance the value might be Billing1 for several users in an OU and this need to be Billing2. So I need to find any instance of the Value of Billing1 and change it to Billing2 not knowing the user or computer object. I can successfully change one at a time if I know who the user is by using Set-ADUser, Set-ADComputer and even with Set-AdObject but I need to figure out a Find and replace function.
I have searched for this and I have found examples of where I can use CSV for users and computers but again I don't know who has what since the value in the attribute can vary and also changes if a reorg happens.
got the correct script...
Get-ADComputer -Properties enterattributename -Filter {enterattributename -like "value to search" } |Set-ADComputer –replace #{ enterattributename =”value to change”}
this also can be applied to Get-ADUser and Get-ADObject