none
PowerShell and CSV RRS feed

  • Question

  • Hello everyone :), 

    I am building a powershell script that make my job more optimal.

    My powershell script connect to MSol service. But i am trying to use a csv file wich contains name, username, and password of Office365 tenant of the client.

    csv file have 3 headers : Nom,username,mdpasse and when i type the name (nom) i got the good informations.

    In first, the script ask for the name of client, and then search in the file the good row, wich contains informations about the client (username and password). Now the script works, he find informations about the client, but it format the result as table or with @and brackets.

    So i did some modifications in the script but i know this is not the good way to do because he let some space in the result.

    My script 

    $choixDuClient = Read-Host "Veuillez ecrire en minuscules le nom du client " 
    $importLogins = import-csv -Path C:\loginsOffice365.csv -Delimiter ";" 
    $nomUtilisateur = $importLogins | Where-Object Nom -eq $choixDuClient | Select-Object username | % { ($_ | Out-String).Trim() -replace '@\{|\}' }  | % { ($_ | Out-String).Trim() -replace 'username' } | % { ($_ | Out-String).Trim() -replace '--------' } 
    $motdepasseUtilisateur = $importLogins | Where-Object Nom -eq $choixDuClient | Select-Object mdpasse | % { ($_ | Out-String).Trim() -replace '@\{|\}' } | % { ($_ | Out-String).Trim() -replace 'mdpasse' } | % { ($_ | Out-String).Trim() -replace '--------' }
    
    $tenant = 'mytenant'
    $userName = $nomUtilisateur
    $password = $motdepasseUtilisateur
    $pass = ConvertTo-SecureString -AsPlainText $Password -Force
    $SecureString = $pass
    $MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecureString
    
    function ConnexionOffice365{
                                    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $MySecureCreds -Authentication Basic -AllowRedirection
                                    if ($Session -ne $null)
                                    { 
                                         Write-Host 'Connexion au tenant : ' -NoNewline; Write-Host -ForegroundColor Yellow $tenant -NoNewline; Write-Host "`r`n"
                                         Write-Host 'Etat de la connexion : ' -NoNewline; Write-Host -ForegroundColor green 'Ok'
    
                                         $msonline = Connect-MsolService -Credential $MySecureCreds
                                         if ($msonline -ne 'Authentication Error: Unexpected authentication failure')
                                         {
                                            Write-Host 'Services Microsoft Online :' -NoNewline; Write-Host -ForegroundColor Green " Activés`r`n"
                                         }
     
                                    }
                                    else
                                    { 
                                        Write-Host -ForegroundColor Red "Echec de la connexion"
                                    }
    
                                   
    }
    function licencesSurTenant {
                                    Get-MsolAccountSku
                               }
    
    function Show-Menu {
                            $procruser = Write-Host -ForegroundColor Yellow '1.' -NoNewline ; Write-Host 'Créer un utilisateur'
                            $propchmdp = Write-Host -ForegroundColor Yellow '2.' -NoNewline; Write-Host 'Changer Mot de Passe'
                            $propchlic = Write-Host -ForegroundColor Yellow '3.' -NoNewline; Write-Host 'Changer de Licence'
                            $deconnexion = Write-Host -ForegroundColor Yellow '4.' -NoNewline; Write-Host "Deconnexion `r`n"
                            $choice = Read-Host
                            Switch ($choice)
                            {
                                "1"
                                {
                                    $nom = Read-Host "Veuillez entrez le nom "
                                    $prenom = Read-Host "Veuillez entrez le prenom "
                                    $affichage = Read-Host "Veuillez entrez le nom d'affichage "
                                    $mail = Read-Host "Veuillez entre le mail à créer "
                                    $motdepasse = Read-Host "Veuillez entrer le mot de passe "
                                    Get-MsolAccountSku | Format-Table
                                    $licence = Read-Host "Veuillez choisir la licence à assigner "
                                    New-MsolUser -DisplayName $affichage -FirstName $prenom -LastName $nom -UserPrincipalName $mail -UsageLocation "FR" -LicenseAssignment $licence -Password $motdepasse
                                    Show-Menu
                                }
    
                                "2"
                                {
                                   $randomPassword = Write-Host -ForegroundColor Yellow 'a.' -NoNewline; Write-Host 'Mot de passe aléatoire'
                                   $chosenPassword = Write-Host -ForegroundColor Yellow 'b.' -NoNewline; Write-Host 'Choisir le mot de passe'
               
                                       $choixmdp = Read-Host
                                       if ($choixmdp -eq 'a')
                                           {
                                           Get-MsolUser| Format-Table 
                                           $passwordMailChangeRandom = Read-Host "Merci d'entrer le mail "
                                           Set-MsolUserPassword -UserPrincipalName $passwordMailChangeRandom -ForceChangePassword
                                           }
                                       else
                                           {
                                           Get-MsolUser | Format-Table 
                                           $passwordMailChange = Read-Host "Merci d'entrer le mail "
                                           $newPassword = Read-Host "Entrer le nouveau mot de passe " 
                                           Set-MsolUserPassword -UserPrincipalName $passwordMailChange -NewPassword $newPassword
                                           }
                                           Show-Menu
                                }
    
                                "3"
                                {
                                $accLicense = Read-Host "Entrez le nom d'utilisateur du compte qui doit subir un chagement de licence "
                                Write-Host "Voici les licences sur le tenant: " ;
                                licencesSurTenant
                                Write-Host "Voici les licences dont dispose l'utilisateur " $accLicense ;
                                (Get-MsolUser -UserPrincipalName $accLicense).licenses.accountskuid
                                $ajoutLicence = Read-Host "Selectionnez la licence à ajouter "
                                $retraitLicence = Read-Host "Selectionner la licence à retirer "
                                Set-MsolUserLicense -UserPrincipalName $accLicense -AddLicenses {$tenant}:{$ajoutLicence} -RemoveLicenses {$tenant}:{$retraitLicence}
                                Show-Menu
                                }
    
                                "4"
                                {
                                    Remove-PSSession $Session
                                }
                            }
    
                                                }
    
    ConnexionOffice365 
    licencesSurTenant                         
    Show-Menu
    

    So when my script is trying to retrieve the good ID and password, it works well the problem is (i think) the format.

    Sorry for my English.

    Can you help me please? 

    Thank's a lot.

    Best Regards.

    Yoann

    Friday, January 3, 2020 11:10 AM

