代码之家  ›  专栏  ›  技术社区  ›  Mark

为什么Powershell脚本在Orchestrator 2012中使用变量时失败?

  •  0
  • Mark  · 技术社区  · 6 年前

    我正在计算机上本地运行System Center 2012 Orchestrator Runbook Designer。我正在尝试运行一个Powershell脚本,它只是查看特定的广告帐户是否已经存在。

    此脚本有效(即用户存在):

    $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" }
    

        $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" }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   kpogue    6 年前

    您正在呼叫中启动powershell的新实例。在该范围内$TestUser不存在。除非有令人信服的理由这样做,否则直接调用Get ADUser而不调用powershell的新实例,如下所示,它应该可以工作。

    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" }