I cannot use a variable for Set-AdComputer within the invoke-command

I appreciate the help as I am new to PowerShell.
I am trying to script changing the Set-AdComputer using an imported list of servers. I am able to run the script using the server name directly. As soon as I use a variable I get the following error: "Cannot validate argument on parameter 'Identity'. The argument is null."
Even if I remove the Import-CSV and set the variable manually I still get the same error.

Here is a simplified version of the script.

I appreciate the help!

Bret
Set-StrictMode -Version Latest
Import-Module ActiveDirectory

$Time = (Get-Date -UFormat "%A, %b %d, %Y %r")
$CertChk = (New-PSSessionOption -SkipCNCheck -SkipCACheck -SkipRevocationCheck)
$Server = "ServerName"
$Desc = "Account Disabled Due to DECOM on $Time"


$SvcAcctCreds = IMPORT-CLIXML "$env:USERPROFILE\svc_acct_corp_creds.xml"
$CorpDesc = Invoke-Command -ComputerName DC -Credential $SvcAcctCreds -UseSSL -SessionOption $CertChk -ScriptBlock {Set-ADComputer $Server -Description $Desc}

$CorpDesc

Open in new window

Bret RobinsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Greg BessoIT Solutions EngineerCommented:
Hi there, I've run into that at one point. I am not sure if it's the only/best way but what I read and then did was to create a filter scriptblock and then pass that along with the function call. Something  like this for example...

$filter = [scriptblock]::Create("SamAccountName -eq `"$SamAccountName`"")
$user = Get-ADUser -Filter $filter -Properties *

Try it out and let us know if that helps. :-)
0
Greg BessoIT Solutions EngineerCommented:
I think I misunderstood your question actually. I was thinking of another issue I ran into related to passing variables to filters in one-line remote sessions somewhere else. What I think you meant was maybe something like this. Here is one example of how to send your variable from local script to the remote session...

# just a test variable with something to send to the remote system...
$var1 = 'username'

#make the session if you don't have it already...
If (!($sessionAD)) { $sessionAD = New-PSSession -ComputerName 'DCNAMEHERE'}

#use the session you have setup...
Invoke-Command -Session $sessionAD -ScriptBlock {
    # get input from function calling remote session
    Param ($var1)

    Import-Module ActiveDirectory
    $thisUser = Get-ADUser -Identity $var1

} -ArgumentList $var1
0
footechCommented:
The issue really comes down to the use of Invoke-Command.  When you use that you're creating a new session or using a different one.  In that session, it has no idea what variables you've defined in your local session.  In your case, when you're trying to run
Set-ADComputer $Server -Description $Desc
it has no idea what either $Server or $Desc are.  You need to use the -ArgumentList parameter of Invoke-Command if you want to pass information between sessions, and the scriptblock has to be set to receive the info.  Although I haven't changed them, the variable names don't have to be the same between the remote and local sessions.
Invoke-Command -ComputerName DC -Credential $SvcAcctCreds -UseSSL -SessionOption $CertChk -ScriptBlock {Param ($Server,$Desc); Set-ADComputer $Server -Description $Desc} -ArgumentList $Server,$Desc

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bret RobinsonAuthor Commented:
Thank you both! Foo, worked perfectly I appreciate the help and the information regarding passing info into the session!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.