I have got Strings in an array which are the names of groups. Now I would like to modify those values and connect an other String to the beginning of this String
$Groups = Get-ADPrincipalGroupMembership $User $GroupArray = #()
foreach ($Group in $Groups)
{
$GroupArray += ($Group | select name)
}
echo $("Domain\" + $GroupArray[0])
This prints something like:
Domain\#{name=Domain Users}
However I would like to get something like:
Domain\Domain Users
Change it to arraylist if you wish to add all the values:
$Groups = Get-ADPrincipalGroupMembership $User
$arraylist = New-Object System.Collections.ArrayList
foreach ($Group in $Groups)
{
$arraylist.Add($Group.Name) | Out-Null
}
$arraylist
PS: You can display the result collating with the Domain however you want; I have not touched that section. Hope it helps.
Related
tldr; I need to fill an array, which is populated in a function, within constricted language. Until now i found only ways, which are not do able in constricted language.
So basicly i want to loop through the AD and identify looping groups and where users are placed looping wise.
To Achive this i wrote a function which calls itslef. The function returns 4 diffrent objects. These objects are needed to handle the loop.
But the function scope needs to return the value to the script scope ("top most") as otherwise the script will loop infinitly on the first object already.
Unfortunatly this is in constrained language, which means the most common resolves wont work.
Shortend Code Sample
$ReturnValue1 = #()
$ReturnValue2 = #()
$ReturnValue3 = #()
$ReturnValue4 = #()
Function Get-ADInfos
{
Param(
$Entitys
)
foreach($Entity in $Entitys){
$Object = New-Object -TypeName PSObject
if($Entity.objectClass -eq "user"){
if($ReturnValue2.User.distinguishedName -contains $Entity)
#Do Something
$ReturnValue1 += $Object
Write-Host "$Entity is already scanned"
}else{
#Do something
$ReturnValue2 += $didsomething
Get-ADInfos $Values #looping
}
}elseif($Entity.objectClass -eq "group"){
if($ReturnValue4.Group.distinguishedName -contains $Entity){
#Do Something
$ReturnValue3 += $didsomething
}else{
#Do Something
$ReturnValue4 += $didsomething
Get-ADInfos $Values
}
}else{
write-host "finished"
}
}
Full Code for Repro (Older) #Note: To use constrained language for testing.
$User = #()
$Gruppen = #()
$LoopUser = #()
$LoopGroup = #()
Function Get-ADInfos
{
Param(
$Entry
)
#$Entry = Get-ADGroup "Domain Users"
if($Entry.objectClass -eq "user"){
$Entitys = Get-ADPrincipalGroupMembership $Entry
}elseif($Entry.objectClass -eq "group"){
$Entitys = Get-ADGroupMember $Entry
}else{}
foreach($Entity in $Entitys){
if($Entity.objectClass -eq "user"){
if($User.user -contains $Entity){
$Row = "" | Select User, Group
$Row.User = $Entity
$Row.Group = $Entry
$LoopUser += $Row #return to "master" scope
Write-Host "$Entity is already scanned"
}else{
$Row = "" | Select User, Group
$Row.User = $Entity
$Row.Group = $Entry
$User += $Row #return to "master" scope
Write-Host "$Entity is in $group"
Get-ADInfos $Entity
}
}elseif($Entity.objectClass -eq "group"){
if($Groups.group -contains $Entity){
$Row = "" | Select ScannedGroup, ParentGroup
$Row.ScannedGroup = $Entity
$Row.ParentGroup = $Entry
$LoopGroup += $Row #return to "master" scope
Write-Host "$Entity is already scanned"
}else{
$Row = "" | Select Group
$Row.Group = $Entity
$Groups += $Row #return to "master" scope
Write-Host "$Entity scanned"
Get-ADInfos $Entity
}
}else{
write-host "finished"
}
}
}
Get-ADGroup "Domain Users" | Get-ADInfos
PowerShell Arrays are immutable (fixed size collections):
$User = #()
$User.add('item')
MethodInvocationException: Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
This means that -besides the inefficient use of the increase assignment operator (+=)- it will create a new copy of the of array in each child scope when you try to change it:
$User = #()
function Test {
$User += 'Item'
Write-Host 'Child scope:' $User
}
Test
Write-Host 'Parent scope:' $User
Yields:
Child scope: Item
Parent scope:
Instead, I recommend you to use List<T> Class knowing that you can use the List<T>.Add(T) Method and the fact that objects are referenced by default:
$User = [Collections.Generic.List[object]]::new()
function Test { $User.Add('item') }
Test
$User # Yields: item
Addendum
As your script appears to run under constrained language mode, you will not be allowed to use the List<T> type or anything similar you might consider to use native PowerShell HashTables instead. See also: Mutable lists in Constrained Language Mode.
To apply this to your script:
Change all the arrays (that have a shared scope) to hashtables.
e.g.: $User = #() → $User = #{}
Change your assignments.
e.g.: $User += $Row → $User[$User.Count] = $Row
Change the conditions.
e.g.: $User.user -contains $Entity → $User.Values.user -contains $Entity
I'm fairly new with Powershell, so this is likely a rookie mistake, but I am trying to take a CSV document containing only user display names, query AD for the required information, and populate that information into object properties using a hash table.
Here is what i have right now
$Path = "C:\Scripts\Generate-CSRSpreadsheets\Roster-Jpay.csv"
$Table = Import-csv -Path $Path -Header EmployeeDisplayName
$Array = #()
$ADUser = Get-ADUser -Properties DisplayName,Manager -Filter {DisplayName -eq $_.EmployeeDisplayname}
ForEach($User in $Table){
$Object = New-Object PSObject -Property #{
DisplayName = $ADUser.DisplayName
GivenName = $ADUser.GivenName
Surname = $ADUser.Surname
Email = $ADUser.Mail
}
$Array += $Object
}
This seems to me like it should work fine, but when I check my output it looks something like this:
Example Output Image
Let's say in this example I have 9 total users, but it's only outputting the information from the last user in the csv. I've been pouring over this code, but I can't see what's wrong with it. Any help would be appreciated.
Thank you
You need to move your Get-ADUser inside the loop:
$Path = "C:\Scripts\Generate-CSRSpreadsheets\Roster-Jpay.csv"
$Table = Import-csv -Path $Path -Header EmployeeDisplayName
$Array = #()
ForEach($User in $Table){
$ADUser = Get-ADUser -Filter "DisplayName -eq '$($User.EmployeeDisplayName)'" -Properties DisplayName,Manager
$Object = New-Object PSObject -Property #{
DisplayName = $ADUser.DisplayName
GivenName = $ADUser.GivenName
Surname = $ADUser.Surname
Email = $ADUser.Mail
}
$Array += $Object
}
I have an array that contains different rows where one column identifies the "record" "type." I want to iterate through this array and sort each item based on that value into a new array so that I have one array per type.
Here's what I have so far:
$data = Get-ADObject -SearchBase $sb -filter * -properties * | select samaccountname,canonicalname,objectclass,distinguishedname | sort objectclass,samaccountname
$oct = $data | select objectclass -Unique
foreach ($o in $oct)
{
$oc = $o.objectclass
Remove-Variable -name "$oc"
New-Variable -name "$oc" -value #()
}
$d = #()
$user = #()
foreach ($d in $data)
{
$oc = $d.objectclass
foreach ($o in $oct)
{
$1 = $o.objectclass
if ($1 -eq $oc)
{
('$' + $oc) += $d
}
}
}
(the lines: Remove-Variable -name "$oc", $d = #(), and $user = #() are for testing purposes so ignore those)
This works great up to the line where I try to dynamically reference my new arrays. What am I doing wrong and how can I fix it?
The error text is:
('$' + $oc) += $d
~~~~~~~~~ The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept
assignments, such as a variable or a property.
CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : InvalidLeftHandSide
I have tried using $($oc), but that didn't work either. If I change it to the name of one of my dynamically created arrays like $user, the code works fine except that it loads everything into the $user array (obviously).
The reason I tried ('$' + $oc) is because this is the only way I could get ISE to output $user.
I also tried ('$' + $oc).add($d) but it appears to be seeing it as a string rather than the array.
Any pointers are appreciated.
Use the Get-Variable and Set-Variable cmdlets:
$curVal = Get-Variable -Name $oc -ValueOnly
Set-Variable -Name $oc -Value ($curVal+$d)
But note that you would be better off building this array in a local variable first, and then assigning it to your "runtime-named" variable once, as these get and set operations are going to be way slower.
Rather than fiddling around with dynamically named variables, I'd use dictionary-type, like for example a hashtable:
# initialize an empty hashtable
$objectsByClass = #{}
# Define list of properties
$properties = 'samaccountname','canonicalname','objectclass','distinguishedname'
# Retrieve AD objects
$Data = Get-ADObject -SearchBase $sb -filter * -properties $properties | select $properties | sort objectclass,samaccountname
#Populate hashtable
$Data |ForEach-Object {
if(-not $objectsByClass.ContainsKey($_.objectClass)){
# Create entry in hashtable
$objectsByClass[$_.objectClass] = #()
}
# Add entry to dictionary
$objectsByClass[$_.objectClass] += $_
}
Now you can access the items by class name:
$users = $objectsByClass['user']
And you can easily discover all class names:
$classNames = $objectsByClass.Keys
As briantist points out, you can also have Group-Object build the hashtable for you if the above gets too verbose:
$objectsByClass = $Data |Group-Object objectClass -AsHashTable
I'm trying to convert a set of SharePoint list items (and associated data) into a JSON object. To do this I'm trying to create a multi-dimensional array and then iterate over my SharePoint objects to populate it.
This is the relevant code so far:
#Lookup Source Address
$rootWeb = $Context.Web
$List = $rootWeb.lists.getByTitle($ListName)
$fields = $List.Fields;
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
#Load the List
$Context.Load($rootWeb)
$Context.Load($List)
$Context.Load($ListItems)
$context.Load($fields)
$Context.ExecuteQuery()
$listArray = #()
$listArray["DisplayTitle"] = #()
$listArray["Description"] = #()
$listArray["Setting"] = #()
$listArray["HealthAreas"] = #()
$listArray["ResourceType"] = #()
$listArray["ExternalURL"] = #()
$listArray["Active"] = #()
Write-Host "List items are"
foreach ($item in $ListItems)
{
$listArray["DisplayTitle"].Add($item["Title"])
$listArray["Description"].Add($item["File Description"])
$listArray["Setting"].Add($item["Setting"])
$listArray["HealthAreas"].Add($item["Health_x0020_Area"])
$listArray["ResourceType"].Add($item["Resource_x0020_Type"])
$listArray["ExternalURL"].Add($item["External_x0020_file_x0020_path"])
$listArray["Active"].Add($item["Currently_x0020_active_x003f_"])
}
Write-Host "############################"
Write-Host $listArray | ConvertTo-Json
I know there's a gap in my thinking here (maybe I need a hashtable) but just can't see it. The error I'm receiving is:
You cannot call a method on a null-valued expression.
However I can't see where my null variable may be originating from as I've confirmed each item in the loop does contain data (by writing to console).
The error that you receive is not related to SharePoint but to PowerShell. You created the PowerShell array and tried to access its elements like it was associative array/hashtable.
Please try this code (I've tested it with my own list with different column names and it works fine):
#Lookup Source Address
$rootWeb = $Context.Web
$List = $rootWeb.lists.getByTitle($ListName)
$fields = $List.Fields;
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
#Load the List
$Context.Load($rootWeb)
$Context.Load($List)
$Context.Load($ListItems)
$context.Load($fields)
$Context.ExecuteQuery()
$listArray = New-Object System.Collections.Generic.List[System.Object]
Write-Host "List items are"
foreach ($item in $ListItems)
{
$listArray.Add([hashtable]#{
DisplayTitle=$item["Title"];
Description= $item["File Description"];
Setting= $item["Setting"];
HealthAreas= $item["Health_x0020_Area"];
ResourceType= $item["Resource_x0020_Type"];
ExternalURL= $item["External_x0020_file_x0020_path"];
Active= $item["Currently_x0020_active_x003f_"];
}
)
}
Write-Host "############################"
$json = $listArray | ConvertTo-Json
Write-Host $json
I'm trying to create a custom object based on server names from a text file.
The script I have goes and imports the txt file into a Variable. Then runs a foreach server in the servers variable to create the custom object. I would like to be able to output the object's properties as a table that doesn't include the header info each time.
See script and output below:
$SERVERS = gc c:\servers.txt
foreach ($srv in $SERVERS)
{
$Obj = New-Object PsObject -Property`
#{
Computername = $srv
SecurityGroup = (Get-QADComputer $srv).memberof
RebootDay = ((Get-QADComputer $srv).memberof).split(',').split(' ')[2]
Combined = ((Get-QADComputer $srv).memberof).split(',').split(' ').split('=')[1]
RebootTime = $obj.combined.substring(0,4)
}
echo $obj | ft Computername,RebootDay -autosize
}
This is the output currently:
Computername RebootDay
SERVER007 Sunday
Computername RebootDay
SERVER009 Sunday
Computername RebootDay
SERVER003 Sunday
I'd like it to look more like:
Computername RebootDay
SERVER007 Sunday
SERVER001 Sunday
SERVER009 Sunday
TessellatingHeckler was on the right track really. The issue with his code is that you can't pipe a ForEach($x in $y){} loop to anything (not to be confused with a ForEach-Object loop that you usually see shortened to just ForEach like $Servers | ForEach{<code here>}) You don't want to pipe objects to Format-Table one at a time, you want to pipe a collection of objects to it so that it looks nice. So here's the modified code:
$SERVERS = gc c:\servers.txt
$Results = foreach ($srv in $SERVERS)
{
New-Object PsObject -Property #{
Computername = $srv
SecurityGroup = (Get-QADComputer $srv).memberof
RebootDay = ((Get-QADComputer $srv).memberof).split(',').split(' ')[2]
Combined = ((Get-QADComputer $srv).memberof).split(',').split(' ').split('=')[1]
RebootTime = $obj.combined.substring(0,4)
}
}
$Results | FT ComputerName,RebootDay -auto
That collects the objects in an array, then you pass the whole array to Format-Table
Don't put the "ft" (Format-Table) command inside the loop, put it outside, once, at the end. e.g.
$SERVERS = gc c:\servers.txt
$results = foreach ($srv in $SERVERS)
{
$Obj = New-Object PsObject -Property`
#{
Computername = $srv
SecurityGroup = (Get-QADComputer $srv).memberof
RebootDay = ((Get-QADComputer $srv).memberof).split(',').split(' ')[2]
Combined = ((Get-QADComputer $srv).memberof).split(',').split(' ').split('=')[1]
RebootTime = $obj.combined.substring(0,4)
}
$Obj
}
$results | ft Computername,RebootDay -autosize
Edit: Fixed for foreach pipeline bug.
You could possibly neaten it a bit because you don't need to make a new PSObject for a hashtable, and then put the object into the pipeline; you don't need to repeat the Get-QADComputer commands three times. I'm suspicious that the $obj.combined line isn't doing anything - how can you refer to an object inside the properties of the new-object call, before it gets assigned that name? And the repeated splits could probably be combined because it operates on individual characters, not strings.
gc c:\servers.txt | foreach {
$memberof = (Get-QADComputer $_).memberof
#{
Computername = $_;
SecurityGroup = $memberof;
RebootDay = $memberof.split(', ')[2];
Combined = $memberof.split(', =')[1];
# ?? RebootTime = $obj.combined.substring(0,4)
}
} | ft Computername,RebootDay -autosize