I am having an issue with the output from one property as a comma separated value instead of a list in the out-gridview. Is there a way to have a value be added to the output as a list instead of a single line?
.'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
Connect-ExchangeServer -Auto -AllowClobber
do {
Write-Host
Write-Host
Write-Host
$name = Read-Host "What is the user's first name or letter?"
Write-Host
Write-Host
Write-Host
$list = Get-ADUser -Filter * | ? {$_.SamAccountName -match $name} |
select #{N="Highlight a User & Press Ctrl+C then Ctrl+V"; E={$_.SamAccountName}} |
sort SamAccountName |
Out-String
Write-Host -ForegroundColor Green $list
$box = Read-Host "Copy and paste the mailbox you want to see?"
$user = $box
$mailbox= Get-Mailbox -Identity $user | Get-MailboxStatistics |
Sort totalitemsize -desc |
select #{Name="User"; Expression={$_.DisplayName}},
#{Expression={"{0:N2}" -f($_.TotalItemSize.Value.ToMb()/1024)};label=”Mailbox Size in GB”},
#{Expression={"{0:N0}" -f($_.TotalItemSize.Value.ToMb())};label=”Mailbox Size in MB”},
#{Name="Message Count"; Expression={"{0:N0}" -f($_.itemcount)}},
#{Name="Database"; Expression={$_.DatabaseName}}
$folders= Get-MailboxFolderStatistics $user |
? {$_.ItemsInFolder -gt 0} |
Sort ItemsInFolder -Descending |
Select Name,
#{N="Items in Folder"; E={"{0:N0}" -f($_.ItemsInFolder)}},
#{N=”Folder Size in MB”;E={"{0:N0}" -f($_.FolderSize.ToMb())}}
$object= [PSCustomObject]#{
User = $mailbox.'User'
'Mailbox Size in MB'= $mailbox.'Mailbox Size in MB'
'Message Count' = $mailbox.'Message Count'
Database = $mailbox.Database
Name = $folders.Name
}
$object | Out-GridView
Write-Host
Write-Host
Write-Host
$runagain = Read-Host "Would you like to get another user's folder size?" | Out-String
Write-Host
Write-Host
}
while($runagain -like "y*")
Any help to get the $folders.Name to show as a list within the same out-Gridview would be great.
Thank you
You're probably looking for:
Name = $($folders.Name -join [Environment]::Newline)
This way you are not using an object anymore but are manually creating a list by joining the elements with a new line.
Related
Actually i have a powershell script which analyzes the ntfs permissions on a file server. i enter the group name, specify the folder and afterwards i get the list. now i want to implement a active directory picker dialog like this instead of typing the group name
is there any powershell code to add to my script? this is what i have.
$gruppe = read-Host "group name"
Function Get-Folder($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
$Ordnername = New-Object System.Windows.Forms.FolderBrowserDialog
$Ordnername.Description = "Ordner auswählen"
$Ordnername.rootfolder = "MyComputer"
if($Ordnername.ShowDialog() -eq "OK")
{
$Ordner += $Ordnername.SelectedPath
}
return $Ordner
}
$o = Get-Folder
write-host
function Get-FolderRightsForAccount([string]$dn, [string]$rootfolder, [switch]$includeInheritedRights){
$sids = #()
$sids += (Get-ADObject $dn -Properties objectSid).objectSid.Value
$sids += Get-ADPrincipalGroupMembership $dn | select -Expand GroupName
$inherited = #{$true=($true,$false);$false=$false}[$includeInheritedRights.IsPresent]
(Get-ACL $rootfolder).Access | ?{try{$_.IdentityReference.Translate([System.Security.Principal.SecurityIdentifier]).Value -in $sids -and $_.IsInherited -in $inherited}catch{}} | select #{n='Folder';e={$rootfolder}},AccessControlType,#{n='Rights';e={$_.FileSystemRights}}
gci $rootfolder -Recurse -Directory -PipelineVariable f | %{
(Get-ACL $_.Fullname).Access | ?{try{$_.IdentityReference.Translate([System.Security.Principal.SecurityIdentifier]).Value -in $sids -and $_.IsInherited -in $inherited}catch{}} | select #{n='Folder';e={$f.Fullname}},AccessControlType,#{n='Rights';e={$_.FileSystemRights}}
}
}
Get-FolderRightsForAccount -dn (Get-ADGroup $Gruppe).DistinguishedName -rootfolder $o -includeInheritedRights | ft -AutoSize
It's not a picker like shown, but could be even more useful. You can utilize the cmdlet Out-GridView. You can allow choosing many or limit to one item. You can filter and/or sort the list as well.
$selectedgroup = Get-ADGroup -Filter * |
Select-Object -Property Name, GroupCategory,GroupScope, SamAccountName,DistinguishedName |
Sort-Object -Property Name | Out-GridView -OutputMode Single -Title "Please choose a group"
if(!$selectedgroup){
Write-Host "No group was selected" -ForegroundColor Yellow
}
I'm trying to list only unique HomeDrive for all users in a Universal Security group and remove nested groups errors.
Thanks for your help.
Denis
I've tried .TrimEnd(':'), can't seem to figure out where to put it
$Group = "Universal Security group"
$HomeDrive = Get-ADGroupMember $Group | `
ForEach-Object {
$UserName = $_.Name
Try {
#$ErrorActionPreference = "Stop"
Get-ADUser $UserName -Properties HomeDrive | Select HomeDrive
}
Catch {
Write-Host "Found a nested Group."
}
} | Sort-Object -Property 'HomeDrive' -Unique | Format-Table -HideTableHeaders | Out-String
Write-Host "$HomeDrive" -BackgroundColor DarkRed
The script does work but some users have their homedrives listed as only F while most are listed as F:. Basically making a lot of double entries and I do want the output to be only F. Also it generates 7 spaces after the :, That's why I have the background color.
Something like this:
$group = "Universal Security group"
$homeDrives = Get-ADGroupMember $Group |
ForEach-Object {
if ($_.ObjectClass -eq "User")
{
$user = Get-ADUser $_.Name -Properties "HomeDrive"
$homeDrive = $user.HomeDrive.Trim().TrimEnd(":")
return $homeDrive
}
} | Sort-Object -Unique
foreach ($homeDrive in $homeDrives)
{
Write-Host "Found home drive: $homeDrive" -BackgroundColor DarkRed
}
Here is the issue I am facing. I am looking through a group variable. It pulls the information but it doesn't display it line by line. Instead, it will present the last groups info sometimes before the group members and then sometimes after. It seems to be dropping the process into a subprocess then continuing to the next line which might process faster. I know I am missing something simple. Here is the code:
foreach ($Groups in $Groups) {
Write-Host "---------- Group Info ----------" -ForegroundColor Yellow
$TempGroup = $Groups
$GroupInfo = Get-ADGroup -Filter "name -like '$TempGroup'" -Properties *
$GroupInfo | select Name,GroupScope,GroupCategory,mail,Created,Modified,DistinguishedName,Description | sort name
write-host "---------- Members ----------" -ForegroundColor Green
Get-ADGroupMember -Identity $GroupInfo.samaccountname -Recursive | select name,samaccountname,description | sort name | ft -AutoSize
write-host "---------- Nested Groups ----------" -ForegroundColor Green
Get-ADPrincipalGroupMembership -Identity $GroupInfo.samaccountname | select name,GroupScope,GroupCategory | sort name | ft -AutoSize | Wait-Job
}
Groups is an array.
Mixing Write-Host with default output from uncaught objects can give you strange results. Write-Host writes directly to the host (console), while the returned objects are sent down the pipeline until it reaches Out-Default (hidden cmdlet) which formats the objects and outputs them to the console (default output).
Pipe the objects directly to Out-Host to avoid this delay. Try:
foreach ($Group in $Groups) {
Write-Host "---------- Group Info ----------" -ForegroundColor Yellow
$TempGroup = $Group
$GroupInfo = Get-ADGroup -Filter "name -like '$TempGroup'" -Properties *
$GroupInfo | select Name,GroupScope,GroupCategory,mail,Created,Modified,DistinguishedName,Description | sort name | Out-Host
write-host "---------- Members ----------" -ForegroundColor Green
Get-ADGroupMember -Identity $GroupInfo.samaccountname -Recursive | select name,samaccountname,description | sort name | ft -AutoSize | Out-Host
write-host "---------- Nested Groups ----------" -ForegroundColor Green
Get-ADPrincipalGroupMembership -Identity $GroupInfo.samaccountname | select name,GroupScope,GroupCategory | sort name | ft -AutoSize | Out-Host
}
Also...
Wait-Job does nothing here because it never recieved a job-object from the cmdlets earlier in the pipeline.
You're using the same variable for the current object and the array in your foreach-loop. I can't remember if PowerShell understands your referring to the current item inside the loop, but at least it makes your code hard to read.
I'm not shure, but I think foreach($Groups in $Groups) could be a problem,
You should use foreach($Group in Groups){
}
Or do it in more Powershell syntax:
$Groups | Foreach{
Write-Host "---------- Group Info ----------" -ForegroundColor Yellow
$TempGroup = $_
$GroupInfo = Get-ADGroup -Filter "name -like '$TempGroup'" -Properties *
$GroupInfo | select Name,GroupScope,GroupCategory,mail,Created,Modified,DistinguishedName,Description | sort name
write-host "---------- Members ----------" -ForegroundColor Green
Get-ADGroupMember -Identity $GroupInfo.samaccountname -Recursive | select name,samaccountname,description | sort name | ft -AutoSize
write-host "---------- Nested Groups ----------" -ForegroundColor Green
Get-ADPrincipalGroupMembership -Identity $GroupInfo.samaccountname | select name,GroupScope,GroupCategory | sort name | ft -AutoSize | Wait-Job
}
I'm trying to export some group memberships all to one CSV file so I can find users who are not in our domain. Everything works great, but when all the outputs get appended I can't see what group each entry is in. Here's what I have now.
$Groups = import-csv "C:\users\USER\desktop\secgroupinput2.csv"
foreach($item in $Groups)
{
Get-ADGroupMember -Server "SERVERDC" -Identity $item.directoryname | export-csv "C:\users\USER\desktop\realexport.csv" -Append
}
How can I add a row between appends with the group name, likely from the import?
Thanks!
I did something similar in the past. Hope this code helps :
Function ADGroupMembers
{
$group = get-content C:\Pshell\PM\group.txt
$i =25
do{
if (get-QADgroup $group[$i] -Empty 0)
{write-output ''; Write-Output -inputobject "The $($group[$i]) group members :"; write-output '';
get-QADGroupMember $group[$i] -IncludeAllProperties | Format-Table -AutoSize DisplayName, Type, Office, Company, Department, Title, WhenCreated | Out-String -Width 4096;
write-output ''}
else {Write-Output -inputobject "*** Member not found in the $($group[$i]) group!"; write-output ''}
$i +=1}
while ($i -ne $group.length)
}
ADGroupMembers | out-File 'c:\Pshell\PM\groupmemberdetails.txt'
I want to limit the results of the $Groups variable below to only return members of a specific group. This code retrieves the groups a computer belongs to. I am interested only in those that are members of any AD group that contains "SCCM".
I tried line 5 but that doesn't work. What is the correct method to do this? Thanks.
$SamAccountNames = (Get-ADComputer Computer).SamAccountName
$OSInfo = Get-WmiObject -Class Win32_OperatingSystem -ComputerName Computer
$ServerGroupMemberShipList = Get-ADPrincipalGroupMembership $SAMAccountNames | Sort SAMAccountName
$Groups = $ServerGroupMemberShipList.Name
#$Groups = $ServerGroupMemberShipList.Name | Select {$ServerGroupMemberShipList.Name -like "SCCM"}
Write-Host " "
Write-Host "Checking Software Updates Compliance on" $Computer.ToUpper() -ForegroundColor Yellow -NoNewline
Write-Host $OSInfo.Caption
$Groups
Rather than using the Select-Object cmdlet, try using the Where-Object cmdlet for filtering or ? for short. The $_ variable is the current object being processed by the cmdlet.
$Groups = $ServerGroupMemberShipList | Where-Object { $_.Name -like "SCCM" }
or
$Groups = $ServerGroupMemberShipList | ? { $_.Name -like "SCCM" }