PowerShell 中函数调用函数的问题

Problem with function calling function in PowerShell

我有两个功能:

  1. CreateComputer-Group
  2. CreateCoomputer-Role

第一个函数执行后,第一个函数中的 GroupCreated 将在第二个函数中用于创建计算机角色。我怎样才能确保第一个函数执行完成,然后只执行第二个函数。

代码如下:

$global:usergroup = "TACACS Admins"
$global:computerrole = "123413-NPARC"
$global:zone = "AWS"
$username = "<>"
$password = "<>"
[String[]] $global:HostServers = 'smp001-01','sl1ps01-13-8'
#[String[]] $global:HostServers = $hostServer.Replace("'","").Split(",")

Import-Module ActiveDirectory
Import-Module Centrify.DirectControl.PowerShell

$Password = ConvertTo-SecureString $password -AsPlainText -Force
$global:Cred = New-Object System.Management.Automation.PSCredential($username, $Password)
Set-CdmCredential -Domain test.com -Credential $Cred

function CreateComputer-Group {
    Param($Cred,$zone,$computerrole)
    try {
        New-ADGroup -Path "ou=Role Groups-Computer,ou=Centrify,ou=Operations,dc=qateradatacloud,dc=com" -Name $computerrole -GroupScope Global -GroupCategory Security -Credential $Cred -ErrorAction Stop
    } catch {
        $ErrorMessage = $_.Exception
        return $ErrorMessage
        break
    }
}

function create-computerRole {
    try {
    $ADGroupName = Get-ADGroup -Identity $computerrole
        Write-Host "********** Get Command Outout *********"
        Write-Host $ADGroupName
        Write-Host $CustomerZone
        Write-Host $computerrole

        $global:Hellow = New-CdmComputerRole -Zone $CustomerZone -Name $computerrole -Group $ADGroupName
        Write-Host $Hellow
    } catch {
        $ErrorMessage = $_.Exception
        return $ErrorMessage
    }
}

不确定为什么 New-CdmComputerRole 命令在服务器上没有显示此类对象。

这是输出:

   ********** Get Command Outout *********
CN=123413-NPARC,OU=Role Groups-Computer,OU=Centrify,OU=Operations,DC=qateradatacloud,DC=com
CN=AWS,CN=qateradatacloud,CN=Zones,OU=Centrify,OU=Operations,DC=qateradatacloud,DC=com
123413-NPARC

System.DirectoryServices.DirectoryServicesCOMException (0x80072030): There is no such object on the server.

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.RefreshCache(String[] propertyNames)
at Centrify.DirectControl.Util.AD.DirectoryEntryEx.RefreshCache(DirectoryEntry de, String[] propertyNames)
at Centrify.DirectControl.Util.ActiveDirectory.Session.GetDirectoryEntryCheckOffline(String domainDcIpNetbios, String dn, String[] propertiesToLoad)
at Centrify.DirectControl.Util.ActiveDirectory.Session.GetDirectoryEntry(String domainDcIpNetbios, String dn, String[] propertiesToLoad)
at Centrify.DirectControl.Util.ActiveDirectory.Session.GetDirectoryEntry(String dn, String[] propertiesToLoad)
at Centrify.DirectControl.PowerShell.Types.CdmAdPrincipal.BindDirectoryEntry(Session session)
at Centrify.DirectControl.PowerShell.Types.CdmAdObject.Bind(Session session)
at Centrify.DirectControl.PowerShell.Commands.NewCdmComputerRole.InnerBeginProcessing()
at Centrify.DirectControl.PowerShell.CmdletBase.BeginProcessing()
at System.Management.Automation.Cmdlet.DoBeginProcessing()
at System.Management.Automation.CommandProcessorBase.DoBegin()

最后我将整个脚本分成了两个脚本。一个用于创建 AG-Group,另一个用于所有 Centrify 命令