I have a following piped command that should print a GPO name and the group that is associated with it, repeating this untill all GPOs and groups have been printed. The output isn't right though. The name comes out correctly but the group says Microsoft.GroupPolicy.GPTrustee instead of the groups name.
How should I access it to get the value?
Here is my piped command:
Get-GPO -All | ForEach-Object {$gpo = $_.DisplayName; Write-Output $_;} | Get-GPPermission -All |
Where-Object {$_.Permission -eq "GpoApply"} |
Select-Object #{Name="GpoName"; Expression={$gpo}},#{Name="Group"; Expression={$_.trustee}}
EDIT: The problem was solved, but I'll still copy the output of "Microsoft.GroupPolicy" -object here to clarify why I thought I'd be able to access it with $_.trustee variable.
It looked like this so $_.trustee.name didn't even cross my mind.
Trustee : Domain Computers
TrusteeType : Group
Permission : GpoApply
Inherited : False
Related
I am trying to delete a user account from AD, but it fails as the user object contains some leaf objects (Exchange active sync devices).
Tried using the Exchange powershell cmdlet:
Remove-ActiveSyncDevice
but the "ExchangeActiveSyncDevices" leaf object does not get removed from the user object.
After entering the command Remove-ActiveSyncDevice
Is there a way to completely remove the ExchangeActiveSyncDevices from the user object in AD through exchange powershell?
Untested by me, but by reading the documentation, this should work:
# parameter Mailbox can use any of these user properties:
# Name
# Alias
# Distinguished name (DN)
# Canonical DN
# <domain name>\<account name>
# Email address
# GUID
# LegacyExchangeDN
# SamAccountName
# User ID or user principal name (UPN)
# get the list of devices for your user and loop through, removing them one-by-one
Get-ActiveSyncDevice -Mailbox "the.user#yourcompany.com" | ForEach-Object {
$_ | Remove-ActiveSyncDevice -WhatIf
}
Exchange 2013 or later
Get-MobileDevice -Mailbox "the.user#yourcompany.com" | ForEach-Object {
$_ | Remove-MobileDevice -WhatIf
}
When satisfied with what info is shown in the console, remove the -WhatIf switch to actually have the code remove the devices.
I've had the same problem - you need to use Get-ADObject to find the empty nodes, and then pipe to Remove-ADObject to remove them. Sorry I don't have a script to share as I used Excel and some powershell commands to:
see if the node exists
check it's empty
remove the node
This is just to confirm that Remove-MobileDevice doesn't delete the empty offending node that affected my ADMT migration.
So I try to link the networkname of a cluster resource to the AD object by the objectGUID in powershell.
If I execute following command Get-ClusterResource -Cluster CLxxx -Name NameOfResource the NetworkName object is returned.
I can ask for the clusterparameters and the parameter ObjectGUID (Get-ClusterResource -Cluster CLxxx -Name NameOfResource | Get-ClusterParameter -Name objectguid).Value
It will return something like 5efa2b5674c75d43bcdad1c2c183d5ca
So far so good.
If I try to get the ADobject from Active Directory (Get-ADComputer -Filter {Name -Like "$Name"}).ObjectGUID.guid
It will return something like 562bfa5e-c774-435d-bcda-d1c2c183d5ca
It is almost the same but not quite.
Al the characters are there but not in the correct order.
ClusterParameter: 5efa2b5674c75d43bcdad1c2c183d5ca
Active Directory: 562bfa5e-c774-435d-bcda-d1c2c183d5ca
If you look at the string AD returned you can split the string in 5 sequences on the -.
The last 2 are in the correct order but the first three have to switch front to back (2 characters at a time).
ClusterParameter: 5efa2b56-74c7-5d43-bcda-d1c2c183d5ca
Active Directory: 562bfa5e-c774-435d-bcda-d1c2c183d5ca
It is the same on 2008,2012 and 2016.
Does someone have an explanation for this?
I'm trying to sort the AWS Instances I pull with the Get-EC2Instance cmdlet but the issue I'm facing is that the property is a Tag and I'm not sure how to format it properly. I only know about assigning simple properties like "Sort-Object -Property Name".
I used the following to get the AWS Instances filtered by the Name tag.
$ids = Get-EC2Instance -Filter #( #{name='tag:Name'; values="*EXAMPLE*"}) | Select-Object -ExpandProperty instances | #insert sort here
Trying to pipeline sort in the last part. I tried properties like tag, tag:Name, tag:Key=Name but all failed. When I used Get-EC2Image, I had no issues with Sort Name but can't figure it out for Get-EC2Instance.
There is a AWS CLI version and answer at Sort EC2 Instances by Tag Name but I wasn't able to apply it to PowerShell.
EDIT: Rewrote question and added more details since it got downvoted.
You can pipe the output to Sort-Object cmdlet. I dont have access to a AWS instance to test this. But try variation of this command
$ids = Get-EC2Instance -Filter #( #{name='tag:Name'; values="*EXAMPLE*"}) | Select-Object -ExpandProperty instances | Sort-Object $_.Tag.Value
There are two ways. You can get the object using the $_
OR
You can reference the property directly by using a method chain like:
(Get-EC2Instance).instances.tag.value
I am trying to figure out something in powershell using piping.
I have something like this:
Get-Project -All | %{ $_.Name }
Note: I know %{} = ForEach{}
Get-Project -All is a NuGet command which returns all project in current solution.
This works for me, but I would like to find out all available variables that get piped through. I only know $_.Name is one of them as I can see from an example online.
When I run Get-Project -All I get the following output
ProjectName Type FullName
----------- ---- --------
ABC C# C:\SolutionDir\Category\ABC.csproj
Now inside my pipe I found the following variables:
$_.Name = ABC
$_.ProjectName = Category/ABC
$_.Type = C#
$_.FullName = C:\SolutionDir\Category\ABC.csproj
Now what I don't understand is
The "Table" output of Get-Project -All has a header of ProjectName with value of ABC. But inside the pipe $_.ProjectName = Category/ABC and$_.Name = ABC`. Are they unrelated?
If I didn't see the example online mentioning $_.Name I would of never known about this variable. Is there some way of getting all available variables?
Note:
Get-Project -All | %{ Get-Variable } does not return any of the above mentioned variables.
When you use the pipeline, it's the entire object created, in this case Get-Project, that is sent through it. To see all members of an object, use Get-Member.
So in your case, you can use Get-Project -All | Get-Member to see all the properties and methods of the object being returned.
When using powershell, sometimes I want to only display for example the name or fullname of a file.
From what I can gather, the way to do this is by using Get-ItemProperty (alias of gp) and passing -n fullname, For example
PS C:\Dev> gp . -n fullname
Notably I want to use this in longer scripts combined with foreach and where, and so on
Powershell then displays the fullname, but it also displays a bunch of other stuff, as follows:
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Dev
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\
PSChildName : Dev
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
fullname : C:\Dev
My question is, how can I get powershell to only display the property I want (fullname) and not cruft the display up with all the other stuff. Is Get-ItemProperty even the right way to do this?
Update:
If I do this:
ls -r | ?{ $_.fullname -match "foo" }
This gives me a series of lists, one for each directory, showing all the 'foo' files in each directory. What I'd like to do is consolidate those multiple lists into one single list, and not show the Mode, LastWriteTime, Length or any other irrelevant stuff. If Get-ItemProperty is not the correct way to show those things, what is?
There are many ways. I think you were looking for Get-ChildItem:
PS > get-childitem x.txt | select fullname
FullName
--------
C:\WINDOWS\system32\WindowsPowerShell\v1.0\x.txt
PS > get-childitem x.txt | select name
Name
----
x.txt
PS > get-childitem x.txt | % { $_.fullname }
C:\WINDOWS\system32\WindowsPowerShell\v1.0\x.txt
PS >
You could use Get-ItemProperty, but I think it's more for things like registries.
PS > Get-ItemProperty x.txt | select name
Name
----
x.txt
Format-Table, alias ft is also good to know about...
PS > gci x.txt | format-table -a fullname,length
FullName Length
-------- ------
C:\WINDOWS\system32\WindowsPowerShell\v1.0\x.txt 126
Commonly used aliases for Get-ChildItem, are ls, dir, and gci. I'd go with gci, when in Rome.
Just an additional comment... The way PowerShell displays information to the screen can also be modified by making changes to its Extended Type System (ETS).
Check the help for Update-TypeData.
Going this route does introduce some more advanced topics...
you can use (depending on what you exactly need):
(gi c:\temp\file -ea 0).fullname
(gi c:\temp\file -ea 0) | select -ExpandProperty fullname
C:\temp\file
-ea: do not show error if file does not exist
If you are in a foreach loop and you have a reference to the object that you are interested in, then have you tried something like the following to just echo the property.
foreach ($file in dir)
{
$file.fullname
}
I've always restricted my use of Get-ItemProperty for navigation providers like the registry where getting data via Get-Item or get-ChildItem. Compare
ls hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
gp hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
what you want to do is use the properties directly
ls | ? { $_.FullName -match "whatever"}