I need to add new users into my AD from a remote computer. WMI is activated - so no problem with that. The argument -Name
is not validated but the variable is set.
The PowerShell command works on my domain controller, but not remote. I have tried to set the PowerShell variable $name
to a static string.
Param(
$firstname,
$lastname,
$password,
$telnum,
$strasse,
$ort,
$bundesland,
$plz,
$land,
$department,
$firma,
$stadt,
$mobilenummer,
$postfach,
$abteilung,
$website
)
$secpasswd = ConvertTo-SecureString "Admin123" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("administrator@lab.local", $secpasswd)
$s1 = New-PSSession -ComputerName srv-dc01.lab.local -Credential $mycreds
Enter-PSSession -Session $s1
$name = "$firstname$lastname"
$username = $lastname.Substring(0, 2) + $firstname.Substring(0, 2)
$usethatname
$checked = 1;
$count = 0;
while ($checked -eq 1) {
$count++;
$excist = Invoke-Command -Session $s1 -ScriptBlock {
Get-ADUser -Filter {SamAccountName -like "$username"}
}
if (!$excist) {
$checked = 0
} else {
$usethatname = "$username$count"
$username = $usethatname
}
}
if (Test-Connection -ComputerName srv-dc01.lab.local -Quiet) {
$pw = (ConvertTo-SecureString -String $password -AsPlainText -Force)
$members_id = Invoke-Command -Session $s1 -ScriptBlock {
Param($name, $department, $firma, $land, $firstname, $stadt, $mobilenummer, $plz, $username, $lastname, $bundesland, $strasse, $abteilung, $postfach, $telnum, $pw)
$name = "$firstname$lastname"
New-ADUser -Name $name -Department $department -Company $firma -Country $land -Givenname $firstname -L $stadt -Mobile $mobilenummer -PostalCode $plz -SamAccountName $username -Surname $lastname -State $bundesland -StreetAddress $strasse -Title $abteilung -POBox $postfach -OfficePhone $telnum -AccountPassword $pw -PassThru
}
} else {
Write-Host "did not work"
}
Exit-PSSession
Error message:
Das Argument für den Parameter "Name" kann nicht überprüft werden. Das Argument ist NULL oder leer. Geben Sie ein Argument an, das nicht NULL oder leer ist, und führen Sie den Befehl erneut aus. + CategoryInfo : InvalidData: (:) [New-ADUser], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.NewADUser + PSComputerName : srv-dc01.lab.local
Translation:
The argument for param "name" cannot be validated becouse the argument is null or empty.
New-ADUser cannot validate argument on parameter name