PowerShell System Admin Module

MVP month !!!!!!!!!!!!! and motivated to share more and more with the community.

Converted my important Powershell functions that are used by me, my followers and team on daily basis to system admin module — vsadmin

You can install this module from Powershell gallery

Use the command –> Install-Module -Name vsadmin  (run in powershell as administrator)

What it contains ????

  • This contains all the office365  all in one connect functions that you are  already using, I added some some error checking/handling to it.

All in One Office 365 Powershell Connect – Includes Exchange online MFA

  • This also contains some of the other general functions such as Write-log, recycle, new folder creation, progress bar etc.

Lets describe these functions briefly after first installing it on a machine.

Launch Powershell as administrator and run the command to install this module

Install-Module -Name vsadmin

Ones installed you will find the below files created inside your modules directory:

C:\Program Files\WindowsPowerShell\Modules

Now you can use this module on your machines by using import-module vsadmin or you can have this in your profile so it auto loads.

Lets check what all functions are present in this module using

Get-Command -Module vsadmin

Lets start with office 365 Functions and then we will move on to other system admin functions that you can use daily.

  • LaunchEOL/RemoveEOL    (Exchange Online)
  • LaunchSOL/RemoveSOL    (Skype online)
  • LaunchSPO/RemoveSPO   (Sharepoint online)
  • LaunchCOL/RemoveCOL    (Security & Compliance)
  • LaunchMSOL/RemoveMSOL (MSonline Azure active directory)
  • LaunchEXOnprem/RemoveEXOnprem (for onpremise Exchnage server)

Three of the office 365 functions are prefixed so that they do not conflict with onpremise commands and easy to use/understand in hybrid script.

Note: Below native office 365 modules are necessary for Office 365 functions in vsadmin module to work, else it will ask you to install them.

Just mentioning it but I know these might already be installed on your machine as you are Office 365 admin and managing it.

If you are skype admin and have skype module installed then launch skype function will work fine for you, it does not mean that you want to install exchange online module as well.

Example:

LaunchEOL if you just want to connect to office 365, it will prompt you for authentication and ones authenticated you will get connected.

It will check if ExchangeOnlineManagemen shell is installed on your computer or not, if its not then it will provide you a hint.

 

See below I am now connected  and can use the exchange commands.

If you want to use in a script, LaunchEOL -Credential can be used by passing ps credentials.

Similarly you can can use other functions as they are designed similar manner.

For SKype Online –> Domain should be present, it is your domain that is prefix to .onmicrosoft.com tenant address.

Example:

LaunchSOL -Domain techwizard

 

For Sharepoint online -> OrgName is required

Example:

LaunchSPOorgName techwizard

Similarly to disconnect session you can use below functions:

RemoveEOL/ RemoveSOL /RemoveSPO etc.

One other good addition to this is onpremise exchange function LaunchEXOnprem/RemoveEXOnprem

To connect to onpremise Exchange Powershell use:

LaunchEXOnprem -psurl http://exchangeserver.techwizard.cloud/Powershell

or

LaunchEXOnprem -ComputerName exchangeserver.techwizard.cloud

both of the options will work.

To disconnect use:

RemoveEXOnprem -computername exchangeserver.techwizard.cloud

Now lets move over to system administration functions:

  • One of the important function of this module is write-log which can create the log path as well as create date formatted log.

Example:

In the script you can use this below syntax to create a log file in log folder or the folder you have created for logs.

$log = Write-Log -Name “log_Enable” -folder logs -Ext log

write-log -Message “start………….Script” -path $log  #default  will log as information

write-log -Message “warning………….Script” -path $log -Severity Warning #you can display warning using the severity

write-log -Message “error………….Script” -path $log -Severity error #you can display error using the severity

On screen:

Time stamped Log inside logs folder:

You can create CSV file as well if you want it for your scripts, just change the extension to csv

$report = Write-Log -Name “log_Enable” -folder reports -Ext csv

 

  • Start-Progressbar is the another function which will provide wait to your scripts in nice format.

Example:

Start-ProgressBar -Title “Test….Title” -Timer 10

  • New-foldercreation will create a folder if it does not exist.

Example:

New-FolderCreation -foldername testfolder

  • Set-Recyclelogs will recycle the filed under the folder as per the limit (in days) you specify

Example:

Set-Recyclelogs -foldername logs -limit 10  #if confirm is not used it will ask for confirmation

Set-Recyclelogs -foldername logs -limit 10 -confirm:$false

Set-Recyclelogs -foldername logs -limit 10 -confirm:$false -verbose # it will tell you files it is deleting

if you want to sepcify the full path as above will work according to current directory:

Set-Recyclelogs -folderlocation c:\data\test -limit 10

similarly you can recycle logs from remote machine as well.

Example:

Set-Recyclelogs -ComputerName testmachine -DriveName c -folderpath data\logs -limit 10

  • Save-EncryptedPassword can generate the password in encrypted form and save it in text file

Example:

Save-EncryptedPassword -password “testpassword” -path c:\data\password1.txt

  • Get-Auth is the another important function that you can use in your scripts to read credentials from encrypted text file which you created above.

Example:

$cred = get-auth -userId sukhija@techwizard.cloud -passwordfile c:\data\password1.txt

$pwd = $cred[0] ### credentials that can be used inside csom /api calls.

$pscredential = $cred[1] ###credentials that can be used for functions that supports ps credentials.

or

$cred = get-auth -userId sukhija@techwizard.cloud -password  “encryptedpassword”

$pwd = $cred[0] ### credentials that can be used inside csom /api calls.

$pscredential = $cred[1] ###credentials that can be used for functions that supports ps credentials.

 

  • Send-Email is similar as send-mailmessage but with an ability to handle array. (that is why it can send full error in message body.)

Example:

Send-Email -smtpserver smtpserver -From donotreply@labtest.com -To vsukhija@labtest.com -body $error -subject “test error”

  • Get-IniContent – to read ini files and the use the values in scripts.

Example:

$readini = Get-IniContent $inifile

$vartest = $readini[“initable”].value

  • last important and useful function is Save-CSV2Excel, as name suggests it converts CSV to formatted excel

Example:

Save-CSV2Excel -CSVPath C:\data\auditmbx.csv -Exceloutputpath c:\data\audit.xlsx

 

  • New-RandomPassword – it can generate random passwords

Example: New-RandomPassword – NumberofChars 9

it has been coded with 5, 9 ,14 and 20

  • Get-ADGroupMembersRecursive  (It can extract group members recursively – AD module required)

Example: Get-ADGroupMembersRecursive -Groups “Test Nested Group”

more about this function – https://techwizard.cloud/2020/10/24/get-ad-group-members-recursively/

  • Get-ADUserMemberOf  (It can check if user is member of group or not)

Example: Get-ADUserMemberOf -User “User” -Group “Group”

more about this function – https://techwizard.cloud/2020/12/31/check-if-ad-user-is-member-of-group/

 

I will be updating this module whenever I get time and will be adding more functions so that its more useful in day to day scripting.

Thanks for reading and installing

Tech Wizard

https://techwizard.cloud

https://syscloudpro.com/