Powershell - Unexpected token 'in' in expression or statement error

zylker 0 Reputation points
2023-07-17T14:17:43.1333333+00:00

Getting Unexpected token 'in' in expression or statement for the below powershell script

 param([string] $principalId)\r\n          Connect-AzureAD\r\n          $tenantId=(get-aztenant).Id\r\n          foreach ($subscription in Get-AzSubscription)\r\n          {\r\n              New-AzRoleAssignment -ObjectId $principalId -RoleDefinitionName \"Reader\" -Scope \"\/subscriptions\/$subscription\"\r\n          }\r\n          Write-Output  $tenantId\r\n          $DeploymentScriptOutputs = @{}\r\n          $DeploymentScriptOutputs['tenantId'] = $tenantId
Windows for business Windows Server User experience PowerShell
Microsoft Security Microsoft Entra Microsoft Entra ID
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-07-17T15:31:13.2466667+00:00

    Try it this way: foreach ($subscription in (Get-AzSubscription)) {

    0 comments No comments

  2. JamesTran-MSFT 36,906 Reputation points Microsoft Employee Moderator
    2023-07-24T20:05:59.3933333+00:00

    @zylker

    Thank you for your post and I apologize for the delayed response!

    I understand that you're running into an Unexpected token 'in'... error when running your PowerShell script. To hopefully resolve your issue or help point you in the right direction, I'll share my findings below.


    Findings:

    This PowerShell script you're using is used to assign the Reader role to a principal ID in all Azure subscriptions. The error Unexpected token 'in' in expression or statement usually occurs when there's a syntax error in the script.

    param([string] $principalId)
    Connect-AzureAD       
    $tenantId=(get-aztenant).Id        
    foreach ($subscription in Get-AzSubscription)    
    {           
    New-AzRoleAssignment -ObjectId $principalId -RoleDefinitionName \"Reader\" -Scope \"\/subscriptions\/$subscription\"         
    }        
    Write-Output  $tenantId       
    $DeploymentScriptOutputs = @{}          
    $DeploymentScriptOutputs['tenantId'] = $tenantId
    

    When looking at the PS script that you shared, this looks to have been caused by the backslashes in your scope parameter, to resolve this you can try replacing the backslashes with forward slashes, for example:

    param([string] $principalId)
    Connect-AzureAD
    $tenantId=(get-aztenant).Id
    foreach ($subscription in Get-AzSubscription)
    {
        New-AzRoleAssignment -ObjectId $principalId -RoleDefinitionName "Reader" -Scope "/subscriptions/$($subscription.Id)"
    }
    Write-Output $tenantId
    $DeploymentScriptOutputs = @{}
    $DeploymentScriptOutputs['tenantId'] = $tenantId
    

    I hope this helps!

    If you have any other questions, please let me know. Thank you for your time and patience throughout this issue.


    If the information helped address your question, please Accept the answer. This will help us and also improve searchability for others in the community who might be researching similar information.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.