Mark
Mark

Reputation: 1537

Why does Powershell script fail when using variables in Orchestrator 2012?

I'm running System Center 2012 Orchestrator Runbook Designer locally on my computer. I'm trying to run a Powershell script that simply looks to see if a specific AD account already exists.

This script works (i.e., User Exists):

$User = powershell { 
     import-module activedirectory
     Get-ADUser -Filter "samaccountname -eq 'username'" -properties samaccountname | select samaccountname
}

if ($User) { $Trace += "User exists" }
else {$Trace += "User does not exist" }

But when I try to put in a variable, it fails (i.e., User does not exist).

    $TestUser = 'username'
$User = powershell { 
     import-module activedirectory
     Get-ADUser -Filter "samaccountname -eq '$TestUser'" -properties samaccountname | select samaccountname
}

if ($User) { $Trace += "User exists" }
else {$Trace += "User does not exist" }

Upvotes: 0

Views: 270

Answers (1)

kpogue
kpogue

Reputation: 700

You're starting a new instance of powershell in your call. In that scope $TestUser does not exist. Unless there is some compelling reason to do so, call Get-ADUser directly without invoking a new instance of powershell as shown below and it should work.

import-module activedirectory
$TestUser = 'username'
$User = Get-ADUser -Filter "samaccountname -eq '$TestUser'" -properties samaccountname |select samaccountname

if ($User) { $Trace += "User exists" }
else {$Trace += "User does not exist" }

Upvotes: 1

Related Questions