powershell exchange 2003 : query ActiveDirectory for activesync, office, and description? - powershell

I'm using powershell to pull a list of exchange 2003 users and based on the user's mailbox "LegacyDN", I was hoping to use an LDAP query to pull their activesyncenabled property (msExchOmaAdminWirelessEnable), description, and office; however, it doesn't seem to be coming up for me. any ideas?
here is how i'm querying for the AD attributes:
$allusers = get-wmiobject -class exchange_mailbox -namespace root\microsoftexchangev2 -computername srv02 | select mailboxdisplayname, legacyDN
foreach ($user in $allusers)
{
$obj = new-object psobject
$exchangever = "2003"
$tmp = [adsi]("LDAP://" + $user.legacydn)
$obj | add-member -membertype noteproperty -name "Display Name" -value $user.mailboxdisplayname
$obj | add-member -membertype noteproperty -name "Office" -value $tmp.physicaldeliveryofficename
$obj | add-member -membertype noteproperty -name "Description" -value $tmp.description
$obj | add-member -membertype noteproperty -name "Activesync" -value $tmp.msExchOmaAdminWirelessEnable
$allusersemail += $obj
}
$alluseremail | export-csv \\srv02\logs\test.csv

Here's one way:
$search = System.DirectoryServices.DirectorySearcher
$search.filter = "(&(homeMDB=*)(objectcategory=user)(objectclass=user))"
$props = "mailNickName","displayName","physicaldeliveryofficename","description","msExchOmaAdminWirelessEnable"
$props | foreach { [void]$search.PropertiesToLoad.Add($_) }
$users = $search.findAll()
foreach($u in $users)
{
$DisplayName = #{name="DisplayName";expression={$u.properties.item("DisplayName")}}
$Office = #{name="Office";expression={$u.properties.item("physicaldeliveryofficename")}}
$Description = #{name="Description";expression={$u.properties.item("Description")}}
$Activesync = #{name="Activesync";expression={$u.properties.item("msExchOmaAdminWirelessEnable")}}
$u | select $DisplayName,$Office,$Description,$Activesync
}

Related

How to find email adress of AzureAD user with ps1 script

here is the script i use to export the data from AzureAD to csv.
Connect-AzureAD
Connect-MsolService
$date = Get-Date -UFormat "%d%m%Y"
$dateInfo = Get-Date
$Results = #()
$Roles = Get-AzureADDirectoryRole
foreach ($Role in $Roles) {
$MemberRole = Get-AzureADDirectoryRoleMember -ObjectId $Role.ObjectId
foreach ($Membre in $MemberRole ) {
$InfosUsers = Get-MsolUser -ObjectId $Membre.ObjectId
$List = New-Object System.Object
$List | Add-Member -type NoteProperty -name Compte -Value $Membre.DisplayName
$List | Add-Member -type NoteProperty -name Active -Value $Membre.AccountEnabled
$List | Add-Member -type NoteProperty -name Role -Value $Role.DisplayName
$List | Add-Member -type NoteProperty -name NumeroTel -Value $InfosUsers.StrongAuthenticationUserDetails.PhoneNumber
$Results += $List
}
}
$Results | Export-Csv path/to/the/file/ -NoTypeInformation
Disconnect-AzureAD
The things is that i would like to get mail adresse from user and i dont know how to do ?
Happy to see you already got your answer, here is one alternative way you can approach to get the email address of Azure AD user.
I did Few changes in your code (ie I have removed Connect-MsolService and replace Get-MsolUser with Get-AzADUser.) and I am able to perform the operation you are trying to do .
Connect-AzureAD
$date = Get-Date -UFormat "%d%m%Y"
$dateInfo = Get-Date
$Results = #()
$Roles = Get-AzureADDirectoryRole
foreach ($Role in $Roles) {
$MemberRole = Get-AzureADDirectoryRoleMember -ObjectId $Role.ObjectId
foreach ($Membre in $MemberRole ) {
$InfosUsers = Get-AzADUser -ObjectId $Membre.ObjectId
$List = New-Object System.Object
$List | Add-Member -type NoteProperty -name Compte -Value $Membre.DisplayName
$List | Add-Member -type NoteProperty -name Active -Value $Membre.AccountEnabled
$List | Add-Member -type NoteProperty -name Role -Value $Role.DisplayName
$List | Add-Member -type NoteProperty -name NumeroTel -Value $InfosUsers.StrongAuthenticationUserDetails.PhoneNumber
$List | Add-Member -type NoteProperty -name Email -Value $Membre.Mail
$List | Add-Member -type NoteProperty -name UPName -Value $Membre.UserPrincipalName
$Results += $List
}
}
$Results | Export-Csv /RahulS -NoTypeInformation
Output---

"Unable to obtain permission for "name of the share" | win2008r2 File Server

