0
votes

i have a variable $user which has an AD user stored in it. i want to change its UPN. if i write this on dc:

$user | set-aduser -UserPrincipalName [email protected]

it works fine. but if i use invoke command like so

Invoke-Command -Session $S -ScriptBlock  {$using:ADUSER | set-aduser -userprincipalname [email protected]}

i get an error (assume $s is a good session, im using it in other lines) my error is:

The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. + CategoryInfo : InvalidArgument: (CN=uzi,OU=IT,OU...shahar,DC=local:PSObject) [Set-ADUser], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.SetADUser + PSComputerName : dc01

edited: lines for aduser

$uname =  $script:textbox1.text+"@"+$script:textbox2.text

$ADUSER = Invoke-Command  -Session $S -ScriptBlock {Get-aduser -property 'emailaddress' -filter "UserPrincipalName -eq '$using:UNAME'"}

can anyone assist please?

1
assuming $ADUSER is the samaccountname (e.g. jdoe1234) try Invoke-Command -Session $S -ScriptBlock {set-aduser -identity $using:ADUSER -userprincipalname [email protected]} - Guenther Schmitz
your code does not define $AdUser ... please add that to your code so that one can do something lore than make wild guesses ... [grin] - Lee_Dailey

1 Answers

1
votes

I think you should add a param block to your scriptblok and use that

Something like

Invoke-Command -Session $S -ScriptBlock {param ($ADUser, $UPN = '[email protected]'); $ADUser | Set-ADUser -UserPrincipalName $UPN } -ArgumentList $ADUser, '[email protected]'

or better readable:

$scriptBlock = {
    param (
        $ADUser, 
        $UPN = '[email protected]'
    )
    $ADUser | Set-ADUser -UserPrincipalName $UPN
}
Invoke-Command -Session $S -ScriptBlock $scriptBlock -ArgumentList $ADUser, '[email protected]'