Hello,
So I have created the following lines from machine A and wish to add that account on machine B
Machine A:
$secureStringPassword = ConvertTo-SecureString -String 'supersecret' -AsPlainText -Force
$credential = [PSCredential]::new( 'lab', $secureStringPassword )
$credential = New-Object -TypeName PSCredential -ArgumentList 'lab', $secureStringPassword
$credential | Export-Clixml D:\test4\mycredential.xml
Machine B:
In another powershell script where I deploy the accounts I have the following code:
## <PERFORM INSTALLATION TASKS HERE>
$group = "Administrators"
$Username = "lab"
$credential = [Xml] (Get-Content -Path "$ScriptDirectory\SupportFiles\mycredential.xml")
$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
$existing = $adsi.Children | where {$_.SchemaClassName -eq 'user' -and $_.Name -eq $Username }
if ($existing -eq $null) {
& NET USER $Username $Password /add /y /expires:never
& NET LOCALGROUP $group $Username /add
}
else { $existing.SetPassword($Password) }
& WMIC USERACCOUNT WHERE "Name='$Username'" SET PasswordExpires=FALSE
$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set-ItemProperty -Path "$RegPath" -Name "AutoAdminLogon" -Value "1"
Set-ItemProperty -Path "$RegPath" -Name "DefaultUsername" -Value "$Username"
Set-ItemProperty -Path "$RegPath" -Name "DefaultPassword" -Value "$Pass"
How can I add the xml file so it fits the script? If I keep the $Username = "lab" the deployment works I can access Windows.
But if I remove that line and replace $credential with $Username it does not work
I also tried to replace $Username $Password with $credential but it didn't work.