Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating Password 1

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
Hi,

I've got a little powershell script that creates a text file with an encrypted string. This string is read by the New-Mailbox -Password commandlet as part of a tool used by our helpdesk to create new users in AD.

This password is the same for all new user accounts, however each user that installs the new user app has to regenerate the hash password. I've written an HTA setup programme to generate this password rather than using the powershell command, however I cannot get it to take the password from the text box within the HTA.

The command line code:
Code:
$secure = Read-Host "Enter Password" -asSecureString  
$bytes = ConvertFrom-SecureString $secure  
$bytes | Out-File defaultPassword.txt

I've changed from above to:
Code:
param($strPassword)

$secure = $strPassword -asSecureString  
$bytes = ConvertFrom-SecureString $secure  
$bytes | Out-File defaultPassword.txt

However, I get an error saying it needs a value expression to the right of the '-'.

I can get the password from the HTA into the powershell command, but I cannot get it to convert it.

If anybody can point me in the right direction, I'd be most grateful.

Many thanks
Can anyone
 
-asSecureString is a parameter to the Read-Host cmdlet. It is not a method of the System.String type. You could try converting the password as follows:
Code:
param($strPassword)

$secure = ConvertTo-SecureString $strPassword -asPlainText -Force
$bytes = ConvertFrom-SecureString $secure
$bytes | Out-File defaultPassword.txt

 
Thanks crobin1. That worked.

For anyone that may be interested, here is the code to generate a password using powershell via VBScript:

Powershell code:
Filename: CreatePassword.ps1
Code:
param($strPassword)
$secure = ConvertTo-SecureString $strPassword -asPlainText -Force
$bytes = ConvertFrom-SecureString $secure
$bytes | Out-File defaultPassword.txt

VBScript Code:
Filename: PSpasswordGen.vbs
Code:
Set fso = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")

strCurrentDir = oShell.CurrentDirectory
strScriptPath = fso.GetParentFolderName(oShell.CurrentDirectory)

runPowershell

Sub runPowershell()

MsgBox "If this script doesn't work you will need to manually run a powershell script." & VbCrLf & _
		"Click Start --> Run and type: Powershell. Click OK." & VbCrLf & _
		"At the PS prompt, type 'Set-ExecutionPolicy Unrestricted -Force'",64,"Tip."

strCreateUserPath = strScriptPath & "\bin\Security\CreatePassword.ps1"

Set oFile=fso.GetFile(strCreateUserPath)

strPasswd = "changeit"

strPScmd = strPasswd

oShell.run "Powershell -command  " & chr(34) & "&{" &oFile.ShortPath & " " & strPScmd & "}" & chr(34),7,True
'Place '-noexit' after Powershell to see what powershell is doing.
End Sub

If the powershell script fails, check powershell scripts are alowed to run:

Code:
PS C:\>Get-ExecutionPolicy

(If you need to change it:)

PS C:\>Set-ExecutionPolicy Unrestricted -Force

MS Article here

Hope this helps and thanks again.

W
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top