PS Studio - Create Active Directory Users from a remote PC

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 9 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
shamir_79
Posts: 33
Last visit: Sun Aug 02, 2015 6:41 am

PS Studio - Create Active Directory Users from a remote PC

Post by shamir_79 »

Hi,

This is my setup

Active Directory server Name: dc01
Client: Win 8
Domain Name: cyquent.ae


I have created a FORM to create Active Directory users in my Domain.

When I open my FORM in the Domain server locally it works perfectly well. (User is getting created without any issues) :)


Question: -

I want to use the invoke-command so that I can run the form on a remote PC, So that I can use this FORM to create Domain Users from a remote PC as well.

I have attached my FORM screenshot to get an idea about my FORM.

These are the steps I used to follow to create users: -

1. Fill the fields with user details
2. Click "CREATE" Button
3. Once you click the CREATE button you should be able to see the user details on the "STATUS" and user should be created in the AD as well

Command I have given in the "CREATE" button:-


$theOU = $comboboxOU.Text

$myOU = Invoke-Expression "(Get-ADOrganizationalUnit -LDAPfilter '(Name=$theOU)' -SearchBase 'OU=Users,OU=LabOU,DC=cyquent,DC=ae').DistinguishedName"


if ($checkboxEnabled.Checked -eq $true)

{
New-ADUser -Path $myOU -DisplayName $txtBoxDisplayName.Text -GivenName $txtBoxGivenname.Text -City $comboboxOffice.Text -Country $comboboxCountry.Text -Description $txtBoxDescription.Text -Surname $txtBoxSurname.Text -Name $txtBoxDisplayName.Text -SamAccountName $txtBoxLoginName.Text -UserPrincipalName $txtBoxLoginName.Text -AccountPassword (ConvertTo-SecureString $txtBoxPassword.Text -AsPlainText -Force) -ChangePasswordAtLogon:$true -Enabled:$true
}


else
{
New-ADUser -Path $myOU -DisplayName $txtBoxDisplayName.Text -GivenName $txtBoxGivenname.Text -City $comboboxOffice.Text -Country $comboboxCountry.Text -Description $txtBoxDescription.Text -Surname $txtBoxSurname.Text -Name $txtBoxDisplayName.Text -SamAccountName $txtBoxLoginName.Text -UserPrincipalName $txtBoxLoginName.Text -AccountPassword (ConvertTo-SecureString $txtBoxPassword.Text -AsPlainText -Force) -ChangePasswordAtLogon:$true -Enabled:$false

}




Note: -

In the FORM load section I have imported the Active Directory module and it works fine.

I think problem is happening with the "-Path $myOU" section.

Please help me on this.

Hope this is clear.

Thanks,
Shamir
Attachments
Interface.jpg
Interface.jpg (151.43 KiB) Viewed 4529 times
User avatar
SAPIEN Support Forums
Posts: 945
Last visit: Thu Oct 22, 2015 1:10 pm

PS Studio - Create Active Directory Users from a remote PC

Post by SAPIEN Support Forums »

This is an automated post. A real person will respond soon.

Thank you for posting, shamir_79.

Here are some hints to help you get an accurate and complete answer to your question.

Ask in the best forum: If you asked in the wrong forum, just copy your question to the right forum.

Anticipate follow-up questions!

Did you remember to include the following?
  • 1. Product, version and build
    2. 32 or 64 bit product
    3. Operating system, e.g. Windows 7 64 bit.
    4. Attach a screenshot, if applicable
    5. Attach logs, crash reports, etc., in a ZIP file
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PS Studio - Create Active Directory Users from a remote

Post by jvierra »

Isn't this the same question you asked before?

http://www.sapien.com/forums/viewtopic. ... 821#p44821

I already showed you how to do that so why are you asking the same quesion with a picture?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PS Studio - Create Active Directory Users from a remote

Post by jvierra »

