Tag Archive: msonline


Powershell 101

The aim of this post is to explain some basic tips and tricks about the usage of powershell in a real world:

For reference to well-known PowerShell web sites, go to my Web links on this web site or here is a reference for powershell cmdlets: http://ss64.com/ps/

else in French a simple tutorial:http://lavalisedutechnicien.free.fr/joomla/index.php?option=com_content&view=article&id=195&Itemid=297

Videos:

https://channel9.msdn.com/Blogs/Taste-of-Premier/PowerShellBasicsPart1

https://channel9.msdn.com/Blogs/Taste-of-Premier/PowerShell-Basics-Part-2-Real-Word-Examples-for-Our-Most-Common-Cmdlets

https://channel9.msdn.com/Blogs/Taste-of-Premier/PowerShell-Basics-Part-3-Real-Word-Script-Examples

https://channel9.msdn.com/Blogs/Taste-of-Premier/Whats-New-in-PowerShell-v5

 

0-to retrieve the version and language of powershell:

$host.version

$PSCulture    or Get-Culture

1- create powershell profiles:

get-help about_profiles   ,  get-help about_*

the two well-known profiles are:

  • $profile        ; to display the path of the current user’s (Current User; Current Host)

NCEDALBERA3\C:\Windows\system32> $profile
C:\Users\jdalbera\My DATA\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

  • $pshome ;  to display the path of the main profile.ps1 (All Users;All Hosts)

NCEDALBERA3\C:\Windows\system32> $pshome
C:\Windows\System32\WindowsPowerShell\v1.0

if there is no profile.ps1 create it !

NCEDALBERA3\C:\Windows\system32> cd C:\Windows\System32\WindowsPowerShell\v1.0
NCEDALBERA3\C:\Windows\System32\WindowsPowerShell\v1.0> dir *.ps1
Répertoire : C:\Windows\System32\WindowsPowerShell\v1.0

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
-a—        04/11/2012     23:13        802 Microsoft.PowerShellISE_profile.ps1
-a—        13/03/2014     22:53       1774 profile.ps1

  • Microsoft.PowerShellISE_profile.ps1  ; correspond to the powershell profile read when ISE is started

 

2- import the modules on profiles (for Windows server):

servermanager, activedirectory

to list the modules available: get-module -list  or get-installedmodules

3- verify the variables (variables psdrive)

get-psdrives  ; to list all available PSdrives

dir variables:

and modify the error;debug… accordingly

4- Powershell help:

PS v3: be careful with the get-help command. http://www.silogix.fr/blog-silogix/powershell-v3-du-nouveau-dans-laide.aspx

– use/read intensively the equivalent of well-knows MAN pages on Unix world, called here, get-help and get-help about_*

– get-help <cmdlet> -examples|-full|-detailed

– <cmdlet> | get-member  ;  to list the cmdlet properties and methods

– get-command -module <modulename>

– get-command *module

– get-command | where-object { $_.definition -like “*-new*”}  ; display all the cmdlets containing -newest as parameter

– get-command | where-object { $_.definition -like “*computername*”}  ; display all the cmdlets containing “computername” as parameter

– get-module -listavailable

– get-alias                ; list all the aliases

– <cmdlet> | fl prop1,*prop2… or | ft -autosize

5- On powershell scripts:

– create functions and modules; function(){}

– use try/catch/finally, to trap errors

– use -ErrorActivation or -ErrorVariable for each cmdlet

– use the foreach-object {} cmdlet

– use the import-CSV, export-CSV, exportto-XML, out-file

– use intensively: where-object {} cmdlet

– use intensively: tee-object -filepath|-variable

– use intensively arrays: get-help about_arrays, about_hash_tables

– WMI support: get-wmiobject cmdlet

5- Examples of well-known cmdlets:

– to restart a computer without be prompted: restart-computer

– get-childitem -path c:\users -recurse -include *.txt

– to rename files in bulk mode: get-ChildItem -Filter “*current*” -Recurse | Rename-Item -NewName {$_.name -replace ‘current’,’old’ }

– to rename image files in bulk mode: Get-ChildItem -Filter *.jpg | %{ Rename-Item $_.FullName ($_.Name.split(“.”)[0].Substring(3)+”.jpg”)}

