Get a list of maibox databases in each DAG - powershell

I have a need to get a list of databases contained in each DAG but I am struggling to get it. If I use
Get-databaseavailabilitygroup
I get a list of the DAGs and the member servers but as soon as I try
Get-databaseavailabilitygroup|get-mailboxdatabase
I get an error saying the DAG name can not be found on the DC.
What am I doing wrong?
We have 3 seperate environments each with different database names and a different number of databases.
I am trying to get a list of the databases in each DAG as this will be passed into a function that works out which DAG to create the mail account on and then creates the account on the database with the least number of users on it.
I want to create it this way so I can use the same script across all environments and it will also cater for new databases
TIA
Andy

That's what Group-Object is for:
Get-MailboxDatabase |
Group-Object MasterServerOrAvailabilityGroup |
Select -ExpandProperty Group

I worked it out but would be interested in anything that is easier
$dags = get-databaseavailabilitygroup
foreach ($dag in $dags){
$mbx = Get-mailboxdatabase | Where-Object {$_.masterserveroravailabilitygroup -EQ $dag}
foreach($db in $mbx){
write-host $db.name
}
}

((Get-MailboxServer) | Where-Object {$_.DatabaseAvailabilityGroup -eq 'EX13DAG'} | Select-Object -Property Name).Name

Related

Build variables from properties in Powershell script

I am trying to build a script to gather the DFSR backlog details of a list of servers.
So far the script will query a text file for a list of servers.
From that list it will use various Powershell commands to determine what replication groups it is a member of and what connections it has. Once I have that data stored in variables I can then use it in dfsrdiag backlog to check the status.
The problem I have is how can I capture and set select properties to variables that I can use to create the dfsrdiag command.
Anyone out there know the best way to select the particular properties and store then to variables in Powershell?
Cheers
Woodsy
Here is a simple example using get-service. You can create an object called $report that contains only the properties you want. You can export $Reportto a CSV.
All you need to do is apply this to your script.
$Services = Get-Service | Where-Object {$_.name -like "A*"}
[Array]$Report = foreach ($service in $Services)
{
[PScustomobject]#{
Name = $service.DisplayName
Shortname = $service.name
Status = $service.Status
StartType = $service.StartType
}
}
$Report | select Name, Shortname, Status, StartType

Find AD security groups on network folders

I am not a programmer, I must of taken a wrong turn! So that's out of the way, how on earth is there not an easy way to take a set of network folders and pipe out a list of AD security groups that are applied to it? I have googled my butt off but there are a million similar questions and i have tested a few scripts but cant get exactly what i want or a lot of errors. We have a top level directory of about 7 folders and security is about 3 levels deep. We want to cleanup unused or orphaned security groups out of AD TOOLS, and try to get a feel of what is used and what is not. Attempting a "Network drive cleanup" at my Organization.
What is the best way to accomplish this? I tried this in PS
Get-ChildItem "\\wfs.company.ca\adv\workgroups\adv services" -recurse | ForEach-Object {Get-Acl $_.FullName} | Export-CSV C:\"adv services".csv
It worked but gave me too much info and not specific Group names.
and i also tried something like this which just produced errors.
# Scope options are Universal, DomainLocal,Global
# Get-GroupMember -Scope DomainLocal
Function Get-GroupMember{
Param(
[parameter(Mandatory=$true)]
[string]
$scope
)
$Groups = Get-ADGroup -Filter {GroupScope -eq $scope -and Members -ne "NULL"} -Properties Name |
Select-Object Name, #{Name="GroupMembers";Expression={(Get-ADGroupMember -Identity "$_" |
Select-Object -ExpandProperty SamAccountName) -join "`n"}}
}
$Groups | Format-Table -AutoSize -Wrap
$Groups | Out-GridView
$Groups | Export-Csv C:\groups.csv -NoTypeInformation
I dont mind putting in the work and research i just dont know where to start.
Any pointers much appreciated.
Thanks!
You could use this to get a unique list of applied identities (groups and users):
(Get-ChildItem "\\wfs.company.ca\adv\workgroups\adv services" -Recurse | Get-Acl).Access.IdentityReference | select -Unique
Furthermore, you could use Get-ADGroup or other ways to check if it's a group or user.

PowerBi List of all workspaces and users within - Newbie Question

My job entails me rotating through different parts of our IT Team. I recently moved to our Analytics department and found myself utilizing Powershell to retrieve data for Power BI. Bear with me here as I am new to this...
My goal is to generate a list of all the workspaces in our organization and the users correlated with each workspace. In general we want to try to get a wider-view on what is taking place within our tenant.
I've tried each of these methods with no success.
https://powershell.org/forums/topic/powerbi-query-trying-to-list-workspaces-and-users/
How to extract all PowerBI users and workspace access using the PowerBI API or Azure Portal?
Ive also tried to run the command below but get the error of "System.Linq.Enumerable+WhereSelectListIterator`2" when it should be the user info.
Get-PowerBIWorkspace -Scope Organization -Include All -All |select-object | export-csv c:\temp\powerbiworkspaces.csv -NoTypeInformation
Any help would be greatly appreciated..
Thank you in advance,
After struggling a bit I ended up re-purposing the code from the one link. This worked for me therefore ill leave this up.
#Gets User, workspace, workspaceId
Get-PowerBIWorkspace -Scope Organization -Include All -All |
ForEach-Object {
$Workspace = $_.name
$WorkspaceId = $_.Id
foreach ($User in $_.Users) {
[PSCustomObject]#{
Workspace = $Workspace
WorkspaceId = $WorkspaceId
User = $User.accessright
Identifier =$user.Identifier}}} | Export-CSV "C:\Temp\WorkspaceDetails.csv" -NoTypeInformation

Sort AWS Instances by Tag:Name in PowerShell after Get-EC2Instance Cmdlet

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

office 365 Powershell

The HR department has 5000 unliscenced users. I want to remove them all.
I'm confused between two powershell commands and want to use the fastest one:
## 1
Get-MsolUser -UnlicensedUsersOnly | Remove-MsolUser -force
## 2
Get-MsolUser -All | where {$_.department -eq "HR"} | Remove-MsolUser -force
Although I don't think that there would be a huge difference in processing time since you're just using two different ways of retrieving a list of data, it seems like it would be a little faster to just pull the unlicensed users, as opposed to pulling all users and then filtering them based on department. However, are you sure that there aren't any unlicensed users in other departments that you may not want to delete?
Regarding which command is faster, have you tried using the Measure-Object command to see how long each one will take? You could just measure the Get-MsolUser command to confirm the difference.
Measure-Object {Get-MsolUser -UnlicensedUsersOnly}
Measure-Object {Get-MsolUser -All | where {$_.department -eq "HR"}}
Remove-MsolUser -force should take the same amount of time for both options. Also, I think you'll need to put the Remove-MsolUser command in a foreach loop:
foreach($user in Get-MsolUser -UnlicensedUsersOnly | where {$_.department -eq "HR"})
{Remove-MsolUser -ObjectId $user.ObjectId.guid -force}
https://technet.microsoft.com/en-us/library/ee176899.aspx