microsoft / Intune-PowerShell-SDK

Native PowerShell support for invoking Microsoft Intune Graph API to enable IT Pro scenario automation.
MIT License
310 stars 77 forks source link

Table of Contents

Intune-PowerShell-SDK

This repository contains the source code for the PowerShell module which provides support for the Intune API through Microsoft Graph.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Getting started

One-time setup (PowerShell Gallery)

  1. Install the Microsoft.Graph.Intune module from: https://www.powershellgallery.com/packages/Microsoft.Graph.Intune
    Install-Module -Name Microsoft.Graph.Intune

One-time setup (GitHub)

  1. Download the module from the Releases tab in the GitHub repository.
  2. The "drop\outputs\build\Release\net471" folder in the zip file contains the module.
    • If you are using Windows, extract the "net471" folder. You must have .NET 4.7.1 or higher installed.
  3. The module manifest is the "Microsoft.Graph.Intune.psd1" file inside this folder. This is the file you would refer to when importing the module.
  4. Import the module:
    Import-Module $sdkDir/Microsoft.Graph.Intune.psd1

Before this module is used in your organization

An admin user must provide consent for this app to be used in their organization. This can be done with the following command:

Connect-MSGraph -AdminConsent

Each time you use the module

To authenticate with Microsoft Graph (this is not required when using CloudShell):

Connect-MSGraph

To authenticate with Microsoft Graph using a [PSCredential] object:

# 1. Create the PSCredential object
$adminUPN = Read-Host -Prompt "Enter UPN"
$adminPwd = Read-Host -AsSecureString -Prompt "Enter password for $adminUPN"
$creds = New-Object System.Management.Automation.PSCredential ($adminUPN, $adminPwd)

# 2. Log in with these credentials
Connect-MSGraph -PSCredential $creds

To authenticate in a non-standard environment:

# 1. Setup the environment
# For example, in a National Cloud environment, the following is required before logging in
Update-MSGraphEnvironment -AuthUrl 'https://login.microsoftonline.us/common' -GraphBaseUrl 'https://graph.microsoft.us' -GraphResourceId 'https://graph.microsoft.us' -SchemaVersion 'beta'

# 2. Log in
Connect-MSGraph

# 3. Use the cmdlets
# NOTE: If the schema version has been changed to something other than "v1.0" as in the above
#   "Update-MSGraphEnvironment" command, only "Invoke-MSGraphRequest" should be used to make calls,
#   because the standard cmdlets (e.g. "Get-IntuneMobileApp") have been generated based on the
#   "v1.0" schema, and can produce unexpected results when used with other schema versions
Invoke-MSGraphRequest -HttpMethod GET -Url 'deviceAppManagement/mobileApps'

Discovering available commands

Get the full list of available cmdlets:

Get-Command -Module Microsoft.Graph.Intune

Get documentation on a particular cmdlet:

Get-Help <cmdlet name>

Use a UI to see the parameter sets more easily:

Show-Command <cmdlet name>

Example usage

Retrieving objects

Get all Intune applications:

Get-IntuneMobileApp

Get all Intune device configurations:

Get-IntuneDeviceConfigurationPolicy

Get all Intune managed devices:

Get-IntuneManagedDevice

Get a filtered list of applications and select only the "displayName" and "publisher" properties:

# The filter string follows the same rules as specified in the OData v4.0 specification.
# Filter string construction rules: http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/abnf/odata-abnf-construction-rules.txt
Get-IntuneMobileApp -Select displayName, publisher -Filter "isof('microsoft.graph.webApp')"

Creating objects

Create a web application:

$bingWebApp = New-IntuneMobileApp -webApp -displayName 'Bing' -publisher 'Microsoft Corporation' -AppUrl 'https://www.bing.com'

Modifying objects

Update the web application that we created in the 'Creating objects' section:

$bingWebApp | Update-IntuneMobileApp -webApp -displayName 'Bing Search'

Deleting objects

Delete the web application that we created in the 'Creating objects' section:

$bingWebApp | Remove-IntuneMobileApp

Calling functions and actions

Lock a managed device:

# Get a device to lock
$allDevices = Get-IntuneManagedDevice
$deviceToLock = $allDevices[0]

# Lock this device
$deviceToLock | Invoke-IntuneManagedDeviceRemoteLock

Tips and Tricks

Notable features

Known issues and workarounds