-1

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

Ansgar Wiechers
  • 175,025
  • 22
  • 204
  • 278
nicolas
  • 61
  • 1
  • 9
  • Have a look at [Enter-PSSession](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/enter-pssession?view=powershell-6). – Theo Jul 11 '19 at 07:45
  • this is not a pssession problem or for passing arguments in a scriptblock, the problem is the argument -name seems to be empty but it isnot empty – nicolas Jul 11 '19 at 07:47
  • 2
    No, they are right. You reference `$name` in your scriptblock that you pass to `Invoke-Command`, but that scriptblock runs in another session where `$name` is not defined. – TheMadTechnician Jul 11 '19 at 07:55
  • ok this does make sense to me, so i tried to start the session in powershell cmd defind the variables and executed the scriptblock, then it worked, but i dont know how to implement that into my script – nicolas Jul 11 '19 at 08:30
  • @nicolas Please show your modified code. How and where do you define those variables? I can practically guarantee you that the variables are not defined in the scope you intend to use them in. – Ansgar Wiechers Jul 11 '19 at 08:35
  • now i modified it, to my best understanding – nicolas Jul 11 '19 at 08:47
  • i tested the connection and it is connected, also when i replace `$name` with a string the script works. How can i set `$name` in the scope of the session, that it works? – nicolas Jul 11 '19 at 09:11
  • Please read the answers to the linked questions again. Your scriptblock now has named parameters, but you still need to pass the variables from your script into the scriptblock. The `Param()` directive won't do that for you. Also, `Enter-PSSession` is for interactive use. Don't use it for automation, and particularly do not mix `Enter-PSSession` with `Invoke-Command`. Pick either one or the other. – Ansgar Wiechers Jul 11 '19 at 15:18
  • thank you very much for all your answers i fixed it only using `invoke-command -scriptblock{}` defining the variable as for example `$firstname=$using:firstname` that way it works – nicolas Jul 22 '19 at 08:57

0 Answers0