So a friend and i wrote a script so we could get some info about the share on our file server but i keep getting an error about the shares, I am a Domain Admin so i dont think the problem is with my permissions.
Here is the script:
$MyPath = "C:\File Share Info\Shares"
$shares = gwmi -Class win32_share -ComputerName $computer #| select -ExpandProperty Name
$AllObjects = #()
foreach ($share in $shares) {
$acl = $null
$ShareName = $share.Name
$ShareCaption = $Share.Caption
if ($ShareName -notlike "*$*"){
$objShareSec = Get-WMIObject -Class Win32_LogicalShareSecuritySetting -Filter "name='$ShareName'"
#-ComputerName $computer
try {
$SD = $objShareSec.GetSecurityDescriptor().Descriptor
foreach($ace in $SD.DACL){
$UserName = $ace.Trustee.Name
If ($ace.Trustee.Domain -ne $Null) {$UserName = "$($ace.Trustee.Domain)\$UserName"}
If ($ace.Trustee.Name -eq $Null) {$UserName = $ace.Trustee.SIDString }
[Array]$ACL = New-Object Security.AccessControl.FileSystemAccessRule($UserName, $ace.AccessMask, $ace.AceType)
$Identity = $ACL[0].IdentityReference.Value
$myObject = New-Object System.Object
$myObject | Add-Member -type NoteProperty -name ShareAccessType -Value $ACL[0].AccessControlType
$myObject | Add-Member -type NoteProperty -name Identity -Value $Identity
$myObject | Add-Member -type NoteProperty -name ShareRights -Value $ACL[0].FileSystemRights
$myObject | Add-Member -type NoteProperty -name SharePath -Value $Share.Path
$myObject | Add-Member -type NoteProperty -name ShareName -Value $Share.Name
$myObject | select ShareName, Identity, ShareRights, SharePath, ShareAccessType | Export-Csv "$MyPath\permissions.csv" -Append -Encoding Unicode #-NoTypeInformation -Delimiter "`t"
}
}
catch
{ Write-Host "Unable to obtain permissions for `"$ShareName`""; $share.Name | Out-File "$MyPath\Failed.txt" -Append}
}
}

Issue with foreach loop (Combining Commands)

The script below works out great for identifying licensing for each individual host across multiple vCenters. What I am trying to include is the tag for each host as well. When I run the command individually it works fine, however when I run it as part of the code it is not functioning correctly. I highlighted the section if anyone can please take a look thanks. The line of code with the issue is commented out within the script below.
I attempted pushing this into a variable outside and insideof the foreach loop but I am receiving either 0 output, or the same output across each object.
Below is the actual command I put inside the foreach loop which is not functional.
(Get-VMhost | where{$_.Category -like "*Host*"})
$sw = [Diagnostics.Stopwatch]::StartNew()
# Declare our list of vCenters
[array]$vclistall = "vcenter01"
# Ensure were not connected to any vcenters
if ($DefaultVIServer.Count -gt 0) {
Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Force:$true > $null
}
[array]$report = $null
foreach ($ScriptVCInstance in $vclistall) {
$connection = Connect-VIServer $ScriptVCInstance -ErrorAction SilentlyContinue
if ($connection) {
Write-Host "Collecting License Assets on vCenter $($ScriptVCInstance)"
# Get the license manager assets
$LicenseManager = Get-view LicenseManager
$LicenseAssignmentManager = Get-View $LicenseManager.LicenseAssignmentManager
$licenses = $LicenseAssignmentManager.GetType().GetMethod("QueryAssignedLicenses").Invoke($LicenseAssignmentManager, #($null))
#Format the asset into an object
foreach ($license in $Licenses) {
$object = New-Object -TypeName PSObject
$object | Add-Member -MemberType NoteProperty -Name "vCenter" -Value $($connection.name)
$object | Add-Member -MemberType NoteProperty -Name "Entity" -Value $($license.EntityDisplayName)
$object | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $($license.Properties | where{$_.Key -eq 'ProductName'} | select -ExpandProperty Value)
$object | Add-Member -MemberType NoteProperty -Name "Product Version" -Calue $($License.Properties | where{$_.Key -eq 'FileVersion'} | select -ExpandProperty Value)
$object | Add-Member -MemberType NoteProperty -Name "License" -Value $($license.AssignedLicense.LicenseKey)
$object | Add-Member -MemberType NoteProperty -Name "License Name" -Value $($license.AssignedLicense.Name)
$object | Add-Member -MemberType NoteProperty -Name "Cost Unit" -Value $($license.Properties | where{$_.Key -eq 'CostUnit'} | select -ExpandProperty Value)
$object | Add-Member -MemberType NoteProperty -Name "Used License" -Value $($license.Properties | where{$_.Key -eq 'EntityCost'} | select -ExpandProperty Value)
$object | Add-Member -MemberType NoteProperty -Name "Total Licenses" -Value $($license.AssignedLicense.Total)
# Issue--> $object | Add-Member -MemberType NoteProperty -Name "Tag" -Value $(Get-VMhost | where{$_.Category -like "*Host*"})
$report += $object
if ($DefaultVIServer.Count -gt 0) {
Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Force:$true > $null
}
} #end foreach $license
} else { # Else for if $connection
Write-warning "Not connected to vCenter $($ScriptVCInstance)"
} # endif $connection
} # End foreach $ScriptVCInstance
# write-out as a CSV file
Write-host "Exporting CSV $($env:USERPROFILE)\Licensed-Assets.csv"
$report | Sort-object "vCenter","License","Entity" | Export-csv "$($env:USERPROFILE)\Licensed-Assets.csv" -NoTypeInformation -UseCulture
$sw.Stop()
$sw.Elapsed

How can i get the disk details in csv format in windows server using powershell

Hi all I want to get details in below format
Hostname Drive_0 Drive_1 Drive_2
Name C: 99.899227142334 d: 99.899227142334 e: 99.899227142334
I can get this detail using below script but this works on PowerShell 3.0
how can I change to execute on PowerShell 2.0
$result = #()
$obj = new-object PSobject
$server = hostname
$obj |Add-Member -MemberType NoteProperty -Name "Hostname" -Value $server -ErrorAction SilentlyContinue
$z = Get-WmiObject -Class win32_Logicaldisk -Filter 'DriveType=3' | Select-Object -Property DeviceID, #{LABEL='TotalSize';EXPRESSION={$_.Size/1GB}}
$z3 = $z.DeviceID
$z4 = $z.TotalSize
$i = 0
foreach($z3 in $z3){
$z1 = $z.DeviceID[$i]
$z2 = $z.TotalSize[$i]
$zx = "$z1" + ": $z2"
$obj | Add-Member -MemberType NoteProperty -Name "Drive_$i" -Value $zx -ErrorAction SilentlyContinue
$i++
}
$result+=$obj
$result | Export-Csv "$env:userprofile\Desktop\Result.csv" -NoTypeInformation
You can change your code to the below. It's a bit neater and sorts out your loops and limits so you don't need to manage them
$result = #()
$obj = new-object PSobject
$server = hostname
$obj |Add-Member -MemberType NoteProperty -Name "Hostname" -Value $server -ErrorAction SilentlyContinue
$z = Get-WmiObject -Class win32_Logicaldisk -Filter 'DriveType=3' | Select-Object -Property DeviceID, #{LABEL='TotalSize';EXPRESSION={$_.Size/1GB}}
$i = 0
$z | % {
$z1 = $_.DeviceID
$z2 = $_.TotalSize
$zx = "$z1" + ": $z2"
$obj | Add-Member -MemberType NoteProperty -Name "Drive_$i" -Value $zx
$i++
}
$result+=$obj
$result | Export-Csv "$env:userprofile\Desktop\Result.csv" -NoTypeInformation
I also removed the -ErrorAction off the Add-Member as you should try and handle anything that crops up yourself, but add it back if you feel the need to.

PowerShell to fetch installed programs

I will be hosting a file on a remote server (read-only) and asking people to run the file on their machines to gather installed program information. I want the file to be saved to their Desktop in their user space, so that I can then have them send it to us.
I have the script, but I'm not managing to obtain information from both "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", and "Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" in the same output file. I'm obviously missing something inherently obvious, as PowerShell is clearly able to do this, and I'm asking that someone please save me from my PEBKAC issue!
Thank you in advance, appreciated!
Here is my code;
$computers = "$env:computername"
$array = #()
foreach($pc in $computers){
$computername=$pc
$UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
$UninstallKey="Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername)
$regkey=$reg.OpenSubKey($UninstallKey)
$subkeys=$regkey.GetSubKeyNames()
Write-Host "$computername"
foreach($key in $subkeys){
$thisKey=$UninstallKey+"\\"+$key
$thisSubKey=$reg.OpenSubKey($thisKey)
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computername
$obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName"))
$obj | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))
$obj | Add-Member -MemberType NoteProperty -Name "InstallLocation" -Value $($thisSubKey.GetValue("InstallLocation"))
$obj | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))
$array += $obj
}
}
$array | Where-Object { $_.DisplayName } | select ComputerName, DisplayName, DisplayVersion, Publisher | export-csv C:\Users\$env:username\Desktop\Installed_Apps.csv
Right now the following two lines set the same variable:
$UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
$UninstallKey="Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
Use this:
$UninstallKey = #(
'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
)
Then wrap the real logic in:
$UninstallKey | ForEach-Object {
$regkey=$reg.OpenSubKey($_)
# the rest of your logic here
}