简体   繁体   中英

Azure with PowerShell: empty string for Identity

I have a PowerShell script that logs into Azure and assign a group call membership to a user based on the input email address the user gave:

Function SetUser
{
    $script:user = Read-Host -Prompt "Enter the user's email address"    

    Write-Host "You set the email address as '$user'." -ForegroundColor yellow
}

Function Assigndelegate($user)
{
    $job = Start-Job -ScriptBlock {
    param($user)
    New-CsUserCallingDelegate -Identity $identity -Delegate $user -MakeCalls $true -ReceiveCalls $true -ManageSettings $false 
} -ArgumentList $user
    while ($job.State -eq 'Running') {
        Write-Host "Connecting to Azure..."
        Wait-Job -Job $job
    }
    $result = Receive-Job $job

    (Get-CsUserCallingSettings -Identity $user).Delegators | ft -Property Id, MakeCalls, ReceiveCalls, ManageSettings
}


Connect-MicrosoftTeams

$script:identity = "user@contoso.com"
SetUser
Assigndelegate $user

This gives me the following error:

Cannot bind argument to parameter 'Identity' because it is an empty string.
    + CategoryInfo          : InvalidData: (:) [New-CsUserCallingDelegate], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,New-CsUserCallingDelegate
    + PSComputerName        : localhost

What I understand is that it cannot read the variable $identity, but I don't see the reason why: I tried also with the "$global:" prefix to see it works, even though that would not be ideal as this changes later, got the same error. If I directly add the value of this variable, it works.

If I define Identity in the same name in the 'param' and 'ArgumentList' the error changes to:

Cannot bind argument to parameter 'Identity' because it is an empty string.
    + CategoryInfo          : InvalidData: (:) [New-CsUserCallingDelegate], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,New-CsUserCallingDelegate
    + PSComputerName        : localhost
 
Get-CsUserCallingSettings : Cannot process argument transformation on parameter 'Identity'. Cannot convert value to type System.String.
At line:36 char:42
+     (Get-CsUserCallingSettings -Identity $user).Delegators | ft -Prop ...
+                                          ~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-CsUserCallingSettings], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-CsUserCallingSettings

UPDATE:

After Updating the argument list, I still get the error:

#Set the user
Function SetUser
{
    $script:user = Read-Host -Prompt "Enter the user's email address"    

    Write-Host "You set the email address as '$user'." -ForegroundColor yellow
}

Function Assigndelegate($user, $identity)
{
    $job = Start-Job -ScriptBlock {
    param($user, $identity)
    New-CsUserCallingDelegate -Identity $identity -Delegate $user -MakeCalls $true -ReceiveCalls $true -ManageSettings $false 
} -ArgumentList @($user, $identity)


    while ($job.State -eq 'Running') {
        Write-Host "Connecting to Azure..."
        Wait-Job -Job $job
    }
    $result = Receive-Job $job

    #(Get-CsUserCallingSettings -Identity $user).Delegators | ft -Property Id, MakeCalls, ReceiveCalls, ManageSettings

}


Connect-MicrosoftTeams

$script:identity = "user@contoso.com"
SetUser
Assigndelegate $user, $identity

UPDATE II

Based on the idea recommended I simplified the script:

Function SetUser
{
    $script:user = Read-Host -Prompt "Enter the user's email address"    

    Write-Host "You set the email address as '$user'." -ForegroundColor yellow
}

Function Assigndelegate($user, $identity)
{
 Write-Output $identity

}

$script:identity = "user@contoso.com"
SetUser
Assigndelegate $user, $identity

It gives no results apart from:

"You set the email address as '$user'." -ForegroundColor yellow

The Start-Job cmdlet starts a process in a separate session, so none of the scopes (local, script or global) are visible to the script block it executes. The only way around this is to add identity as a parameter in your script block and pass it explicitly, ie:

Function SetUser
{
    $script:user = Read-Host -Prompt "Enter the user's email address"    

    Write-Host "You set the email address as '$user'." -ForegroundColor yellow
}

Function Assigndelegate($user, $identity)
{
    $job = Start-Job -ScriptBlock {
        param($user, $identity)
        New-CsUserCallingDelegate -Identity $identity -Delegate $user -MakeCalls $true -ReceiveCalls $true -ManageSettings $false
    } -ArgumentList @($user, $identity)

    while ($job.State -eq 'Running') {
        Write-Host "Connecting to Azure..."
        Wait-Job -Job $job
    }

    $result = Receive-Job $job

    (Get-CsUserCallingSettings -Identity $user).Delegators | ft -Property Id, MakeCalls, ReceiveCalls, ManageSettings
}

Connect-MicrosoftTeams
$script:identity = "user@contoso.com"
SetUser
Assigndelegate -user $user -identity $identity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM