locked
Need help with a Script - Create User RRS feed

  • Question

  • Can you all look and see where I am going wrong?

    [cmdletbinding()]
    param (
    [parameter(mandatory = $true)]
    $InputFile,
    $OutputDirectory
    )

    if(!$outputdirectory) {
    $outputdirectory = (Get-Item $InputFile).directoryname
    }
    $failedcomputers = Join-Path $outputdirectory "failed-computers-accountcreation.txt"
    $stream = [System.IO.StreamWriter] $failedcomputers
    $stream.writeline("ComputerName `t IsOnline `t AccountCreationStatus")
    $stream.writeline("____________ `t ________ `t ____________________")

    $user = Read-Host "Enter the Username"

    $password = Read-Host "Enter the password" -AsSecureString
    $confirmpassword = Read-Host "Confirm the password" -AsSecureString

    $pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
    $pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($confirmpassword))

    if($pwd1_text -ne $pwd2_text) {
    Write-Error "Entered passwords are not same. Script is exiting"
    exit
    }

    if(!(Test-Path $InputFile)) {
    Write-Error "File ($InputFile) not found. Script is exiting"
    exit
    }

    $Computers = Get-Content -Path $InputFile

    foreach ($Computer in $Computers) {
    $Computer = $Computer.toupper()
    $Isonline = "OFFLINE"
    $Status = "SUCCESS"
    Write-Verbose "Working on $Computer"
    if((Test-Connection -ComputerName $Computer -count 1 -ErrorAction 0)) {
    $Isonline = "ONLINE"
    Write-Verbose "`t$Computer is Online"
    } else { Write-Verbose "`t$Computer is OFFLINE" }

    try {
    $server = [ADSI]"WinNT://$Computer"
    $newuser = $server.Create("User","$user")
    $newuser.SetPassword("$pwd1_text")
    Write-Verbose "`tAccount created successfully"
    }
    catch {
    $status = "FAILED"
    Write-Verbose "`tFailed to create user. Error: $_"
    }

    try {
    $newuser = [ADSI]("WinNT://$user")
    $Group = [ADSI]("WinNT://$Computer/Administrators,Group")
    $Group.PSBase.Invoke("Add",$newuser.PSBase.Path)
    Write-Verbose "`tAdded user to Administrator's group successfully"
    }
    catch {
    $status = "FAILED"
    Write-Verbose "`tFailed to add user to Administrator's group. Error: $_"
    }

    $obj = New-Object -TypeName PSObject -Property @{
      ComputerName = $Computer
      IsOnline = $Isonline
      AccountCreationStatus = $Status
    }

    $obj | Select ComputerName, IsOnline, AccountCreationStatus

    if($Status -eq "FAILED" -or $Isonline -eq "OFFLINE") {
    $stream.writeline("$Computer `t $isonline `t $status")
    }

    }
    $stream.close()
    Write-Host "`n`nFailed computers list is saved to $failedcomputers"

    Monday, May 6, 2013 6:50 PM

Answers

  • I don't see where you invoke SetInfo anywhere. The object only exists in memory until you invoke SetInfo to save it.


    Richard Mueller - MVP Directory Services

    • Proposed as answer by Yan Li_ Friday, May 10, 2013 5:13 AM
    • Marked as answer by Yan Li_ Thursday, May 16, 2013 3:05 AM
    Monday, May 6, 2013 7:51 PM

All replies

  • could you specify your issue, its hard to analyze all of that code since i cant run it in my env..

    MK

    Monday, May 6, 2013 6:52 PM
  • It would probably be nice if you actually, tell us what is wrong. What errors are you receiving, what isn't working as you intended. Just posting code and having us look over it, really isn't going to work.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Monday, May 6, 2013 6:52 PM
  • I don't see where you invoke SetInfo anywhere. The object only exists in memory until you invoke SetInfo to save it.


    Richard Mueller - MVP Directory Services

    • Proposed as answer by Yan Li_ Friday, May 10, 2013 5:13 AM
    • Marked as answer by Yan Li_ Thursday, May 16, 2013 3:05 AM
    Monday, May 6, 2013 7:51 PM