REM do not remove this first line setlocal enableextensions enabledelayedexpansion cls @echo off REM ################## Information ##################### set Title=Office 365 set Subject=Administrating a office365 environment set Author=Mark de Bruin set Website=http://tools.fnetonline.nl/script/003 set Lasteditdate=15-04-2015 set ver=1.0 set update=2 REM ##################################################### REM ######################## settings ################### REM the settings bellow can be changed by the user in REM the settings menu REM the default window and text color REM default 0A Set usercolor=0A REM Color when script is started as administrator REM default 4E set admincolor=4E REM shall we save the administrator username set saveadminuser=yes REM ##################################################### title %title% %ver%.%update%: Starting Please wait... REM do not change this REM it is to prevent errors set newver=niks md c:\temp\ set cp=%0 set localadmin=no cd /d c:\temp\ rem check if user is local admin net session>NUL if %errorlevel% == 0 set localadmin=yes rem change color when user is localadmin set defcolor=%usercolor% if %localadmin% == yes ( set defcolor=%admincolor% ) else ( cls color 0c Echo WARNING: many functions of this script require elevated rights Echo. Set /p DoAdminRun=Do you want to start the script as administrator?[Y/N] if /i "!DoAdminRun!" == "y" ( cls echo starting script as administrator echo please wait ...... powershell.exe Start-Process -verb runas %0 exit ) ) color %defcolor% cls echo checking version powershell -Command "(New-Object Net.WebClient).DownloadFile('%website%/RSV.txt', 'C:\temp\RSV.bat')" cls REM get the language of the system for /f "tokens=3" %%a in ('reg query "hklm\system\currentcontrolset\control\nls\language" /v Installlanguage') do set language=%%a REM Prepair the script for the color function setlocal EnableDelayedExpansion for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do ( set "DEL=%%a" ) X set /p ".=." attrib +h X REM get the name of the volume we currently using for /f "tokens=7" %%t in ('vol') do @set volid=%%t if "%volid%" == "" for /f "tokens=6" %%t in ('vol') do @set volid=%%t REM if there are saved settings load them now call :getsettings REM check for update if /I NOT exist c:\temp\RSV.bat goto ver-next call c:\temp\RSV.bat if /I NOT %newver% == %ver% ( call :color 0c "This version is not up to date" echo. goto scriptupdate ) :ver-next REM convert the userdnsdomain string to lowercase SET String=%USERDNSDOMAIN% CALL :LoCase String set dns=%string% :menu REM the main menu title %title% %ver%.%update%: Menu color %defcolor% cls echo ################ Echo # Script # Echo # ROLOUT # echo # Version %ver% # Echo ################ echo # echo # echo # 1 echo # 2 add alias to cloud mailbox echo # 3 echo # 4 echo # 5 echo # 6 echo # 7 echo # 8 echo # 9 Federated services echo # 0 Exit echo # S settings echo # echo # echo # ? About echo # set /p menu=Choice: call :getsettings color %defcolor% Call :log %volid%: option %menu% :menu-next REM processing the mneu cls if /I %menu% == 2 goto add-alias if /I %menu% == 9 goto advmenu if /I %menu% == 0 goto exit if /I %menu% == u goto scriptupdate if /I %menu% == cmd goto cmd if /I %menu% == s goto settings if /I %menu% == ? goto about REM if none of the above options is used we think the user made a mistake and return to the menu color 0c echo This is not a valid choice pause goto Menu :advmenu title %title% %ver%.%update%: Advanced menu REM some less used options color %defcolor% cls echo ################ Echo # Script # Echo # Federated # echo # Version %ver% # Echo ################ echo # echo # echo # 1 Reset users aliasses echo # 2 Add new user echo # 3 echo # 4 echo # 5 echo # 6 echo # 7 echo # 8 echo # 9 echo # 0 Back to menu echo # echo # echo # echo # echo # echo # echo # set /p advmenu=Choice?: cls Call :log %volid%: option advmenu %advmenu% REM processing the mneu if /I %advmenu% == 1 goto Add-proxy if /I %advmenu% == 2 goto add-fed-user if /I %advmenu% == 0 goto menu REM if none of the above options is used we think the user made a mistake and return to the menu color 0c echo This is not a valid choice pause goto advmenu :settings cls REM ############################################ Settings ############################################ REM some settings for the script REM give the user the abbility to save the username of the admin user account (NOT THE PASSWORD) title %title% %ver%.%update%: Settings echo ################ Echo # settings # echo # Version %ver% # Echo ################ echo # echo # 1. adminuser: %adminuser% echo # 2. auto use saved admin user: %saveadminuser% echo # 3. Default color: %usercolor% echo # 4. Administrator color: %admincolor% echo # 5. Set default aliasses for new users echo # 6. Default ou for new users: %NewUserOu% echo # 7. Current license group: %licgroup% echo # 0. Back set /p settingmenu=Choice: cls if /I %settingmenu% == 1 goto set-adminuser if /I %settingmenu% == 2 goto saveadminuser if /I %settingmenu% == 3 goto usercolor if /I %settingmenu% == 4 goto admincolor if /I %settingmenu% == 5 Goto Aliassenlist if /I %settingmenu% == 6 goto set-NewUserOu if /I %settingmenu% == 7 goto set-licgroup if /I %settingmenu% == 0 goto menu color 0c echo This is not a valid choice pause goto setting :set-adminuser set /p adminuser=Adminuser: call :setsettings goto settings :saveadminuser if "%saveadminuser%" == "yes" ( set saveadminuser=no ) else ( set saveadminuser=yes ) call :setsettings goto settings :usercolor echo Please enter 2 hex numbers echo The first is the background echo The second is the forground echo default is 0A echo 0 = Zwart 8 = Donkergrijs echo 1 = Blauw 9 = Pastelblauw echo 2 = Groen A = Limoengroen echo 3 = Groenblauw B = Lichtblauw echo 4 = Rood C = Lichtrood echo 5 = Paars D = Lichtpaars echo 6 = Geel E = Lichtgeel echo 7 = Grijs F = Wit set /p usercolor=color?: call :setsettings goto settings :admincolor echo Please enter 2 hex numbers echo The first is the background echo The second is the forground echo default is 4E echo 0 = Zwart 8 = Donkergrijs echo 1 = Blauw 9 = Pastelblauw echo 2 = Groen A = Limoengroen echo 3 = Groenblauw B = Lichtblauw echo 4 = Rood C = Lichtrood echo 5 = Paars D = Lichtpaars echo 6 = Geel E = Lichtgeel echo 7 = Grijs F = Wit set /p admincolor=color?: call :setsettings goto settings :set-NewUserOu cls REM Set a new OU location echo ############################################################ echo # Set the default location for new user accounts # echo # use the distinguishedName format like: # echo # "OU=Users,OU=Global,OU=Location,DC=fnetonline,DC=local" # echo # make sure you use quotes around the distinguishedName # echo ############################################################ set /p NewUserOu=distinguishedName?: call :checkOU %NewUserOu% if NOT %OUExist% == 0 echo OU does not exist && pause && goto set-NewUserOu Call :setsettings goto settings :set-licgroup echo ######################################################################## echo # Set the default license for new users # echo # use the distinguishedName format like: # echo # CN=O365-lic,OU=Groups,OU=Global,OU=Location,DC=fnetonline,DC=local" # echo # make sure you use quotes around the distinguishedName # echo ######################################################################## set /p licgroup=Distinguished name of the license group?: Call :setsettings goto settings :about cls title %title% %ver%.%update%: About if NOT "%update%" == "" set isupdate=Update echo ################## About ##################### echo # %title% echo # Version %ver% %isupdate% %update% echo # This script is last changed on %lasteditdate% echo # By %author% echo # This script is used for: %subject% echo # For more information visit the link bellow echo # %website% echo ############################################## pause>Nul Goto menu goto functions-next REM ############################################ Functions start ############################################### REM the functions part is only used when the script CALL a function :Color REM color part REM this part makes it posible to use multiple color parts in one screen REM usage REM call :color 0a "my text" REM to end a line use echo. REM EXAMPLE: call :color 0a "first part is this line is green" REM EXAMPLE: call :color 0c "Second part of this line is red" REM EXAMPLE: echo. to give a hard reteurn setlocal EnableDelayedExpansion set "param=^%~2" ! set "param=!param:"=\"!" findstr /p /A:%1 "." "!param!\..\X" nul NUL if %errorlevel% == 0 ( net use \\%victem: =% if %errorlevel% == 0 ( set vicstat=online ) else ( set vicstat=offline ) ) else ( set vicstat=offline ) Goto :EoF REM if there are settings availible use them :getsettings REM set drive to drive letter (e.g. d:\ ) if "%cp:~2,1%" == ":" set drive=%cp:~1,3% if "%cp:~1,1%" == ":" set drive=%cp:~,3% if "%drive%" == "" set /p drive=please write down your drive letter (e.g. d:\): if /i "%drive%" == "c:\" set drive=c:\temp\ if exist %drive%script-settings.bat ( call %drive%script-settings.bat ) else ( call :color 0c "no settings found" echo. call :setsettings call :color 0c "default settings loaded" echo. ) goto :EoF REM after changing the settings write them to the same location as the script :setsettings del %drive%script-settings.bat echo REM this file is part of the %title% script > %drive%script-settings.bat echo set adminuser=%adminuser: =%>> %drive%script-settings.bat echo set saveadminuser=%saveadminuser: =%>> %drive%script-settings.bat echo set admincolor=%admincolor: =%>> %drive%script-settings.bat echo set usercolor=%usercolor: =%>> %drive%script-settings.bat echo set NewUserOu=%NewUserOu%>> %drive%script-settings.bat echo set licgroup=%licgroup%>>%drive%script-settings.bat attrib +h %drive%script-settings.bat set defcolor=%usercolor% if %localadmin% == yes set defcolor=%admincolor% goto :EoF REM when a update is availible update te script :scriptupdate title %title% %ver%.%update%: Updating Please wait... Echo downloading update please wait... powershell -Command "(New-Object Net.WebClient).DownloadFile('%website%/script.txt', 'C:\temp\script.txt')" cls if exist c:\temp\script.txt goto scriptupdate2 color 0c echo sorry the source is not availible at this moment echo please try again later pause goto menu :scriptupdate2 echo the script wil be copied to %0 echo pres control+c to cancel echo ##################### echo # what is new? echo # %changelog1% echo # %changelog2% echo # %changelog3% echo ##################### powershell.exe sleep 3 for /f "tokens=7" %%t in ('vol') do @set volid=%%t call :log %volid%: update script %volid% from %ver%.%update% to %newver% copy c:\temp\script.txt %0 && start "starting" %0 && del c:\temp\script.txt && exit REM if thhe above script fails go back to menu goto menu REM for test :cmd set /p cmd=Command?: %cmd% pause goto menu :prep-MSOL REM Prepair the system to connect to MS online services call :color 0c "Answer the question bellow with Y" echo. powershell get-executionpolicy|find "RemoteSigned">NUL if NOT %errorlevel% == 0 powershell "Set-ExecutionPolicy RemoteSigned" sc query winrm|find ": 4">NUL if NOT %errorlevel% == 0 powershell "Start-service winrm" cls goto :EoF :installdsquery Echo downloading DSquery please wait.... powershell -Command "(New-Object Net.WebClient).DownloadFile('http://tools.fnetonline.nl/tool/004/dsquery.txt', 'C:\temp\dsquery.cab')" expand c:\temp\dsquery.cab -F:* %windir%\system32\ goto :EoF :checkOU dsquery * "%*" >Nul SET OUExist=%errorlevel% goto :EoF REM ############################################ Functions end ############################################### :functions-next :Aliassenlist cls echo ############################################################################### echo # echo # please make a list of all aliasses for a default user echo # availible variable are echo # Echo # ################################################### call :color %defcolor% "# # EXAMPLE:" call :color 04 " %%%%" call :color 04 "Fname: =." call :color 04 "%%%%" call :color 04 "-" call :color 04 "%%%%" call :color 04 "Sname: =." call :color 04 "%%%%" call :color 04 "@domainname.com" call :color %defcolor% " #" echo. call :color %defcolor% "# # RESULT: " call :color 04 " Henk.de.Vries@domain.com" call :color %defcolor% " #" echo. Echo # ################################################### echo # REM set the %%%% signes on a difrent line to prevent processing the variables REM 4x % is needed for 1 % sign to show Echo # Firstname call :color %defcolor% "# " call :color 04 "%%%%" call :color 04 "Fname: =." call :color 04 "%%%% " call :color %defcolor% "First name with spaces replaced by dot." echo. call :color %defcolor% "# " call :color 04 "%%%%" call :color 04 "Fname: =" call :color 04 "%%%% " call :color %defcolor% " First name with spaces removed." echo. call :color %defcolor% "# " call :color 04 "%%%%" call :color 04 "Fname:~,1" call :color 04 "%%%% " call :color %defcolor% "First letter of the first name" echo. Echo # Surename call :color %defcolor% "# " call :color 04 "%%%%" call :color 04 "Sname: =." call :color 04 "%%%% " call :color %defcolor% "Surename with spaces replaced by dot." echo. call :color %defcolor% "# " call :color 04 "%%%%" call :color 04 "Sname: =" call :color 04 "%%%% " call :color %defcolor% " Surename with spaces removed." echo. call :color %defcolor% "# " call :color 04 "%%%%" call :color 04 "Sname:~,1" call :color 04 "%%%% " call :color %defcolor% "Fist letter of the surename." echo. echo # echo # echo # echo # echo # echo ############################################################################### powershell sleep 3 if /i exist == c:\temp\office365-aliasses.file copy /y c:\temp\office365-aliasses.file c:\temp\office365-aliasses.file.bac >NUL if /i not exist == c:\temp\office365-aliasses.file echo. 2>c:\temp\office365-aliasses.file notepad c:\temp\office365-aliasses.file goto menu :Add-proxy title %title% %ver%.%update%:add proxy to AD users. set /p victem=Username?: :Add-proxy2 REM Check if the file is available if /i not exist c:\temp\office365-aliasses.file ( Call :color 0c "Error:Aliasses file not found" echo. pause goto Aliassenlist ) REM get some information from the active directory dsquery user -samid %victem%>c:\temp\dsquery.tmp for /f "tokens=*" %%a in ('type c:\temp\dsquery.tmp') do set temp1=%%a dsquery user %temp1% |dsget user -fn |find /v /i "fn "|find /v /i "dsget">C:\temp\Fname.tmp for /f "tokens=*" %%f in ('type c:\temp\Fname.tmp') do set temp-Fname=%%f set Fname=%temp-Fname:~,-2% dsquery user %temp1% |dsget user -ln |find /v /i "ln "|find /v /i "dsget">C:\temp\Lname.tmp for /f "tokens=*" %%l in ('type c:\temp\Lname.tmp') do set temp-Sname=%%l set Sname=%temp-Sname:~,-2% REM clean up temp files del c:\temp\dsquery.tmp del C:\temp\Fname.tmp del C:\temp\Lname.tmp cls Echo Current target: "%Fname%" "%Sname%" (%victem%) Echo Generating file please wait.... SetLocal EnableExtensions disableDelayedExpansion echo SMTP:%victem%@%dns%>c:\temp\proxy.tmp echo SIP:%victem%@%dns%>>c:\temp\proxy.tmp for /f "tokens=*" %%b in (c:\temp\office365-aliasses.file) do call echo smtp:%%b>>c:\temp\proxy.tmp Echo Done Echo you can make changes to the list powershell sleep 1 notepad c:\temp\proxy.tmp set /p add-proxy-continue=Do you want to continue? [y/n]: if /i not "%add-proxy-continue%" == "y" goto menu cls Echo please wait a moment... REM replace everything in the proxylist with a . so we know whats in the list powershell set-aduser %victem% -replace @{'ProxyAddresses'=^('.'^)} REM we can remove everything since we know whats in the list powershell set-aduser %victem% -remove @{'ProxyAddresses'=^('.'^)} REM process the temp file and add it to the proxy list for /f "tokens=*" %%c in (c:\temp\proxy.tmp) do ( call echo %%c powershell set-aduser %victem% -add @{'ProxyAddresses'=@^('%%c'^)} ) REM clean up the temp file del /q c:\temp\proxy.tmp goto menu :add-alias title %title% %ver%.%update%:Add alias to cloud mailbox. Call :prep-MSOL cls REM add alias to shared mailbox cls set /p victem=primair mailadress of the targets mailbox?: echo #################### Manual mode ################################ echo # Please type all the aliasses you want to add to mailbox %victem% echo # when done enter a single dot ( . ) to end echo ################################################################# echo import-module msonline>c:\temp\alias.ps1 echo $LiveCred = Get-Credential>>c:\temp\alias.ps1 echo $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection>>c:\temp\alias.ps1 echo cls>>c:\temp\alias.ps1 echo $import = import-pssession $session>>c:\temp\alias.ps1 echo connect-msolservice -credential $livecred>>c:\temp\alias.ps1 echo cls>>c:\temp\alias.ps1 echo echo please wait>>c:\temp\alias.ps1 :add-alias-start set /p list=Alias?: cls if "%list%" == "." goto add-alias-next echo Set-Mailbox %victem% -EmailAddresses @{add='%list%'}>>c:\temp\alias.ps1 goto add-alias-start :add-alias-next powershell.exe c:\temp\alias.ps1 pause del /q c:\temp\alias.ps1 goto menu :add-fed-user REM check if the New user location is set if "%NewUserOu%" == "" goto set-NewUserOu if "%licgroup%" == "" goto set-licgroup if NOT exist %windir%\System32\dsquery.exe call :installdsquery title %title% %ver%.%update%:New Federated user echo ###################### echo # New Federated user # echo ###################### set /p NewUserFname=Firstname?: set /p NewUserSname=Surename?: :add-fed-user-OU set /p temOU=Location? [%NewUserOu%]: if "%tempOU%" == "" set tempOU=%NewUserOu% call :checkOU %tempOU% if NOT %OUExist% == 0 echo OU does not exist && goto add-fed-user-OU REM here you can set the default naming of your new account :add-fed-user-check-existing REM make sure the variable is empty set victem= set /p victem=New username? [%NewUserFname: =.%.%NewUserSname: =.%]: if "%victem%" == "" set victem=%NewUserFname: =.%.%NewUserSname: =.% net user /domain %victem% >Nul if %errorlevel% == 0 ( call :color 0c "%victem% already exist" echo. powershell sleep 3 cls goto add-fed-user-check-existing ) cls Echo New user account for %NewUserFname% %NewUserSname% is:%victem% set /p psswd=New users password?: cls dsadd user "CN=%NewUserFname% %NewUserSname%,%tempOU%" -samid %victem% -fn "%NewUserFname%" -ln "%NewUserSname%" -display "%NewUserFname% %NewUserSname%" -email %victem%@%dns% -upn %victem%@%dns% -disabled no -pwd %psswd% -memberof "%licgroup%" If NOT %errorlevel% == 0 Echo there is a error with errorcode %errorlevel% please try again && pause && goto menu echo User added sucsessfully :add-fed-user-license REM after 30 minutes the new users should be created in the MSOL portal REM we wont wait for it but let a script wait in the background Echo $host.ui.RawUI.WindowTitle = "Waiting for License for %victem%">c:\temp\%victem%.ps1 Echo $LiveCred = Get-Credential>>c:\temp\%victem%.PS1 Echo Function Start-Countdown >>c:\temp\%victem%.PS1 Echo { >>c:\temp\%victem%.PS1 Echo Param(>>c:\temp\%victem%.PS1 Echo [Int32]$Seconds = 10,>>c:\temp\%victem%.PS1 Echo [string]$Message = "Pausing for 10 seconds...">>c:\temp\%victem%.PS1 Echo )>>c:\temp\%victem%.PS1 Echo ForEach ($Count in (1..$Seconds))>>c:\temp\%victem%.PS1 Echo { Write-Progress -Id 1 -Activity $Message -Status "Waiting for $Seconds seconds, $($Seconds - $Count) left" -PercentComplete (($Count / $Seconds) * 100)>>c:\temp\%victem%.PS1 Echo Start-Sleep -Seconds 1>>c:\temp\%victem%.PS1 Echo }>>c:\temp\%victem%.PS1 Echo Write-Progress -Id 1 -Activity $Message -Status "Completed" -PercentComplete 100 -Completed>>c:\temp\%victem%.PS1 Echo }>>c:\temp\%victem%.PS1 Echo cls>>c:\temp\%victem%.PS1 Echo Start-Countdown -Seconds 10 -Message "You can minimize this window">>c:\temp\%victem%.PS1 Echo cls>>c:\temp\%victem%.PS1 Echo Start-Countdown -Seconds 1872 -Message "Waiting for the sync to complete (approximate 30 minutes)">>c:\temp\%victem%.PS1 Echo import-module msonline>>c:\temp\%victem%.PS1 Echo $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic –AllowRedirection>>c:\temp\%victem%.PS1 Echo cls>>c:\temp\%victem%.PS1 Echo $import = import-pssession $session>>c:\temp\%victem%.PS1 Echo connect-msolservice -credential $livecred>>c:\temp\%victem%.PS1 REM change usagelocation if other than dutch Echo Set-MsolUser -UserPrincipalName "%victem%@%dns%" -UsageLocation NL>>c:\temp\%victem%.PS1>>c:\temp\%victem%.PS1 Echo Set-MsolUserLicense -UserPrincipalName "%victem%@%dns%" -AddLicenses %userdomain%:ENTERPRISEPACK>>c:\temp\%victem%.PS1>>c:\temp\%victem%.PS1 Echo Completed>>c:\temp\%victem%.PS1 Echo sleep 120>>c:\temp\%victem%.PS1 Echo del c:\temp\%victem%.PS1;exit>>c:\temp\%victem%.PS1 cls Call :color 0c "please save the file before closing this window" echo. powershell sleep 5 start /wait "" notepad.exe c:\temp\%victem%.PS1 start "License %victem%" "powershell.exe" "c:\temp\%victem%.PS1" cls Call :color 0c "please be aware of the minimized powershell window" echo. Echo This windows will wait 30 minutes before activating the license powershell sleep 40 goto Add-proxy2