Note that you cannot execute commands in a local session without importing the remote session.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PS Studio - Create Active Directory Users from a remote

Post by jvierra »

Here is how to structure the New-AdUser Cmdlety loading correctly.
PowerShell Code
Double-click the code block to select all.
$userprops=@{
    Path=$comboboxOU.SelectedItem.DistinguishedName
    DisplayName=$txtBoxDisplayName.Text 
    GivenName=$txtBoxGivenname.Text 
    City=$comboboxOffice.Text 
    Country=$comboboxCountry.Text
    Description=$txtBoxDescription.Text
    Surname=$txtBoxSurname.Text 
    Name=$txtBoxDisplayName.Text 
    SamAccountName=$txtBoxLoginName.Text 
    UserPrincipalName=$txtBoxLoginName.Text 
    AccountPassword=(ConvertTo-SecureString $txtBoxPassword.Text -AsPlainText -Force)
    ChangePasswordAtLogon=$true 
    Enabled=$checkboxEnabled.Checked 
}
New-ADUser @userprops
You would be better off installing RSAT. If you insist on remoting then you are going to have to learn more about how to use PowerShell remoting from th ecommandline before attempting it in a form. In remoting we can "Import" the session to make this work. If you onlly use Invoke-Command it wil be very slow and cause you to have to write a lot of extra code.

YOu cannot us eInvoke-Expression at all. iIT is not intended to be used as you are using it.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PS Studio - Create Active Directory Users from a remote

Post by jvierra »

Here is an example of how to statically import a session. The attached method will work in a forms script and not just in PowerShell Studio.

Of course you can also set PowerShell Studio to do the remoting and it will be incorporated into a script or EXE.
Attachments
Demo-RemoteForm.psf
(27.68 KiB) Downloaded 195 times
User avatar
shamir_79
Posts: 33
Last visit: Sun Aug 02, 2015 6:41 am

Re: PS Studio - Create Active Directory Users from a remote

Post by shamir_79 »

Hi,

From my remote PC I tried using below command to create users in AD. But in the "-path" section I am getting an error. Please help me to resolve this.

-----------------------------------------------
$theOU = "HR"
#status = success :|

$myOU = Invoke-Command -ComputerName dc01 -ScriptBlock {"(Get-ADOrganizationalUnit -LDAPfilter '(Name=$theOU)' -SearchBase 'OU=Users,OU=LabOU,DC=cyquent,DC=ae').DistinguishedName" }
#status = success :|


Invoke-Command -ComputerName dc01 -ScriptBlock {New-ADUser -DisplayName peter -Path $myOU -GivenName peter -City Houston -Country US -Description Accountant -Surname jason -Name peter -SamAccountName peter -UserPrincipalName peter -AccountPassword (ConvertTo-SecureString password@123 -AsPlainText -Force) -ChangePasswordAtLogon:$true -Enabled:$true}

#status = Error :oops:
Error: Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or
empty, and then try the command again.
----------------------------------------------------------


Note:- If I type -path as -PATH 'OU=HR,OU=Users,OU=LabOU,DC=cyquent,DC=ae' I am able to create the user successfully.

But I need to use it through a variable as I was trying.

Note: - "HR" is the Organization Unit name where I am trying to create the user. :|

plz help me.

Thanks,
Shamir
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PS Studio - Create Active Directory Users from a remote

Post by jvierra »

You cannot pass variables into a scriptblock. Use the "ArgumentList" paramter to pass argumetns.
User avatar
shamir_79
Posts: 33
Last visit: Sun Aug 02, 2015 6:41 am

Re: PS Studio - Create Active Directory Users from a remote

Post by shamir_79 »

Okay...

Could you please help me to use "ArgumentList" paramter for this example as I am putting lots of efforts to get this thing done.?


For last 03 days I am struggling with this :oops:
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PS Studio - Create Active Directory Users from a remote

Post by jvierra »

This topic is 9 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked