none
Parameter to generate a log file to a script. RRS feed

  • Question

  • 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

    Thursday, June 26, 2014 3:03 PM

Answers

  • I got the expression HELP Start-Transcript -FULL would record the session to a text file but some one can help to tell me the command I can use with this? Sorry if i am asking same question again.

    TechNet

    I think you are just playing games with us.  If you do not know how to use help then you need to go to another forum for new Windows users and ask how to use Windows.

    I really don't think we can help you in any way. YOu have no background in technology and apparently not much experience with Windows. Both of the are prerequisites for scripting.

    Not also that this is not a forum where you can get your scripts fixed.  We can answer questions but  you will have to do the fixing or contact a consultant to help you.


    ¯\_(ツ)_/¯

    Thursday, June 26, 2014 5:50 PM

All replies

  • What's the purpose of your script and why do you need a log file?

    -- Bill Stewart [Bill_Stewart]

    Thursday, June 26, 2014 3:29 PM
    Moderator
  • The purpose of the script is to take user extract from active directory. I am looking for a log file to capture errors if any when the scheduler run the job to get extract.

    TechNet

    Thursday, June 26, 2014 3:36 PM
  • By "user extract," I assume you mean you want to extract data about users from Active Directory.

    So I don't have to try to read and understand your code, what attributes of users are you interested in retrieving?

    What exactly is failing that provokes you to ask about creating a log file?


    -- Bill Stewart [Bill_Stewart]


    Thursday, June 26, 2014 3:38 PM
    Moderator
  • In detail....Start-Transcript

    HELP Start-Transcript -FULL

    That is the easiest way to log all steps.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, June 26, 2014 3:39 PM
    • Proposed as answer by jrv Thursday, June 26, 2014 5:35 PM
    Thursday, June 26, 2014 3:39 PM
  • There are a large number of logic, syntax and other errors.  You have compensated but will never get output.

    Here is a closer attempt using real PowerShell syntax and commands.

    $props=@{ 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') } Get-ADUser @props | ForEach-Object{ $group=@() Get-ADPrincipalGroupMembership $_.SamAccountName | Foreach-Object{ if($_.Name -match 'staff|student|faculty'){ $group+=$_.Name } }
    if(-not $group){$group+='other'} $group=$group -join ',' $newobj=@{ System_Role = "NONE" External_Person_Key = $_.cn User_ID = $_.samAccountName Passwd = "test1" FirstName = $_.givenName MiddleName = $_.middleName LastName = $_.sn Email = $_.mail Institution_Role = $group } New-Object PSObject -Property $newobj } | Export-Csv c:\BB.dat -NoTypeInformation -Delimiter '|' -Encoding ASCII

    It should also be easier to read.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, June 26, 2014 4:01 PM
    Thursday, June 26, 2014 3:59 PM
  • Thank is correct - "user extract," I mean to extract data about users from Active Directory. Script is working fine but not consistently i am getting output. Sometime I am getting accurate number of user extract and some time not which looks like it is failing in the background. I want to get log file which tells me why script failed (if it failed) or if finished then some thing like succeeded message. 

    TechNet

    Thursday, June 26, 2014 4:03 PM
  • I got the expression HELP Start-Transcript -FULL would record the session to a text file but some one can help to tell me the command I can use with this? Sorry if i am asking same question again.

    TechNet

    Thursday, June 26, 2014 5:44 PM
  • I got the expression HELP Start-Transcript -FULL would record the session to a text file but some one can help to tell me the command I can use with this? Sorry if i am asking same question again.

    TechNet

    I think you are just playing games with us.  If you do not know how to use help then you need to go to another forum for new Windows users and ask how to use Windows.

    I really don't think we can help you in any way. YOu have no background in technology and apparently not much experience with Windows. Both of the are prerequisites for scripting.

    Not also that this is not a forum where you can get your scripts fixed.  We can answer questions but  you will have to do the fixing or contact a consultant to help you.


    ¯\_(ツ)_/¯

    Thursday, June 26, 2014 5:50 PM