none
Office 365 powershell and TenantID RRS feed

  • Question

  • Hello guys, 

    I need an advice with a script I´m currently developing to make my work easier. 

    I work on script where you only enter first part of primary domain of a tenant, this gets saved to variable, and then used to get tenant ID. Problem is that it gets all tenant ID´s and when I try to print the variable they show as empty. Can you help with that? 


    write-host "enter primary domain" -ForegroundColor Yellow
    
    $tendom = Read-host 'primary domain of customer?:'
    $tid = (get-msolpartnercontract -domain $tendom.onmicrosoft.com).tenantId.guid
    write-host +++++++++++
    write-host setting  Tenant ID   -foregroundcolor green
    write-host +++++++++++
    write-host -ForegroundColor white "domain is $tendom.onmicrosoft.com"
    write-host -ForegroundColor Yellow "you can run scripts using -Tenantid $\tid\"
    
    write-host -ForegroundColor white "Tenantid is $tid"

    After this script block you jump out of the script but the variables should stay there don´t they? 

    Also If I only run this short part it correctly uses all the variables, and prints them, but then when I want to use them or use write-output to print them to screen they are both empty. 

    $tendom = Read-host 'primary domain zákazníka?:' 
    $tid = (get-msolpartnercontract -DomainName "$tendom.onmicrosoft.com").tenantId.guid   
        
    write-host  -ForegroundColor white  	"domain is $tendom.onmicrosoft.com"    
    write-host  -ForegroundColor white  	"Tenantid je $tid" 

    Any clues why the values in variables ale empty after the script runs? 

    Thanks Jan

    Jan Cizek

    Friday, February 5, 2016 11:07 AM

Answers

  • Hi Jan,

    First of all, ideally you want to call the cmdlet like this:

    get-msolpartnercontract -DomainName "$($tendom).onmicrosoft.com"

    The first script fails, because it doesn't pass the string you want, but rather looks at the content of the $tendom variable, looking for a property named "onmicrosoft" and there, it tries to look for a property named "com". Since a string obviously lacks those properties, it uses $null instead.

    Instead you need to wrap the domain name in a string. Furthermore, to ensure uniform behavior across powershell and OS versions, I recommend in most situations to wrap any variables used within a string into a $(....), as this prevents it from erroneously interpreting parts of the natural string as code.

    Variable is empty after script finishes
    When you call a script file from another script or console, it executes in its own scope (a kind of "sandbox", simply put. It's a big topic, try running Get-Help about_scopes if you want to know more). If you want to keep the variables of the script after it finishes, you need to run it in your own scope (or without a sandbox). To do this, simply dotsource the script:

    . .\Examplescript.ps1

    Yepp, simply add a dot (".") without quotes, but with a whitespace behind it, at the beginning of the line.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Jan Cizek Monday, February 8, 2016 8:31 AM
    Friday, February 5, 2016 11:55 AM
  • The following works fine for me:

    get-msolpartnercontract -DomainName $tendom.onmicrosoft.com

    Since you are only retrieving an ID I would do it like this:

    function Get-TenentID{
    
    	write-host "enter primary domain" -ForegroundColor Yellow
    
    	$tendom = Read-host 'primary domain of customer?:'
    	$tid = (get-msolpartnercontract -domain $tendom.onmicrosoft.com).tenantId.guid
    	write-host +++++++++++
    	write-host setting  Tenant ID -foregroundcolor green
    	write-host +++++++++++
    	write-host -ForegroundColor white "domain is $tendom.onmicrosoft.com"
    	write-host -ForegroundColor Yellow "you can run scripts using -Tenantid $\tid\"
    
    	write-host -ForegroundColor white "Tenantid is $tid"
    	$tid
    }
    
    $tid=Get-TenentID

    This will persist the results in the current scope and allow you to grab and save multiple ids.

    $tid1=Get-TenentID

    $tid2=Get-TenentID

    Which might be useful.


    \_(ツ)_/

    • Marked as answer by Jan Cizek Monday, February 8, 2016 8:31 AM
    Friday, February 5, 2016 2:27 PM

All replies

  • Hi Jan,

    First of all, ideally you want to call the cmdlet like this:

    get-msolpartnercontract -DomainName "$($tendom).onmicrosoft.com"

    The first script fails, because it doesn't pass the string you want, but rather looks at the content of the $tendom variable, looking for a property named "onmicrosoft" and there, it tries to look for a property named "com". Since a string obviously lacks those properties, it uses $null instead.

    Instead you need to wrap the domain name in a string. Furthermore, to ensure uniform behavior across powershell and OS versions, I recommend in most situations to wrap any variables used within a string into a $(....), as this prevents it from erroneously interpreting parts of the natural string as code.

    Variable is empty after script finishes
    When you call a script file from another script or console, it executes in its own scope (a kind of "sandbox", simply put. It's a big topic, try running Get-Help about_scopes if you want to know more). If you want to keep the variables of the script after it finishes, you need to run it in your own scope (or without a sandbox). To do this, simply dotsource the script:

    . .\Examplescript.ps1

    Yepp, simply add a dot (".") without quotes, but with a whitespace behind it, at the beginning of the line.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Jan Cizek Monday, February 8, 2016 8:31 AM
    Friday, February 5, 2016 11:55 AM
  • The following works fine for me:

    get-msolpartnercontract -DomainName $tendom.onmicrosoft.com

    Since you are only retrieving an ID I would do it like this:

    function Get-TenentID{
    
    	write-host "enter primary domain" -ForegroundColor Yellow
    
    	$tendom = Read-host 'primary domain of customer?:'
    	$tid = (get-msolpartnercontract -domain $tendom.onmicrosoft.com).tenantId.guid
    	write-host +++++++++++
    	write-host setting  Tenant ID -foregroundcolor green
    	write-host +++++++++++
    	write-host -ForegroundColor white "domain is $tendom.onmicrosoft.com"
    	write-host -ForegroundColor Yellow "you can run scripts using -Tenantid $\tid\"
    
    	write-host -ForegroundColor white "Tenantid is $tid"
    	$tid
    }
    
    $tid=Get-TenentID

    This will persist the results in the current scope and allow you to grab and save multiple ids.

    $tid1=Get-TenentID

    $tid2=Get-TenentID

    Which might be useful.


    \_(ツ)_/

    • Marked as answer by Jan Cizek Monday, February 8, 2016 8:31 AM
    Friday, February 5, 2016 2:27 PM
  • The script itself worked fine, problem - at least what I found - was with scope. The ID and domain name were kept as sctipt local scope and therefore when script ended and I jumped out the ID were not there. I fieed it with usin them as $global:tendom and $global:tenid then it all works fine but I´ll definitely play with the function. THanks a lot. 
    Monday, February 8, 2016 8:59 AM
  • The script itself worked fine, problem - at least what I found - was with scope. The ID and domain name were kept as sctipt local scope and therefore when script ended and I jumped out the ID were not there. I fieed it with usin them as $global:tendom and $global:tenid then it all works fine but I´ll definitely play with the function. THanks a lot. 

    Which is why the function works and solves the problem without resorting to dangerous "global" variables.

    \_(ツ)_/

    Monday, February 8, 2016 9:05 AM