I wrote this small script to pull the office property from get-user by piping the exchange mailbox object.
$server = "tms08"
$arrUser = get-mailbox -Server $server |Get-MailboxStatistics |select identity
foreach ( $id in $arrUuser)
{
$office = get-user -Identity $id.Identity |select office
$out += $id.Identity
}
$out
I don't get any output or red errors. just the warning:
WARNING:There is no data to return for
the specifed mailbox
'Globe/MobileElect Usertemplate',
because it has not been logged on to.
WARNING:By default only the first 1000
items are returned. To change the
number of items returned, specify the
parameter "-ResultSize". To return all
items specify "-ResultSize Unilimited"
(Note: REturning all items maytake a
long time and consume a large amount
of memory depending on the actual
number of items). It is not
recommended to store the results in a
variable; instead pipe the results to
another task or script to perform
batch changes.
Any ideas on what might be causing this?
My goal is to develop a script which executes once a day via scheduled task which compiles all mailbox names, mailbox sizes, totalitems, totaldeleted items, along with their office and description fields (from active directory).
I'm guessing the get-qaduser is part of the quest powershell addon. I'll install it locally and give it a try..
the identiy property seems to give a number similar to the guid which looks like
1234as01-4f54-1234-b1df-f1df1df12d2d
I tried running
get-user -identity 1234as01-4f54-1234-b1df-f1df1df12d2d
and it found a name (joey blasio) and recipient type (usermailbox)
then i ran
get-user -Identity 1234as01-4f54-1234-b1df-f1df1df12d2d | select displayname, distinguistedname
Displayname (Joey Blasio ) and DistinguishedName (CN=Joey
Blasio,OU=EWE,DC=BLA-1,DC=net)
It is done by DisplayName
$exchangeservers = Get-MailboxServer
$AllUsers = #()
$AllUsersEmail = #()
foreach ($server in $exchangeservers)
{
$AllUsers += Get-Mailbox -Server $server |Get-MailboxStatistics |select servername,displayname,itemcount,totalitemsize
}
foreach ($user in $AllUsers)
{
$obj = new-object psObject
$mailinfo = get-mailbox -identity $user.displayname |select PrimarySMTPAddress,Office, DistinguishedName
$tmp = [adsi]("LDAP://" + $mailinfo.DistinguishedName)
$obj |Add-Member -MemberType noteproperty -Name "Server Name" -Value $user.ServerName
$obj |Add-Member -MemberType noteproperty -Name "Display Name" -Value $user.DisplayName
$obj |Add-Member -MemberType noteproperty -Name "Item Count" -Value $user.ItemCount
$obj |Add-Member -MemberType noteproperty -Name "Total Item Size" -Value $user.TotalItemSize
$obj |Add-Member -MemberType noteproperty -Name "Email Address" -Value $mailinfo.PrimarySMTPAddress
$obj |Add-Member -MemberType noteproperty -Name "Office" -Value $mailinfo.Office
$obj |Add-Member -MemberType noteproperty -Name "Description" -Value $tmp.description
$AllUsersEmail += $obj
}
$AllUsersEmail |Export-Csv c:\test.csv -NoTypeInformation
I believe the problem is that you're accessing a mailbox that's never been accessed normally. Can you try this with a mailbox that you know the owner has opened and worked with? Or is that already the case?
Also, as I don't have access to my Exchange machine at the moment, can you give me an idea of what the Identity property contains? I'm absolutely certain using a cmdlet like Get-QADUser vs. Get-User in Exchange, will ultimately bring you more satisfaction. We just need to mesh up the right property from Get-MailboxStatistics to something Get-QADUser can consume, so that it can get you the right user.
It might also be a bit helpful to understand what your end goal is - possibly there's an entirely different approach that will get you to where you want to be.
Related
I have a PS script that reads a .txt file of pcnames, checks remote registry to report back on the installed programs. It's partially working when I export it to a .csv file, however only the pcname field changes to the next pcname from my .txt file. The .csv file is duplicating the same list of installed programs that the first pc in the list has installed. I'm not sure why it is outputting the pcname for each different pc correctly, but the installed program list and version is duplicated for every pc when I view the .csv file. I've tried changing the location of the foreach loops, but haven't had any luck. Thanks for any suggestions. Here's the code:
$pcnames = Get-Content "C:\scripts\pclist.txt"
foreach ($pcname in $pcnames) {
$list=#()
$InstalledSoftwareKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
$InstalledSoftware=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$pcname)
$RegistryKey=$InstalledSoftware.OpenSubKey($InstalledSoftwareKey)
$SubKeys=$RegistryKey.GetSubKeyNames()
Foreach ($key in $SubKeys){
$thisKey=$InstalledSoftwareKey+"\\"+$key
$thisSubKey=$InstalledSoftware.OpenSubKey($thisKey)
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $pcname
$obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName"))
$obj | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))
$list += $obj
}
$list | where { $_.DisplayName } | select ComputerName, DisplayName, DisplayVersion | Export-csv 'c:\scripts\allprograms111722.csv' -append
}
I adjusted the closing } for both foreach loops, thinking that would fix the issue, but it did not.
Your script works for me. To be sure that you are not using the content read from the previous computer if a computer in the list is unreachable, you should clear variables between each loop :
$list=#()
clear-variable InstalledSoftware
clear-variable SubKeys
clear-variable RegistryKey
Good Afternoon,
I searched through this forum and a few others combining ideas and trying different angles but haven't figured this out. If this has been answered, and I suck at searching, I am sorry and please let me know.
End Goal of script: have an excel file with columns for the AD properties Name, Office, Org, and most importantly a seperate column for each group a user is a member of.
The problem I am running into is creating a new column for each/every group that a user has. Not all users have the same amount of groups. Some have 10 some have 30 (yes 30, our AD is a mess).
Here is what I have done so far, and the spot that I am having difficulty with is towards the end:
$scoop = get-content C:\temp\SCOOP.txt ##This is a text file with a list of user id's, to search AD with
$outfile = 'C:\temp\SCOOP_ID.csv'
$ou = "OU=Humans,OU=Coupeville,DC=ISLANDS" #This is the searchbase, helps AD isolate the objects
Clear-Content $outfile #I clear content each time when I am testing
Foreach($ID in $scoop){
##AD Search filters##
$filtertype = 'SamAccountName'
$filter1 = $ID
##End AD Search filters##
##AD Search --MY MAIN ISSUE is getting the MemberOF property properly
$properties = get-aduser -SearchBase $ou -Filter {$filtertype -eq $filter1} -Properties Name,Office,Organization,MemberOf | select Name,Office,Organization,MemberOf
##AD Search ## Turns the MemberOf property to a string, I tried this during my testing not sure if objects or strings are easier to work with
#$properties = get-aduser -SearchBase $ou -Filter {$filtertype -eq $filter1} -Properties Name,Office,Organization,MemberOf | select Name,Office,Organization, #{n='MemberOf'; e= { $_.memberof | Out-String}}
#Attempt to specify each property for the output to csv
$name = $properties.Name
$office = $properties.Office
$org = $properties.Organization
$MemberOf = $properties.MemberOf
$membersArray = #()
foreach($mem in $MemberOf){ $membersArray += $mem }
###This is what I typically use to export to a CSV - I am sure there are other maybe better ways but this one I know.
$Focus = [PSCustomObject]#{}
$Focus | Add-Member -MemberType NoteProperty -Name 'Name' -Value $name -Force
$Focus | Add-Member -MemberType NoteProperty -Name 'Office' -Value $office -Force
$Focus | Add-Member -MemberType NoteProperty -Name 'Org' -Value $org -Force
$Focus | Add-Member -MemberType NoteProperty -Name 'Groups' -Value $MemberOf -Force
<########################
#
# Main ISSUE is getting the $memberOf variable, which contains all of the users groups, into seperate columns in the csv. To make it more plain, each column would be labeled 'Groups', then 'Groups1', and so on.
I have tried a few things but not sure if any were done properly or what I am messing up. I tried using $memberof.item($i) with a FOR loop but couldnt figure out how to send each
item out into its own Add-Member property.
#
#######################>
##Final Output of the $Focus Object
$Focus | Export-Csv $outfile -Append -NoTypeInformation
}
I came up with this solution, simply loop $MemberOf and add a unique name.
$MemberOf = #('a','b','c','d','e','f') #example
$Focus = [PSCustomObject]#{}
$Focus | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'test' -Force
$i=0; $MemberOf | % {
$i++
$Focus | Add-Member -MemberType NoteProperty -Name "Group $i" -Value $_ -Force
}
$Focus | Export-Csv xmgtest1.csv -Append -Force -NoTypeInformation
However this doesn't work in your case, since you run this code once per user and there's no "awareness" of the entire set. So you would (in your existing architecture) need to ensure that the largest number of groups is added first.
You can fix this by creating the CSV all at once (only other option would be to constantly load/unload the csv file).
First, add $csv = #() to the top of your file.
Then, after you finish creating $Focus, add it to $csv.
Finally, you may remove -Append.
The slimmed down version looks like this:
$csv = #()
#Foreach($ID in $scoop){ etc..
$MemberOf = #('a','b','c','d','e','f') #example
$Focus = [PSCustomObject]#{}
$Focus | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'test' -Force
#$Focus | Add-Member etc ...
$i=0; $MemberOf | % {
$i++
$Focus | Add-Member -MemberType NoteProperty -Name "Group $i" -Value $_ -Force
}
$csv += $Focus
# } end of Foreach($ID in $scoop)
$csv | Export-Csv xmgtest1.csv -NoTypeInformation
Hope this helps.
I am trying to create a backup powershell script for user documents. I have created a script where I am putting on a form box the username I want to backup and then the script proceeds. The last thing is that I want to have a rule that if I put a wrong name, the script will not proceed.
Does anyone knows how I can "read" the present useraccounts on a laptop, in order to create rule to cross-check the input with the useraccounts?
Best regards.
This will read all of the folders in C:\Users and attempt to find a corresponding account in AD:
Get-Childitem C:\users -Directory |
ForEach-Object {
$profilePath = $_.FullName
Get-AdUser -Filter {SamAccountName -eq $_.Name} |
ForEach-Object {
New-Object -TypeName PsCustomObject |
Add-Member -MemberType NoteProperty -Name Name -Value $_.Name -PassThru |
Add-Member -MemberType NoteProperty -Name ProfilePath -Value $profilePath -PassThru |
Add-Member -MemberType NoteProperty -Name SID -Value $_.SID -PassThru
}
} | Format-Table Name,SID, profilePath -AutoSize
Obviously you can modify to get different AD properties, if needed. Also, remove the Format-Table command in order to pipe the output objects for further manipulation.
Note:
This requires the AD PowerShell Module to be installed on the system you run the script on.
It only outputs an object if it finds a user account, so you won't see anything for folders it finds that have no account.
Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | Select Name
will give you names of local accounts. Other available fields for select are listed here.
I am fairly new to scripting in Powershell and have been faced with this task. Display a table that identifies the servers in a particular OU and ensure that they are a member of the correct group. There should also be nothing "hard coded" into the script (i.e. server names or group names). Essentially, the script should evaluate the server's name and determine which group it should be a member. If it is in the right group, print it to a table showing it is correct but if is not, also print it to the table indicating it is in the wrong group.
I began by utilizing the Quest ActiveRoles plugin and determining I would need to use nested loops. I would first loop through the security groups and evaluate each server against that group to see if it was a member. I then add the computer and the group to a custom PSObject and am able to print that out as a Correct answer. That part works fine. I run into problems when I try to also print out the incorrect answers. Because of the way I have the loops structured, the majority of the servers will not be in the selected group so they will all come back as incorrect.
My question is, what would be the best way to loop through this information and compare the server name to the group membership and still be able to show which are in the correct group and which are in the incorrect group. My code is below.
#Load Quest Active Roles Snapin
Add-PSSnapin quest.activeroles.admanagement
#Load Active Directory module
Import-Module ActiveDirectory
#Variables
$AccelGroup = Get-QADGroup -SearchRoot 'lumosnet.com/Centrify/Role Groups' -GroupType 'Security' | where {$_.name -like "* Servers"}
$AccelComputer = Get-QADComputer -SearchRoot 'lumosnet.com/Centrify/servers'
$report = #()
$AccelGroup | foreach{$ADN=$_.DN
$AccelComputer | foreach{$AMember=$_.memberof
$AComp=$_.name
If($AMember -like $ADN)
{
$ReportObj = New-Object PSObject
$ReportObj | Add-Member -MemberType NoteProperty -Name Computer_Name -Value $AComp
$ReportObj | Add-Member -MemberType NoteProperty -Name Group_Name -Value $($AMember -replace '^CN=([^,]+),OU=.+$','$1')
$ReportObj | Add-Member -MemberType NoteProperty -Name Correct? -Value "Correct"
$report += $ReportObj
}
Else
{
}
}
}
write-host ($report | FT -Autosize | Out-String)
Instead of trying to do the join yourself, consider using LDAP queries to retrieve the servers in the group, and then another to retrieve those not in the group. It will probably still end up simpler:
$ouPath = "LDAP://OU=Database,OU=Domain Servers,DC=EXAMPLE,DC=LOCAL"
$groupPath = "CN=ITT Testing Servers,OU=Domain Servers,DC=EXAMPLE,DC=LOCAL"
$correctFilter = ("(&(objectClass=computer)(memberof={0}))" -f $groupPath)
$incorrectFilter = ("(&(objectClass=computer)(!(memberof={0})))" -f $groupPath)
# set up the searcher
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $ouPath
$objSearcher.PageSize = 1000
$objSearcher.SearchScope = "Subtree"
#("name","operatingSystem","lastLogonTimestamp") | %{ $unused = $objSearcher.PropertiesToLoad.Add($_) }
# retrieve the correct items
$objSearcher.Filter = $correctFilter
Write-Host "Servers correctly in group"
$goodItems = $objSearcher.FindAll()
$goodItems | %{ New-Object PSObject -Property $_.Properties } | ft name,operatingsystem,lastLogonTimestamp
# retrieve the bad items
$objSearcher.Filter = $incorrectFilter
Write-Host "Servers in wrong group"
$badItems = $objSearcher.FindAll()
$badItems | %{ New-Object PSObject -Property $_.Properties } | ft name,operatingsystem,lastLogonTimestamp
You can, of course, loop over each result to get them to a single list as you are doing currently.
I hit a common problem with my scripting lately and decided to throw it into the wild to see how other people deal with this problem.
tl;dr; I want to export objects which have a varying number of properties. eg; object 1 may have 3 IP address but object 2 has 7 IP addresses.
I've evolved to creating a new object with custom properties and then injecting these objects into an array as my method of catching results - happy to hear if there is a better way but this is how I roll. This method works 100% when outputting to the screen as the objects are shown in list format - I've tried a number of export/out-file methods to no avail when I want to capture and store the output for reading in something like Excell.
The following is an example of me building an object and storing it (the function of the code is not important here - just the results it generates):
add-pssnapin Quest.ActiveRoles.ADManagement
$Groups = get-qadgroup AmbigousGroupNameHere-
$UserInfo = #()
ForEach ( $Group in $Groups ) {
$CurrentGroupMembers = get-qadgroupmember $Group
write-host "Processing group:" $Group
ForEach ( $GroupMember in $CurrentGroupMembers ) {
If ( $GroupMember.type -eq "User" ) {
$counter = 1
write-host "Processing member:" $GroupMember
$UserObject = get-qaduser $GroupMember | select SamAccountName,FirstName,LastName
$objUserInfo = New-Object System.Object
$objUserInfo | Add-Member -MemberType NoteProperty -Name SamAccountName -Value $UserObject.SamAccountName
$objUserInfo | Add-Member -MemberType NoteProperty -Name FirstName -Value $UserObject.FirstName
$objUserInfo | Add-Member -MemberType NoteProperty -Name LastName -Value $UserObject.LastName
$GroupMembership = get-qadgroup -ContainsMember $GroupMember | where name -like "AmbigousGroupNameHere-*"
ForEach ( $GroupName in $GroupMembership ) {
$objUserInfo | Add-Member -MemberType NoteProperty -Name CtxGroup$counter -Value $GroupName.SamAccountName
$counter++
}
$UserInfo += $objUserInfo
} else {
write-host "This is a group - we are ignoring it."
}
}
}
$UserInfo | Export-Csv UsersOutput.csv -NoType
From the above - you can see I scale the object property name by 1 for each group. CtxGroup$counter allows me to scale an object for the correct number of groups each user has. Confirmed this works great when outputting to the screen by default. The object is listed and I can see a new property for each group that matches for that user.
Now for the problem. When I export-csv or out-file the file is generated with enough headers based off the first object - so it creates the headings based on the amount of properties the first object has. So lets say the first user has 3 matching groups, it will create heading CtxGroup1, CtxGroup2, CtxGroup3. Great! No.
If the next user has 5 matching groups - only the first three are included in the output and the additional 2 are discarded as we don't have headings for CtxGroup4, CtxGroup5.
How on earth do other people deal with this?
side note; I considered creating my first object as a dummy with a massive amount of object (and hence headings) but well - that is not cool and really makes me feel inefficient.
You can obtain what you want ordering $UserInfo array by the number of properties, it can be done, but it's not so simple, in your case I would add another propertie with the count of groups added:
...
ForEach ( $GroupName in $GroupMembership ) {
$objUserInfo | Add-Member -MemberType NoteProperty -Name CtxGroup$counter -Value $GroupName.SamAccountName
$counter++
}
$objUserInfo | Add-Member -MemberType NoteProperty -Name NbCtxGroup -Value ($counter - 1)
$UserInfo += $objUserInfo
...
And then order descending the array on this propertie :
$UserInfo | Sort-Object -Property NbCtxGroup -Descending | Export-Csv .\F.csv
It's not so nice but it will do the trick.
Have a look at :
$objUserInfo.psobject.Properties | Measure-Object