List of users to match a list of job titles in AD - arrays

I have a list of Job titles in a csv file and need to match these to whichever user has the title in AD.
CSV Contents is like this:
JobTitle1
JobTitle2
JobTitle3
The following command will work to get a list of all users with the JobTitle1 title:
Get-ADUser -Filter "Title -like 'JobTitle1'"
However I can't get this to work with the CSV file as an array, it doesnt return any output:
$Titles = Get-Content -Path "c:\jobtitles.csv"
ForEach ($Title In $Titles)
{
Get-ADUser -Filter {Title -like '$Title'}
}

In PowerShell a variable within single quotes doesn't get populated so you have to use double quotes instead - or just omit them:
$Titles = Get-Content -Path "c:\jobtitles.csv"
ForEach ($Title In $Titles)
{
Get-ADUser -Filter {Title -like $Title}
}

Related

Verify if a disabled user account has a shared mailbox

A co-worker and I are trying to develop a script that will show us shared mailboxes attached to a disabled user account and when the account was disabled by looking at the Last password change date time property on their AD account in Azure. When we run this half the time it only grabs the info for the shared mailboxes attached to disabled user the other it pulls no data to the csv. Any help is appreciated.
$DisabledUsers = Get-AzureADUser -Filter {(Enabled -eq $False)} | Select-Object Lastpasswordchangedatetime
$SharedInboxes = Get-Mailbox -RecipientTypeDetails SharedMailbox $Results = #()
Foreach ($SharedInbox in $SharedInboxes)`
{
  $InboxPermissions = Get-MailboxPermission -Identity $SharedInbox.Identity | Where-Object {$_.User.ToString().Split("\")[1] -in $DisabledUsers}
  If ($InboxPermissions)
  {
    $Result = [PSCustomObject]#{
      SharedInbox = $SharedInbox.Identity
      DisabledUser = $InboxPermissions.User.ToString().Split("\")[1]
    }
    $Results += $Result
  }
} $Results | Export-Csv -Path "C:\SharedInboxforDisabledAccounts.csv" -NoTypeInformation
Expected a csv to generate with the mailboxes attached to disabled users and the date the password was last changed on the account indicating when the user was disabled

Bulk adding users to AzureAD Script error

The goal of the script is to run and add all users with a specific job title to an AzureAD Group.
I've tested the variable and it grab the right people and will display if I write the variable to the screen.
The error message I get states "ADD-AzureADGroupMember : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter.
#Grab all users with the job title IT Support Technician
$User = (Get-AzureADUser -All $true |
Where-Object -FilterScript {$_.JobTitle -eq "IT Support Technician"}).ObjectId
#Grab the ObjectID of the AzureADGroup
$Branch = (Get-AzureADGroup -SearchString "Test SG").ObjectId
#Add each of the Users to the AzureADGroup
$User | ForEach-Object {
Add-AzureADGroupMember -ObjectId $Branch -RefObjectId $User
}
You're iterating over $user then trying to add all users within your Foreach-Object loop.
To reference the current pipeline item you can use $_ or $PSItem
$User | ForEach-Object {
Add-AzureADGroupMember -ObjectId $Branch -RefObjectId $_
}
Documentation on Automatic Variables
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.2#_

Exporting Multi-Valued Attributes in PowerShell to Excel - Attempting to Automate Using an Array of Hash Tables

I am trying to automate the unwrapping of multi-valued attributes in PowerShell for the purpose of exporting the information to an excel sheet. The known solution for enabling multi-valued exporting to excel is to manually create a hash table for each multi-valued attribute, which works just well. Example:
Get-ADForest -Server <servername> |
Select-Object #{Name='ApplicationPartitions';Expression={$.ApplicationPartitions}},
DomainNamingMaster,
#{Name="Domains";Expression={$.Domains}},
ForestMode,
#{Name="GlobalCatalogs";Expression={$.GlobalCatalogs}},
Name,
PartitionsContainer,
RootDomain,
SchemaMaster,
#{Name="Sites";Expression={$.Sites}} |
Export-Csv $home'AD Forest Information.csv'
However, I would like to automate this for commands that have many multi-valued attributes. Here is what I have so far (I am using Get-ADForest for testing):
$Objects = #()
$Object = #{}
Get-ADForest | Get-Member | Where-Object {$_.membertype -eq 'property'} |
Select-Object Name |
ForEach-Object {$Object = #{Name=$_.Name;Expression="$" + "_" + "." + $_.Name} ; $Objects +=$Object}
Write-Output $Objects
Get-ADForest -Server <servername> | Select-Object $Objects | Export-Csv $home\'AD Forest Information.csv'
The issue is that the exported .csv has the correct column titles, but the values of the attributes are still Microsoft.ActiveDirectory.Management.ADPropertyValueCollection like they would be without unwrapping the attribute.
Please let me know if you have any questions.
Any suggestions would be awesome.
Thank you!
Austin
Try this for your example, let me know if this is what you expect as output for your csv:
cd $Home\Documents
function parseADObject{
param(
[cmdletbinding()]
[parameter(mandatory,valuefrompipeline)]
$InputObject
)
$properties=($inputObject|gm -MemberType Property).Name
$parsedObject=#{}
foreach($prop in $Properties)
{
$thisProp=$inputObject.$prop
if($thisProp -and $thisProp.GetType() -match 'Collection|\[]')
{
$val=($inputObject.$prop|out-string).Trim()
}
elseif($thisProp -and $thisProp.GetType() -match 'Int|Boolean|Double')
{
$val=$thisProp
}
elseif($thisProp)
{
$val=($thisProp.ToString()).Trim()
}
else
{
$val=$null
}
$parsedObject.$prop=$val
}
return $parsedObject|%{New-Object PSObject -Property $_}
}
Get-ADForest|parseADObject|Export-Csv test.csv -NoTypeInformation;ii test.csv
Explanation, I'm gonna use ADForest for this example:
First you get the properties of the object you want to parse:
$adforest=Get-ADForest
PS C:\> $properties=($adforest|gm -MemberType Property).Name
PS C:\> $properties
ApplicationPartitions
CrossForestReferences
DomainNamingMaster
Domains
ForestMode
GlobalCatalogs
Name
PartitionsContainer
RootDomain
SchemaMaster
Sites
SPNSuffixes
UPNSuffixes
Then you loop on the properties of your object asking for the type of object:
PS C:\> foreach($prop in $properties){$adforest.$prop.gettype()}
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False ADPropertyValueCollection System.Collections.CollectionBase
True False ADPropertyValueCollection System.Collections.CollectionBase
True True String System.Object
True False ADPropertyValueCollection System.Collections.CollectionBase
True True ADForestMode System.Enum
True False ADPropertyValueCollection System.Collections.CollectionBase
True True String System.Object
True True String System.Object
True True String System.Object
True True String System.Object
True False ADPropertyValueCollection System.Collections.CollectionBase
True False ADPropertyValueCollection System.Collections.CollectionBase
True False ADPropertyValueCollection System.Collections.CollectionBase
And then you're just asking what type of object it is:
If the type matches 'Collection or []' then you're passing the value of that property to Out-String this is useful for properties like proxyAddresses on the AD Users.
ElseIf the type matches a Boolean or Integer or Double then you're leaving the value as is without any changes.
ElseIf the the property has any Value, meaning, is not $null, I'm using the ToString() method. This is useful for properties like ObjectGUID or ObjectSID
Notice I'm using Trim() each time I use Out-String or ToString() to eliminate any empty blank spaces at the beginning or the end
The value of each property loop, stored in the $val variable is then being added to the hashTable $parsedObject and then after looping is finished the variable is being converted to a PSObject (if you don't do this the export will not show as you expect it, it will show the type of the values instead of the actual data).

Working with Arrays / GetWebVirtualDirectory in PowerShell

I am new to PowerShell, so I got this far
$sites = Get-Website
$OrderSite = $sites | Where Name -eq 'SiteName'
$directories = Get-WebVirtualDirectory -Site $OrderSite.Name
So that $directories looks like
Name Physical Path
---- -------------
Profile C:\Path1
WebMain C:\Path1
Now I am stuck
$directories[0] =
Name Physical Path
---- -------------
Profile C:\Path1
But $directories[0].Name, $directories[0]["Name"], $directories |Where Name -eq 'Profile', $directories |Where Name -like 'P*' and $directories | ForEach {$_.Name } are all null
However, $directories | ForEach {$_ } and $directories |Where Name -like '*'
Return the full list.
So what is the correct way to filter or Access the property Name or how do I verify what the names or properties of my array real are?
Unfortunately I can't test this as no IIS Provider on my PC.
I believe this will get you what you're after, based on what works and doesn't work in the question:
$directories | Where-Object {Path -eq 'Profile'}
Where-Object expects a statement {...}

JSON Array in PowerShell

Am trying to generate the below JSON string in PowerShell:
[
{
"FirstName": "Test",
"LastName": "Account2"
}
]
The code I have at the moment in PowerShell is:
$Body = #{
#{
FirstName='Test'
LastName='Account2'
}
}
The error I get with this is: Missing '=' operator after key in hash literal.
The outer [] is a list in JSON and you're trying to use #{}, which is a hashtable in PowerShell. Use #() which is an array (list) in PowerShell:
$Body = #(
#{
FirstName='Test'
LastName='Account2'
}
)
ConvertTo-Json -InputObject $Body
(and I use -InputObject instead of piping, because PowerShell is obsessed with flattering lists, and drops the list otherwise).
I had some problems adding new items to the Powershell list. I tried to add an item using the .add() function. This returned the error "Collection was of a fixed size."
Adding items to the list is just simple by using the += symbol.
$Body += #{FirstName='Test2'; LastName='Account3'}
That simple.

Resources