asked on
$DataA = Import-CSV D:\Import\DataA.csv
$DataB = Import-CSV D:\Import\DataB.csv
$properties = $DataA[0].PSObject.Properties.Name + $DataB[0].PSObject.Properties.Name
$IndexedProperty = 'distinguishedName'
$DataBIndex = @{}
for ($i = 0; $i -lt $DataB.Count; $i++) {
$DataBIndex.Add($DataB[$i].$IndexedProperty, $i)
}
foreach ($itemA in $DataA) {
if ($DataBIndex.Contains($itemA.$IndexedProperty)) {
$itemB = $DataB[$DataBIndex.($itemA.$IndexedProperty)]
foreach ($property in $itemB.PSObject.Properties) {
$itemA | Add-Member $property.Name $property.Value
}
$itemA | Select-Object $properties | Export-CSV D:\Export\Result.csv -Append -NoTypeInformation
}
}
ERROR: System.Management.Automation.MethodInvocationException: Exception calling "Add" with "2" argument(s): "Key cannot be null.
Parameter name: key" ---> System.ArgumentNullException: Key cannot be null.
Parameter name: key
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
at CallSite.Target(Closure , CallSite , Object , Object , Object )
--- End of inner exception stack trace ---
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
ASKER
ASKER
$DataA = Get-ADUser -SearchBase "OU=Users,DC=Contoso,DC=com" -SearchScope 1 -Properties samaccountname,LastLogon,whenCreated,distinguishedname
-Filter {LastLogon -lt $time -AND enabled -eq $true -AND whenCreated -lt $time} |
select-object SamAccountName,Name,@{Name="LastLogon"; Expression={[DateTime]::FromFileTime($_.lastLogon).ToString('yyyy-MM-dd hh:mm:ss')}},
WhenCreated,@{Name="ReferenceDate"; Expression={($datetoday)}},
@{N='ADAccountDaysInactive'; E={$($(Get-Date) - $([DateTime]::FromFileTime($_.lastLogon))).Days}},distinguishedname |
Export-CSV -Path D:\Export\DataA.csv -NoTypeInformation -Append -Encoding UTF8
$mbxall = Get-Mailbox -ResultSize Unlimited -OrganizationalUnit $dnou | Where {$_.OrganizationalUnit -eq "Contoso.com/Users"} |
Select-Object alias,displayname,distinguishedname
$mbxinactive = ForEach($mbx in $mbxall)
{
$DataB = Get-MailboxStatistics $mbx.alias | where {$_.LastLogonTime -lt $time -AND $_.WhenMailboxCreated -lt $time} |
Select-Object displayname,lastlogontime,lastloggedonuseraccount,@{Name="ReferenceDate"; Expression={($datetoday)}},
@{N='MailboxDaysInactive'; E={ ((Get-Date) - ($_.LastLogonTime)).Days} },@{N='DistinguishedName'; E={($mbx.distinguishedname)} } |
Export-CSV -Path D:\Export\DataB.csv -NoTypeInformation -Append
}
$properties = $DataA[0].PSObject.Properties.Name + $DataB[0].PSObject.Properties.Name
$IndexedProperty = 'distinguishedName'
$DataBIndex = @{}
for ($i = 0; $i -lt $DataB.Count; $i++) {
$DataBIndex.Add($DataB[$i].$IndexedProperty, $i)
}
foreach ($itemA in $DataA) {
if ($DataBIndex.Contains($itemA.$IndexedProperty)) {
$itemB = $DataB[$DataBIndex.($itemA.$IndexedProperty)]
foreach ($property in $itemB.PSObject.Properties) {
$itemA | Add-Member $property.Name $property.Value
}
$itemA | Select-Object $properties | Export-CSV D:\Export\Result.csv -Append -NoTypeInformation
}
}
ASKER
ASKER
ERROR: System.Management.Automation.PSInvalidOperationException: No valid sessions were specified. Ensure you provide valid sessions that are in the Opened state and are available to run commands.
at Microsoft.PowerShell.Commands.InvokeCommandCommand.BeginProcessing()
at System.Management.Automation.Cmdlet.DoBeginProcessing()
at System.Management.Automation.CommandProcessorBase.DoBegin()
Import-Module ActiveDirectory
$exuri = ‘http://exchangeserver.contoso.com/PowerShell/?SerializationLevel=Full’
$ExSession = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri $exuri -Credential $Credentials –Authentication Kerberos
Import-PSSession $ExSession
$DaysInactive = 60
$time = (Get-Date).Adddays(-($DaysInactive))
$datetoday = (Get-Date).ToString('yyyy-MM-dd hh:mm:ss')
$dnou = "OU=Users,DC=Contoso,DC=com"
$ErrorActionPreference = 'SilentlyContinue'
$dataAParams = @{
SearchBase = "OU=User,DC=Contoso,DC=com"
SearchScope = 'OneLevel'
Properties = 'samaccountname', 'LastLogon', 'whenCreated', 'distinguishedname'
Filter = { LastLogon -lt $time -AND enabled -eq $true -AND whenCreated -lt $time }
}
$DataA = Get-ADUser @dataAParams |
Select-Object SamAccountName,
Name,
@{Name="LastLogon"; Expression={ [DateTime]::FromFileTime($_.lastLogon).ToString('yyyy-MM-dd hh:mm:ss') }},
WhenCreated,
@{Name="ReferenceDate"; Expression={ $datetoday }},
@{Name='ADAccountDaysInactive'; Expression={ ((Get-Date) - ([DateTime]::FromFileTime($_.lastLogon))).Days }},
distinguishedname
$DataB = Get-Mailbox -ResultSize Unlimited -OrganizationalUnit $dnou |
Where { $_.OrganizationalUnit -eq "Contoso.com/Users" } |
ForEach-Object {
$mailbox = $_
$_ | Get-MailboxStatistics | Where {$_.LastLogonTime -lt $time -AND $_.WhenMailboxCreated -lt $time} |
Select-Object DisplayName,
LastLogonTime,
LastLoggedOnUserAccount,
@{Name="ReferenceDate"; Expression={ $datetoday }},
@{Name='MailboxDaysInactive'; Expression={ ((Get-Date) - ($_.LastLogonTime)).Days }},
@{Name='DistinguishedName'; Expression={ $mailbox.distinguishedname }}
}
$properties = $DataA[0].PSObject.Properties.Name + $DataB[0].PSObject.Properties.Name
$IndexedProperty = 'distinguishedName'
$DataBIndex = @{}
for ($i = 0; $i -lt $DataB.Count; $i++) {
$DataBIndex.Add($DataB[$i].$IndexedProperty, $i)
}
foreach ($itemA in $DataA) {
if ($DataBIndex.Contains($itemA.$IndexedProperty)) {
$itemB = $DataB[$DataBIndex.($itemA.$IndexedProperty)]
foreach ($property in $itemB.PSObject.Properties) {
$itemA | Add-Member $property.Name $property.Value
}
$itemA | Select-Object $properties | Export-CSV D:\Export\Result.csv -Append -NoTypeInformation
}
}
Remove-PSSession $ExSession
ASKER
ERROR: System.Management.Automation.RuntimeException: Cannot index into a null array.
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
ASKER
$mailboxes = Get-Mailbox -ResultSize Unlimited -OrganizationalUnit $dnou |
Where { $_.OrganizationalUnit -eq "Contoso.com/Users" }
foreach ($mailbox in $mailboxes) {
Get-MailboxStatistics -Identity $mailbox.Identity | Where {$_.LastLogonTime -lt $time -AND $_.WhenMailboxCreated -lt $time} |
Select-Object DisplayName,
LastLogonTime,
LastLoggedOnUserAccount,
@{Name="ReferenceDate"; Expression={ $datetoday }},
@{Name='MailboxDaysInactive'; Expression={ ((Get-Date) - ($_.LastLogonTime)).Days }},
@{Name='DistinguishedName'; Expression={ $mailbox.distinguishedname }}
}
ASKER
ASKER
$DaysInactive = 60
$time = (Get-Date).Adddays(-($DaysInactive))
$datetoday = (Get-Date).ToString('yyyy-MM-dd hh:mm:ss')
$dnou = "OU"
$ErrorActionPreference = 'SilentlyContinue'
$dataAParams = @{
SearchBase = "OU"
SearchScope = 'OneLevel'
Properties = 'samaccountname', 'LastLogon', 'whenCreated', 'distinguishedname'
Filter = { LastLogon -lt $time -AND enabled -eq $true -AND whenCreated -lt $time }
}
$DataA = Get-ADUser @dataAParams |
Select-Object SamAccountName,
Name,
@{Name="LastLogon"; Expression={ [DateTime]::FromFileTime($_.lastLogon).ToString('yyyy-MM-dd hh:mm:ss') }},
WhenCreated,
@{Name="ReferenceDate"; Expression={ $datetoday }},
@{Name='ADAccountDaysInactive'; Expression={ ((Get-Date) - ([DateTime]::FromFileTime($_.lastLogon))).Days }},
distinguishedname
$mailboxes = Get-Mailbox -ResultSize Unlimited -OrganizationalUnit $dnou |
Where { $_.OrganizationalUnit -eq "OU" }
$DataB = New-Object System.Collections.Generic.List[PSObject]
foreach ($mailbox in $mailboxes) {
$result = Get-MailboxStatistics -Identity $mailbox.Identity | Where {$_.LastLogonTime -lt $time -AND $_.WhenMailboxCreated -lt $time} |
Select-Object DisplayName,
LastLogonTime,
LastLoggedOnUserAccount,
@{Name="ReferenceDate"; Expression={ $datetoday }},
@{Name='MailboxDaysInactive'; Expression={ ((Get-Date) - ($_.LastLogonTime)).Days }},
@{Name='DistinguishedName'; Expression={ $mailbox.distinguishedname }}
$DataB.Add($result)
}
$DataB = $DataB.ToArray()
$properties = $DataA[0].PSObject.Properties.Name + $DataB[0].PSObject.Properties.Name
$IndexedProperty = 'distinguishedName'
$DataBIndex = @{}
for ($i = 0; $i -lt $DataB.Count; $i++) {
$DataBIndex.Add($DataB[$i].$IndexedProperty, $i)
}
foreach ($itemA in $DataA) {
if ($DataBIndex.Contains($itemA.$IndexedProperty)) {
$itemB = $DataB[$DataBIndex.($itemA.$IndexedProperty)]
foreach ($property in $itemB.PSObject.Properties) {
$itemA | Add-Member $property.Name $property.Value
}
$itemA | Select-Object $properties | Export-CSV D:\Export\Result.csv -Append -NoTypeInformation
}
}
ASKER
ASKER
$DaysInactive = 60
$Time = (Get-Date).Adddays(-($DaysInactive))
$CurrentTimeStamp = (Get-Date).ToString('yyyy-MM-dd hh:mm:ss')
$OrganizationalUnit = "OU=Users,DC=nl,DC=Contoso,DC=com"
$OrganizationalUnit2 = "Contoso.com/Users"
$DomainControllers = Get-ADDomainController -Filter {Name -like "*"}
$ErrorActionPreference = 'SilentlyContinue'
$dataAParams = @{
SearchBase = "OU=Users,DC=Contoso,DC=com"
SearchScope = 'OneLevel'
Properties = 'samaccountname', 'LastLogon', 'whenCreated', 'distinguishedname'
Filter = { LastLogon -lt $Time -AND enabled -eq $true -AND whenCreated -lt $Time }
Server = $DomainControllerHostname
}
foreach($DomainController in $DomainControllers)
{
$DomainControllerHostname = $DomainController.HostName
$DataA = Get-ADUser @dataAParams |
Select-Object SamAccountName,
Name,
@{Name="LastLogon"; Expression={ [DateTime]::FromFileTime($_.lastLogon).ToString('yyyy-MM-dd hh:mm:ss') }},
WhenCreated,
@{Name="ReferenceDate"; Expression={ $CurrentTimeStamp }},
@{Name='ADAccountDaysInactive'; Expression={ ((Get-Date) - ([DateTime]::FromFileTime($_.lastLogon))).Days }},
distinguishedname
}
$AllMailboxes = Get-Mailbox -ResultSize Unlimited -OrganizationalUnit $OrganizationalUnit |
Where {$_.OrganizationalUnit -eq $OrganizationalUnit2} |
Select-Object alias,displayname,distinguishedname
$DataB = @()
$InactiveMailbox = ForEach($Mailbox in $AllMailboxes) {
$DataB += Get-MailboxStatistics $Mailbox.alias | Where {$_.LastLogonTime -lt $Time -AND $_.WhenMailboxCreated -lt $Time} |
Select-Object displayname,
lastlogontime,
lastloggedonuseraccount,
@{Name="ReferenceDate"; Expression={($CurrentTimeStamp)}},
@{Name='MailboxDaysInactive'; Expression={ ((Get-Date) - ($_.LastLogonTime)).Days} },
@{Name='DistinguishedName'; Expression={($Mailbox.distinguishedname)} }
}
$properties = $DataA[0].PSObject.Properties.Name + $DataB[0].PSObject.Properties.Name
$IndexedProperty = 'distinguishedName'
$DataBIndex = @{}
for ($i = 0; $i -lt $DataB.Count; $i++) {
$DataBIndex.Add($DataB[$i].$IndexedProperty, $i)
}
foreach ($itemA in $DataA) {
if ($DataBIndex.Contains($itemA.$IndexedProperty)) {
$itemB = $DataB[$DataBIndex.($itemA.$IndexedProperty)]
foreach ($property in $itemB.PSObject.Properties) {
$itemA | Add-Member $property.Name $property.Value
}
$itemA | Select-Object $properties | Export-CSV -Path D:\Export\Result.csv -Append -NoTypeInformation
}
}
ASKER
$DaysInactive = 60
$Time = (Get-Date).Adddays(-($DaysInactive))
$CurrentTimeStamp = (Get-Date).ToString('yyyy-MM-dd hh:mm:ss')
$OrganizationalUnit = "OU=Users,DC=Contoso,DC=com"
$OrganizationalUnit_Format = "Contoso.com/Users"
$DomainControllers = Get-ADDomainController -Filter {Name -like "*"}
$ErrorActionPreference = 'SilentlyContinue'
$DataAValues = @{}
foreach($DomainController in $DomainControllers) {
$DomainControllerHostname = $DomainController.HostName
$dataAParams = @{
SearchBase = "OU=Users,DC=Contoso,DC=com"
SearchScope = 'OneLevel'
Properties = 'samaccountname', 'LastLogon', 'whenCreated', 'distinguishedname'
Filter = { LastLogon -lt $Time -AND enabled -eq $true -AND whenCreated -lt $Time }
Server = $DomainControllerHostname
}
Get-ADUser @dataAParams |
Select-Object SamAccountName,
Name,
@{Name="LastLogon"; Expression={ [DateTime]::FromFileTime($_.lastLogon).ToString('yyyy-MM-dd hh:mm:ss') }},
WhenCreated,
@{Name="ReferenceDate"; Expression={ $CurrentTimeStamp }},
@{Name='ADAccountDaysInactive'; Expression={ ((Get-Date) - ([DateTime]::FromFileTime($_.lastLogon))).Days }},
distinguishedname |
ForEach-Object {
if ($DataAValues.Contains($_.SamAccountName)) {
if ($DataAValues[$_.SamAccountName].LastLogon -lt $_.LastLogon) {
$DataAValues[$_.SamAccountName].LastLogon = $_.LastLogon
}
} else {
$DataAValues.Add($_.SamAccountName, $_)
}
}
}
$DataA = $DataAValues.Values
$AllMailboxes = Get-Mailbox -ResultSize Unlimited -OrganizationalUnit $OrganizationalUnit |
Where {$_.OrganizationalUnit -eq $OrganizationalUnit_Format} |
Select-Object alias,displayname,distinguishedname
$DataB = @()
$InactiveMailbox = ForEach($Mailbox in $AllMailboxes) {
$DataB += Get-MailboxStatistics $Mailbox.alias | Where {$_.LastLogonTime -lt $Time -AND $_.WhenMailboxCreated -lt $Time} |
Select-Object displayname,
lastlogontime,
lastloggedonuseraccount,
@{Name="ReferenceDate"; Expression={($CurrentTimeStamp)}},
@{Name='MailboxDaysInactive'; Expression={ ((Get-Date) - ($_.LastLogonTime)).Days} },
@{Name='DistinguishedName'; Expression={($Mailbox.distinguishedname)} }
}
$properties = $DataA[0].PSObject.Properties.Name + $DataB[0].PSObject.Properties.Name
$IndexedProperty = 'distinguishedName'
$DataBIndex = @{}
for ($i = 0; $i -lt $DataB.Count; $i++) {
$DataBIndex.Add($DataB[$i].$IndexedProperty, $i)
}
foreach ($itemA in $DataA) {
if ($DataBIndex.Contains($itemA.$IndexedProperty)) {
$itemB = $DataB[$DataBIndex.($itemA.$IndexedProperty)]
foreach ($property in $itemB.PSObject.Properties) {
$itemA | Add-Member $property.Name $property.Value
}
$itemA | Select-Object $properties | Export-CSV -Path D:\Export\Result.csv -Append -NoTypeInformation
}
}
ASKER
Exchange is the server side of a collaborative application product that is part of the Microsoft Server infrastructure. Exchange's major features include email, calendaring, contacts and tasks, support for mobile and web-based access to information, and support for data storage.
TRUSTED BY