Forum Discussion
Run the same Cmdlets for 100 different tenant
Hi Guys,
Need your expertise for a power shell Script.
I have around 100 tenants for which i want to run a same command and want to get the output in a CSV file. And i have Admin username and password in CSV. So how can i connect the powershell to a different tenant every time to run a same command in a loop.
Have a sample script however for this we have to enter the credentials again and again.
#========================================================================================== #Run This section first to create the admin tokents #========================================================================================== $Desktop = [Environment]::GetFolderPath("Desktop") CD $Desktop MKDIR 365Tokens Get-Credential | Export-CliXml -Path $Desktop\365Tokens\"admin@abc.com.xml" #========================================================================================== #Run this section second to use the admin tokens to run commands on your selected tenants #========================================================================================== Import-Module MSOnline $files = Get-ChildItem $Desktop\365Tokens\ -File foreach ($file in $files) { $Credential = Import-Clixml -Path $Desktop\365Tokens\$file Connect-MsolService -Credential $Credential # run your commands here Get-MsolCompanyInformation | Export-Csv .\Companyinfo Get-PSSession | Remove-PSSession }
3 Replies
- Manidurai MohanamariappanIron Contributor
You can try this script
$path= "C:\csv\users.csv"
$csvs = Import-Csv $Path
$Allcompanyinfo = @()
foreach($values in $csvs )
{
$username=$values.username
$password= $values.password
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
Connect-MsolService –Credential $cred
$companyifo = Get-MsolCompanyInformation
$Allcompanyinfo+= $companyifo
$companyifo =$null
}
$Allcompanyinfo | Export-csv "C:\csv\companyinfo.csv" -NoTypeInformation Hi,
Any approach will require:
1) Initially manually entering the credentials for each tenant, and,
2) Creating a separate Msol connection to the tenantWhich is what you are doing. Once those credentials are entered and stored, you won't have to manually enter them again obviously
Storing them on disk with Export-CliXml is more secure than plain text passwords but not super secure if those are Global Administrator credentials (it uses the private key machine to encrypt).
Unlike Exchange Online, and SfBO, Connect-MsolService isn't creating a local PS session, so you don't need the Remove-PSSession.
Curtis
The trouble with MSOL cmdlets though is that you don't have a "disconnect" cmdlet, thus if there is something wrong with the new session you are creating, the tenant context will not get switched and you will run the same cmdlets against the previously connected one. Thus I'd recommend using the Azure AD module instead, if possible.
And I fully agree about the storing credentials remark Curtis made, be very careful with that.