Answers

  • I got your query.

    As you are using select statement after importing the csv file. You will always get this type of format.

    You need to use 

    $nomUtilisateur = $($importLogins | Where-Object Nom -eq $choixDuClient).username

    • Proposed as answer by asharma5 Sunday, January 5, 2020 1:51 PM
    • Marked as answer by yoannjacobs Sunday, January 5, 2020 3:17 PM
    Friday, January 3, 2020 12:01 PM

All replies

  • HI Yoannjacobs,

    what are you getting in the output, and how you want.

    Friday, January 3, 2020 11:22 AM
  • Hi,

    i am getting the good information (in this case the mail and the password of the client) but if i dont put in code the command "replace ..", it format me the result as table. If i delete the replace commands, it give me : 

    @{mail.com}

    but the problem with this format is not good, because the credentials receive "@{mail.com}  instead of "mail.com".

    Friday, January 3, 2020 11:35 AM
  • I got your query.

    As you are using select statement after importing the csv file. You will always get this type of format.

    You need to use 

    $nomUtilisateur = $($importLogins | Where-Object Nom -eq $choixDuClient).username

    • Proposed as answer by asharma5 Sunday, January 5, 2020 1:51 PM
    • Marked as answer by yoannjacobs Sunday, January 5, 2020 3:17 PM
    Friday, January 3, 2020 12:01 PM
  • A big thank's you. You were helpful for me. Yes it works like that ! :)
    Sunday, January 5, 2020 10:51 AM
  • Please mark that as a answer if it work for you.

    Thank You

    Sunday, January 5, 2020 1:53 PM