Author Posts

May 16, 2017 at 12:45 pm

Hi guys.

I wrote little function, It reset user password on my domain.
And it work very well when i logging on domain controller. But how could i run it function remotely?

I mean, i run script on my pc and i write credential and script start remotely on server.

function Reset-UserPassword{
    
    [CmdletBinding()]
    param(
        [string]$UserName = (Read-Host "Enter User Name for Reset Password"),
        $message = ("##### User Password has been reset to:").ToUpper()
        )

    Set-ADAccountPassword -Identity $UserName -Reset -NewPassword (ConvertTo-SecureString "Pa$$w0rd" -AsPlainText -Force)

    Write-Host $message "Pa$$w0rd #####"

} 

I try insert something like this:

Enter-PsSession -ComputerName $mySRV -Credential $myCred

function reset{
.....
}
Exit-PsSession

but it do not work properly.

Could you help me pls with it ?

May 16, 2017 at 12:49 pm

The correct code formatting tags are shown above the posting text box.

I assume you know how awful it is to have a hardcoded, clear-text password in your script, and that you've already taken other measure so that you don't need to do that.

In your example of Exit-PSSession, I don't see where you are actually running your function. Additionally, if you could provide more detail than "it do not work properly," that would be helpful. Is there an error? Does anything at all happen?

May 16, 2017 at 2:31 pm

Hi, Don.

Thank you for you answer.

I know about clear-text password i wrote it for an example.

I have code which i wan to run remotely on server. For an Example above and belove.

function Reset-UserPassword{
    
    [CmdletBinding()]
    param(
        [string]$UserName = (Read-Host "Enter User Name for Reset Password"),
        $message = ("##### User Password has been reset to:").ToUpper()
        )

    Set-ADAccountPassword -Identity $UserName -Reset -NewPassword (ConvertTo-SecureString "Pa$$w0rd" -AsPlainText -Force)

    Write-Host $message "Pa$$w0rd #####"
}

When i run it on server it work properly. User have new password "password" (for an example).

So, i wan to run it on my pc but and it must running on server, for it i use Enter-PsSession, for an example:

Enter-PsSession -ComputerName main-server -Credential domain\Administrator

function Reset-UserPassword{
    
    [CmdletBinding()]
    param(
        [string]$UserName = (Read-Host "Enter User Name for Reset Password"),
        $message = ("##### User Password has been reset to:").ToUpper()
        )

    Set-ADAccountPassword -Identity $UserName -Reset -NewPassword (ConvertTo-SecureString "Pa$$w0rd" -AsPlainText -Force)

    Write-Host $message "Pa$$w0rd #####"
}

Reset-UserPassword

Exit-PsSession

When i start this on my pc i have next:
1) System asking me for my admin cred
2) I typing my admin passowrd in Windows Power Shell credential require window
3) System asking me for user name, i typing ad user name
4) i have next error:

Set-ADAccountPassword : The term 'Set-ADAccountPassword' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was include
d, verify that the path is correct and try again.
At line:11 char:5
+ Set-ADAccountPassword -Identity $UserName -Reset -NewPassword (ConvertTo-Sec ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-ADAccountPassword:String) [], CommandNotFoundExc
eption
+ FullyQualifiedErrorId : CommandNotFoundException

I think it happen because script still running on my pc.

What's wrong ?

May 16, 2017 at 2:55 pm

are you sure you have the active directory module installed on the remote computer?

May 16, 2017 at 2:59 pm

yup, this code (belove) work properly on server

function Reset-UserPassword{
    
    [CmdletBinding()]
    param(
        [string]$UserName = (Read-Host "Enter User Name for Reset Password"),
        $message = ("##### User Password has been reset to:").ToUpper()
        )

    Set-ADAccountPassword -Identity $UserName -Reset -NewPassword (ConvertTo-SecureString "Pa$$w0rd" -AsPlainText -Force)

    Write-Host $message "Pa$$w0rd #####"
}

I adding (Enter-PsSession) to connect to this server from my PC.

I add to this code next string: Write-Host "Im in $env:COMPUTERNAME" after Enter-PsSession param and i have had next msg: Im in KLIMOV-PC.

So, I entered to the remote server after all the parts of the script worked (when script worked out PS connected me to remote server). But how may i do asking script working step by step ?

1) Dude please enter to the remote server here you are credential parameters for you
1.1) Ok Dude i'm in there
2) Cool, run pls function Reset-UserPassword
2.2) ok it done
3) Cool, invoke pls Reset-UserAccount for user test
3.1) done
4) Close session
4.1) done