– get-service | format-list  ; get-service | get-member |out-gridview

– get-process | format-wide

6- How to remove string in a variable:

PS C:\WINDOWS\system32> $grp = “o365grp-my group-jda”

PS C:\WINDOWS\system32> $res = $grp -creplace ‘^o365grp-‘,”
PS C:\WINDOWS\system32> $res
my group-jda

7- How to install AzureAD and MSOnline modules:

Get-InstalledModule -Name “AzureAD*”

To uninstall a previous version of AzureADPreview or AzureAD, run this command:
Uninstall-Module AzureADPreview
or
Uninstall-Module AzureAD

To install the latest version of AzureADPreview, run this command:   Install-Module AzureADPreview -Force

To install MSOnline run: Install-Module Msonline -Force

To install MSonline latest version: Install-Module -Name MSOnline -RequiredVersion 1.1.183.8

Get-InstalledModule -Name “MSOnline”

8– Parsing text with powershell:

https://devblogs.microsoft.com/powershell/parsing-text-with-powershell-1-3/

https://devblogs.microsoft.com/powershell/parsing-text-with-powershell-2-3/

https://devblogs.microsoft.com/powershell/parsing-text-with-powershell-3-3/

 

Advertisements

AzureAD PowerShell module

Versionning:

Note: the preview module is the most recent and contain new cmdlet like Get-AzureADDirectorySetting or Set-AzureADDirectorySetting

Download and install the 64-bit version of the Online Services Sign-In Assistant from the following url: http://www.microsoft.com/en-us/download/details.aspx?id=41950

Download and install the 64-bit version of the Windows Azure Active Directory Module for Windows PowerShell the following url: https://bposast.vo.msecnd.net/MSOPMW/9031.1/amd64/AdministrationConfig-en.msi

 

Reference:

Getting started with PowerShell for O365:

https://docs.microsoft.com/en-us/office365/enterprise/powershell/getting-started-with-office-365-powershell

Azure AD PowerShell module:

https://docs.microsoft.com/en-us/powershell/azure/active-directory/install-adv2?view=azureadps-2.0

https://docs.microsoft.com/en-us/powershell/gallery/readme

Powershell cmdlets reference and tips/scripts:

https://docs.microsoft.com/en-us/powershell/module/azuread/?view=azureadps-2.0

https://drewmadelung.com/managing-office-365-group-using-azure-ad-powershell-v2/

https://github.com/dmadelung/O365GroupsScripts/blob/master/DrewO365GroupsScripts%20-%20Azure%20AD%20Cmdlets

How to install the module to access Office 365/Azure AD:

The PowerShellGet module requires PowerShell 3.0 or newer. Therefore, PowerShellGet requires one of the following operating systems:

  • Windows 10
  • Windows 8.1 Pro
  • Windows 8.1 Enterprise
  • Windows 7 SP1
  • Windows Server 2016 TP5
  • Windows Server 2012 R2
  • Windows Server 2008 R2 SP1

PowerShellGet also requires .NET Framework 4.5 or above. You can install .NET Framework 4.5 or above from here.

Then from a PowerShell prompt enter:

Check installed module:
Get-InstalledModule -Name “AzureAD*”

Get-InstalledModule -Name “MSOnline”

To uninstall a previous version of AzureADPreview or AzureAD, run this command:
Uninstall-Module AzureADPreview
or
Uninstall-Module AzureAD

To install the latest version of AzureADPreview, run this command:   Install-Module AzureADPreview -force

At the message about an untrusted repository, type Y. It will take a minute or so for the new module to install.

Then

Connect-azureAD

Connect-msolservice

example:  get-azureaduser -filter “displayname eq ‘firstname lastname'” | select displayname,mail

To list all user attributes:
get-msoluser -userprincipalname firstname.lastname@mydomain.com | select *

With AzureAD cmdlets (need module AzureAD):
get-azureaduser -filter “displayname eq ‘firstname lastname'” | select *

Working with extension attributs:

https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-graph-api-quickstart

Working with Azure AD Extension Attributes with Azure AD PowerShell v2