I am using below script. My script is not giving me consistent result.
May i ask, if I can use any statement with a script to get the log file generated so we could find out if my script is extracting exact user count or if it is failing i could get in log file which I could export via email every time it runs.
Any suggestions please? I am a rookie in writing power shell code so please tell me in detail. Thank you
Script I am using.
Import-Module ActiveDirectory
Get-ADUser -Filter { memberOf -RecursiveMatch "CN=status,OU=Test,OU=Users,DC=QA,DC=GU,DC=com" } -SearchBase "OU=Test,OU=Users,DC=QA,DC=GU,DC=com" -Properties cn, samaccountname, givenName, middlename, sn, mail | Foreach-Object {
$group = ""
Get-ADPrincipalGroupMembership $_.SamAccountName | Foreach-Object {
if (($_.Name -eq "staff") -or ($_.Name -eq "student") -or ($_.Name -eq "faculty"))
{
$group += $_.Name + ","
}
}
if ($group -eq "")
{
$group = "other"
}
else
{
$group = $group.Substring(0, $group.Length - 1)
}
New-Object -TypeName PSObject -Property @{
System_Role = "NONE"
External_Person_Key = $_.cn
User_ID = $_.samAccountName
Passwd = "test1"
FirstName = $_.givenName
MiddleName = $_.middleName
LastName = $_.sn
Email = $_.mail
Institution_Role = $group
} | Select-Object System_Role, External_Person_Key, User_ID, Passwd, FirstName , MiddleName, LastName, Email, Role
} | convertto-csv -Delimiter '|'-notype |% {$_ -replace '"', ""} | out-file c:\BB.dat -Force -Encoding ascii
** Here is need to add statement to get log file***
TechNet