Change pass for local user on remote server

by Dmitry at 2013-04-05 00:13:57

Hello!
Need help writing a script that would run on a single server (Server1). Script must create and changed the password for a local user on the remote server (Server2). After doing sends to a new password. Password change should occur every month. Found an example for the user in AD]import-module activedirectory

[int] $len = 12
[string] $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
$bytes = new-object "System.Byte" $len
$rng = new-object System.Security.Cryptography.RNGCryptoServiceProvider
$rng.GetBytes($bytes)
$result = ""
for( $i=0; $i -lt $len; $i++ )
{
$result += $chars[ $bytes[$i] % $chars.Length ]
}
$result

$securestring = ConvertTo-securestring $result -asplaintext -force

get-aduser "GuestUserName" | set-adaccountpassword -newpassword $securestring

$month= get-date -format MMMM

###Sets the mail values
$FromAddress = "Wireless_Guest@some-domain.com"
$ToAddress = "public-folder@some-domain.com"
$MessageSubject = "New Wireless Guest Details for $month"
$MessageBody = "Username: GuestUserName Password: $result"
$SendingServer = "my.mail-relay.com"

###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody

###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)[/powershell]

But how to adapt this script for my task?It seems to have come module Local Account Management Module 2.1. Maybe someone has already faced a similar task
by DonJ at 2013-04-05 00:23:46
What is it, exactly, that you need to change in this script?
by Dmitry at 2013-04-05 00:46:22
In this script need to change the module to Local Account Management . I understand that there will be something similar to this:
import-module LocalAccounts

[int] $len = 12
[string] $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
$bytes = new-object "System.Byte" $len
$rng = new-object System.Security.Cryptography.RNGCryptoServiceProvider
$rng.GetBytes($bytes)
$result = ""
for( $i=0; $i -lt $len; $i++ )
{
$result += $chars[ $bytes[$i] % $chars.Length ]
}
$result

$securestring = ConvertTo-securestring $result -asplaintext -force

Get-LocalUser -Name Username | Set-LocalUserPassword -NewPassword $securestring
$month= get-date -format MMMM

###Sets the mail values
$FromAddress = "Wireless_Guest@some-domain.com"
$ToAddress = "public-folder@some-domain.com"
$MessageSubject = "New Wireless Guest Details for $month"
$MessageBody = "Username: GuestUserName Password: $result"
$SendingServer = "my.mail-relay.com"

###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody

###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)


But I do not understand how the code looks like this syntax:
SYNTAX
Set-LocalUserPassword -NewPassword <String> [-Name] <String> [[-Source] <String>] [-Credential <PSCredential>] [<CommonParameters>]
If you just run the example code like this:
import-module LocalAccounts
Get-LocalUser -Name Username | Set-LocalUserPassword -NewPassword Qq123Lj

after the error]Set-LocalUserPassword : Exception has been thrown by the target of an invocation.
At C:\Users\User\Desktop\test.ps1:2 char:34
+ Get-LocalUser -Name Username | Set-LocalUserPassword -NewPassword Qq123Lj
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (Username:UserPrincipal) [Set-LocalUserPassword], TargetInvocationException
+ FullyQualifiedErrorId : UserSetPasswordError,LocalAccounts.SetLocalUserPassword[/powershell]
by DonJ at 2013-04-05 01:43:14
Most people would use the Win32_userAccount class from WMI. I have not personally worked with the module you’re using. But it may just be that the password you supplied doesn’t meet the complexity and length requirements. You do appear to have the correct syntax.
by Dmitry at 2013-04-05 02:27:54
And how will look the script using Win32_userAccount class from WMI? Password meets the requirements as a snap it easily installed.