(Updated Title as I wrote it on the fly)
Got very stumped by this so far and relatively new to powershell.
I have the below script:
cd e:
Import-CSV e:\UserList.csv | Foreach-Object {Get-MailboxFolderPermission -Identity "$($_.account):\Kalender" -User Default}
| select-object User,AccessRights | export-csv AccessList.csv
However whenever I run it I get the Object Type returned for the AccessRights parameter e.g.:
User,"AccessRights"
Default,"System.Collections.ObjectModel.Collection`1[Microsoft.Exchange.Management.StoreTasks.MailboxFolderAccessRight]"
Default,"System.Collections.ObjectModel.Collection`1[Microsoft.Exchange.Management.StoreTasks.MailboxFolderAccessRight]"
Anyone know how I can get the value out as it prints to powershell? e.g.:
User AccessRights
---- ------------
Default {AvailabilityOnly}
Not quite sure why it's behaving that way, but this seems to be a usable workaround:
Import-CSV e:\UserList.csv |
Foreach-Object {
Get-MailboxFolderPermission -Identity "$($_.account):\Kalender" -User Default
} | select-object User,#{l='AccessRights';e={$_.AccessRights}} |
export-csv AccessList.csv
Related
I'm trying to output a list of members of a shared mailbox by selecting displayName. I have the below script which will output the users userPrincipalName(UPN), the problem with this is our users login with an ID number, for example when I run the script I get the output: 666777#mycompany.com I would like to output the actual name of the user so I don't have to go to Active Directory and type in 666777 to find out the name of the user. The script I use is:
Get-Mailbox -Identity "NorthOffice#mycompany.com" -ResultSize:Unlimited | Get-MailboxPermission | Select-Object User
The above script outputs for example:
User
----
666777#mycompany.com
I've tried using: | Select-Object displayName but these properties show as empty/blank
Anyone know a way around this?
EDIT
I've found a way to do it but it's quite long winded:
I export the results of the script into a csv
Get-Mailbox -Identity "NorthOffice#mycompany.com" -ResultSize:Unlimited | Get-MailboxPermission | Select-Object User | Export-csv c:\myfolder\NorthOffice.csv
I then change the header of the csv to userPrincipalName and save. I then import the csv and run a 'ForEach' like below:
Import-Csv C:\myfolder\NorthOffice.csv | ForEach {
Get-ADUser -Filter "userPrincipalName -eq '$($_.userPrincipalName )'" -Properties Name, SamAccountName | Select Name,SamAccountName
} | Export-CSV -path C:\myfolder\NorthOfficeOutput.csv -NoTypeInformation
Does anyone know a quicker way to achieve this?
Try this:
Get-MailboxPermission -Identity "NorthOffice#mycompany.com" | select -ExpandProperty User | findstr /v /c:"NT AUTHORITY\SELF" | foreach {Get-ADUser -Identity ($_ -split "#")[0] -Properties * | select displayName} | sort
Get-ADUser won't accept UserPrincipalName as Identity (but it will accept SamAccountName), so we split the SMTP address at the "#" to get just the first part. (This, of course, assumes that the UPN and SMTP address are the same, and the first part of the SMTP address is the same as the SamAccountName.)
If this does not work for you, and you don't need to drop back to AD, you can just use Get-Mailbox to resolve the DisplayName directly from Exchange:
Get-MailboxPermission -Identity "NorthOffice#mycompany.com" | select -ExpandProperty User | findstr /v /c:"NT AUTHORITY\SELF" | foreach {Get-Mailbox -Identity $_ | select displayName} | sort
It has been many moons since I have last done this and I am having problems exporting some commands to CSV. The biggest one that is getting me right now is
$ADGroupList = Get-ADGroup -Filter * -property * | Select Name -ExpandProperty Name | Sort
ForEach($Group in $ADGroupList)
{
Write-Host "Group: "$Group.Name
Get-ADGroupMember -Identity $Group | Select Name -ExpandProperty Name | Sort
Write-Host ""
}
Export-Csv -path "c:\Temp\test675.csv"
Works fine with out trying to export but the second I try to export the command will run and either generate a blank file or no file at all.
I am able to run other commands with out a issue exporting them to csv. Thanks for any help in advance.
Tim,
From what I can see you're not giving Export-Csv anything to write. Try this:
$ADGroupList = Get-ADGroup -Filter * -property * | Select Name -ExpandProperty Name | Sort
ForEach($Group in $ADGroupList)
{
Write-Host "Group: "$Group.Name
Get-ADGroupMember -Identity $Group |
Select Name -ExpandProperty Name |
Sort |
Export-Csv -path "c:\Temp\test675.csv"
Write-Host ""
}
I'd test this but I don't have access to ActiveDirectory.
Also the Write-Hosts you probably don't want in the .csv file as they won't play well with the format, headers & columns.
HTH
Ok, so I'm a beginner in Powershell.
I'm at work so I wanted to auto some things. My work with this script is to select those specific users and their password expiration date, format the date to be human readable and then save all that in a txt file to be analized.
My current code is this:
$List = Get-Content D:\Users\c05896\Desktop\VIPS.txt
$Users = #()
foreach ($Item in $List){
$Users += Get-ADUser -Filter * -Properties "msDSUserPasswordExpiryTimeComputed"| where {$_.Name -like "*$Item*"}}
$Users | Sort-Object | Select-Object Name, #{Name="Password expires";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed" )}}
Out-File -filepath D:\Users\c05896\Desktop\VIPLista.txt -InputObject $Users
So, the script above is what I wrote.
The output WITHIN the Powershell ISE is at follows:
Name Password expires
---- ---------------
User 1 10/11/2010 01:39:53 p.m.
User 2 21/10/2018 10:21:43 a.m.
User 3 21/10/2018 08:38:23 a.m.
. .
. .
. .
That's what I want to output in the txt file, but instead I got this format:
DistinguishedName : CN=USER LASTNAME
OU=VIPs,OU=Users
Enabled : False
GivenName : USER LASTNAME
msDS-UserPasswordExpiryTimeComputed : 129192702049912335
Name : USER
ObjectClass : user
ObjectGUID : bb033273-6117-470b-9f07-aee2885a45bc
SamAccountName : B00057
SID : S-1-5-21-808411757-1154953693-317593308-27012
Surname : USER
UserPrincipalName : XXXXX
I erased all names and lastnames just for privacy.
So, the question is what can I do to get the Out-File to display the information as it shows on Powershell ISE?
Thank you very much for your answers
# --> $Users = $Users | ...
$Users = $Users | Sort-Object | Select-Object Name, #{Name="Password expires";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed" )}}
Out-File -filepath D:\Users\c05896\Desktop\VIPLista.txt -InputObject $Users
# or
# combine ISE output with file output in one command using Tee-Object
$Users | Sort-Object |
Select-Object Name, #{Name="Password expires";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed" )}} |
Tee-Object -FilePath D:\Users\c05896\Desktop\VIPLista.txt
Nas has a good answer for you regarding how to meet your objective. I'm going to try to answer the why.
When you used Get-ADUser to retrieve your user objects a number of properties are returned by default. You were aware of that and used Select-Object to select which attributes to return and you even added a new custom attribute.
The Select-Object output is what displayed in the ISE (and would have displayed in a console if you weren't using the ISE as well).
What you did not do is actually modify the objects that were stored in the $Users array so when you sent those objects to Out-File it received all the attributes of the objects.
Both of the options that Nas gives you above modify the objects in $Users while they are in the pipeline so that when they are sent to Out-File only the attributes you wanted to output to the file are remaining. His first example actually replaces the original $Users array with a new $Users array so that if you subsequently examine it, only your specified attributes will remain. In his second example, using Tee-Object, $Users still remains unmodified and if you examined the objects you would see that they still retain DistinguishedName, Enabled, GivenName, etc.
I am trying to create a script that will do the above,
I did something like that:
Search-ADAccount -AccountDisabled | Export-Csv -Path C:\csv\DisabledUsers.csv -NoTypeInformation
$csv = Import-Csv -Path c:\csv\DisabledUsers.csv
foreach ($SamAccountName in $csv)
{
Get-ADPrincipalGroupMembership | format table | Export-Csv -Path C:\csv\DisabledUsersGroupM.csv -NoTypeInformation
}
It is work fine until the foreach part. Seems like no values imported from the CSV.
I get this:
cmdlet Get-ADPrincipalGroupMembership at command pipeline position
Supply values for the following parameters:
(Type !? for Help.)
Identity:
What am I doing wrong?
You're calling Get-ADPrincipalGroupMembership without a required parameter (the identity of the object whose group membership you want to obtain).
Change
Get-ADPrincipalGroupMembership
to
Get-ADPrincipalGroupMembership $SamAccountName
Also, the Export-Csv inside the loop would overwrite the output file with every iteration, so you'd end up with just the groups of the last user. Add the parameter -Append to avoid this.
With that said, a much simpler approach would probably be a pipeline like this:
Search-ADAccount -AccountDisabled |
Select-Object SamAccountName, #{n='Groups';e={(Get-ADPrincipalGroupMembership $_.SamAccountName | Select-Object -Expand Name) -join ';'}} |
Export-Csv 'C:\csv\DisabledUsers.csv' -NoType
I've got a requirement. As part of which, I'm trying to export the ACLs on an Exchange mailbox to a CSV and importing it a bit later. One of the attribute on Exchange mailbox permissions is an array so our CSV header has $_.AccessRights as the array's header since we use Select {$_.AccessRights} to export this array.
The problem arises when importing because Import-Csv won't like this. I'm trying to find a way to edit just the $_. out of the header only for that attribute and re-write the file before importing it again. Have tried Import-Csv -Header and Get-Object with -replace with no luck so far. Any other thoughts?
Edit: I'm actually not just trying to do Import-Csv but actually trying to run run Add-MailboxPermission - Exchange script to import ACLs on a mailbox. So, the code I'm trying to run is
Import-Csv $ACLExportFile | foreach { Add-MailboxPermission -Identity $_.Identity -user $_.user -AccessRights $_.AccessRights | Out-File $LogACLImport -append }
Where $ACLExportFile is the permissions file I exported earlier using
Get-Mailbox -identity $OldUser | Get-MailboxPermission | Select {$_.AccessRights}, Deny, InheritanceType, User, Identity, IsInherited, IsValid | Export-Csv $ACLExportFile
Sample file that I got is
Uploaded to Google Drive here
Not sure to understand the question.
Have a look to Select-Object CmdLet, you can provide a property parameter that can be a new calculated property. To create a calculated, property, use a hash table. Valid keys are:
-- Name (or Label)
-- Expression or
-Property #{Name="AccessRights";Expression={$_.AccessRights}}
Edit :
In you case, when you export you should use :
Get-Mailbox -identity $OldUser | Get-MailboxPermission | Select #{Name="AccessRights"; Expression={$_.AccessRights}}, Deny, InheritanceType, User, Identity, IsInherited, IsValid | Export-Csv $ACLExportFile
So that you can import like this :
Import-Csv $ACLExportFile | foreach { Add-MailboxPermission -Identity $_.Identity -user $_.user -AccessRights $_.AccessRights | Out-File $LogACLImport -append }