Office365

This script is for office365 administrators.

Todo

  • give the option to add a new users including add aliasses and assign license

Changelog

  • Version 0.4
    – first production version
    – add the abbility to mass add the aliasses to active directory users
  • Version 0.6
    Add aliasses to cloud mailboxe

Download as .bat
Download as .txt

 

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=08-09-2014
set ver=0.6
set update=
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\

net session>NUL
if %errorlevel% == 0 set localadmin=yes
rem check if user is local admin
rem change color when user is localadmin
set defcolor=%usercolor%
if %localadmin% == yes set defcolor=%admincolor%

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"
)
<nul > 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

: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 Advanced options
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 #    ROLOUT    #
echo # Version %ver%  # 
Echo ################
echo #
echo # 
echo # 1 Reset users aliasses
echo # 2 
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?:
Call :log %volid%: option advmenu %advmenu%
REM processing the mneu
if /I %advmenu% == 1 goto Add-proxy
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 # 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% == 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

: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 set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
goto :EoF


:log
powershell -Command "(New-Object Net.WebClient).DownloadFile('%website%/log.php?text=%username% : %computername% : %userdomain% : V%ver%.%update% : %*', 'c:\temp\temp.dat')"
del c:\temp\temp.dat
Goto :EoF


REM check if client is online
REM second check for connection to the OS
REM Computer is %vicstat% 
:checkvictem
ping %victem: =% -n 1|find ": bytes=32 time" > 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.
echo REM this file is part of the rolout script > %drive%script-settings.bat
echo set admincolor=%admincolor: =%>> %drive%script-settings.bat
echo set usercolor=%usercolor: =%>> %drive%script-settings.bat
attrib +h %drive%script-settings.bat
call :color 0c "default settings loaded"
echo.

)
goto :EoF

REM after changing the settings write them to the same location as the script
:setsettings
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
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 roloutscript %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"
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-proxy
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%@%USERDNSDOMAIN%>c:\temp\proxy.tmp
echo SIP:%victem%@%USERDNSDOMAIN%>>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...
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
REM Set-Mailbox mark-test@fikira.nl -EmailAddresses @{add="testvanmark2@fikira.com"}
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 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

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *