Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Wednesday, 16 July 2014

Assigning Office 365 options to users (Basic)


I have recently been working with a school who have signed up to Microsoft’s OVS-ES service, as a part of this they are entitled to apply for Student Advantage licenses which will allow pupils to download and install an up to date version of Office on up to 5 devices at home. In order to access this feature the school need to sign up for an Office 365 account and to use the A2 (Free) service.
Administrators can apply licenses to staff and pupils through a website, however this only allows you to modify a limited number of users at a time but Microsoft have been generous and provided a means of automating some of these processes using PowerShell.
In order to accomplish this you will require the Windows Azure Components installed on your computer, once these are installed you will be able to use PowerShell to connect and work with your Office 365 service.
The script below is an example of PowerShell code which will allow you to apply a license to users within a certain department (AD Attribute), once this license has been applied any restrictions you wish on the services within the license are added.
This script could be amended to work with Pupil users by editing the License used and the Department searched for, however the script is very basic and will not take into account any users who may have conflicting entries to those you set, I have not tested this scenario.
I will follow up on this basic post with a more comprehensive one which will allow you to pick the department and license you wish to apply based on basic text menu’s presented in the shell but for now this will give you something to be going on with.
1 ################################################################################### 2 # Marc Hundley 3 # 4 # Version 0.1 5 # 6 # 16/07/2014 7 # 8 # Purpose : Allocating with restrictions access for Staff to the O365 tools online 9 ################################################################################### 10 #11 # Requirements 12 # ------------ 13 #14 # Windows Azure Components 15 # Office 365 online account 16 #17 ###### 18 #19 # Suitable amount of licenses to allocate to staff 20 #21 ###### 22 #23 # Knowledge of TENANT_ID - can be obtained by using the following commands : 24 #25 # import-module MSOnline 26 # $msolcred = Get-Credential 27 # Connect-MsolService -Credential $msolcred 28 # $licenses = Get-MsolAccountSku 29 # $licenses 30 #31 # This will give you all of the available licenses, the TENANT_ID will be the 32 # common factor before the : 33 #34 ###### 35 #36 # Edit the -DisabledPlans entry depending on the needs of the customer, these 37 # can be obtained with the command : 38 #39 # import-module MSOnline 40 # $msolcred = Get-Credential 41 # Connect-MsolService -Credential $msolcred 42 # $licenses = Get-MsolAccountSku 43 # $licenses[x].ServiceStatus 44 #45 # Where [x] is the license you wish to view (array starting at 0) 46 #47 # Options for the STANDARDWOFFPACK_FACULTY are as follows 48 #49 # YAMMER_EDU 50 # SHAREPOINTWAC_EDU 51 # MCOSTANDARD 52 # SHAREPOINTSTANDARD_EDU 53 # EXCHANGE_S_STANDARD 54 #55 ###### 56 #57 # Staff members are to be members of the Staff department in Active Directory 58 # which has synchronised with Office 365 59 #60 ################################################################################### 61 62 #Import O365 Azure module 63 import-module MSOnline64 #Clear Screen 65 cls66 67 #Connect to Office 365 with admin credentials 68 $msolcred = Get-Credential69 Connect-MsolService -Credential $msolcred 70 71 #get users 72 $users = Get-MsolUser -Department "Staff" -MaxResults 2500 73 74 #Assign faculty pack exclusions to variable (edit disabledplans as needed by customer) 75 $myO365Sku = New-MsolLicenseOptions -AccountSkuId <TENANT_ID>:STANDARDWOFFPACK_FACULTY -DisabledPlans EXCHANGE_S_STANDARD76 77 #Assign components for each user 78 foreach ($user in $users) {79 $username = $user.UserPrincipalName80 write-Host "Assigning License for "$username 81 #Add Overall license (required before setting restrictions) 82 Set-MsolUserLicense -UserPrincipalName $username -AddLicenses <TENANT_ID>:STANDARDWOFFPACK_FACULTY83 #Assign exclusions 84 Set-MsolUserLicense -UserPrincipalName $username -LicenseOptions $myO365Sku 85 }

Thursday, 4 April 2013

Migrating Wireless settings between Windows 7/8 Machines

 

I have recently been given a new laptop from work, as part of the setup process I needed to copy over the large number of wireless networks that I connect to as part of my job.Windows provides an export using the netsh command

netsh wlan export profile folder=. key=clear

The code above will export all networks as individual xml files into the current working directory which is great, however the netsh command does not to my knowledge allow the import of all definition files in a directory in one go, so how to solve this problem? Our old friend PowerShell, after all we are batching a command and PowerShell is pretty good at not giving a damn about file names or how many items are in a folder.

 

So this post is going to be all about a PowerShell script which allows you to export your wireless settings and import them again elsewhere. The script itself is split into several functions and a few loops and is pretty simple and can probably be made much more elegant, however it does work Smile

 

Code:

We start by declaring or resetting our variables, always a good idea if we are running the code in the PowerShell ISE or PowerGui (My preference) in case any values remain from a previous run through

1 $path="D:\Wireless"
2 $menuanswer=$null
3 $pathready=$null

$path is the path we want to use to store the wireless xml files in, this can be edited to any folder we like (useful if you don’t have a d: drive like I do)


Now we need a few functions, the first will export our wireless connections to xml files


Export Profiles


1 function export_wifi_profiles{
2 #check for folder path
3 $pathready = Test-Path $path
4 if ($pathready -eq $false){
5 mkdir $path
6 }
7 cd $path
8 netsh wlan export profile folder=. key=clear
9 }

Line 3 is used to check whether the path we have specified earlier as available, if the path does not exist then the folder specified will be created before moving on, if it exists then the netsh command will be run which will dump all wireless networks as individual files into the path.


 


Import WIFI Profiles



1 function import_wifi_profiles{
2 #check default path, if not found prompt for location
3 do {
4 $pathready = Test-Path $path
5 if ($pathready -eq $false){
6 $path=Read-Host "Default import path not found, please enter the path your profiles are stored in"
7 }
8 } until ($pathready -eq $true)
9
10 $path | Get-ChildItem | ForEach-Object{
11 $filepath=$_.FullName
12 netsh wlan add profile filename=$filepath user=all
13 }
14 }


As with the export this function checks for the existence of the folder stated in the $path variable, if it does not exist then the user is prompted to enter the path to their network profiles, once a valid file path is provided the script collects all of the items found within (Get-ChildItem), it then copies the full file path and name into the $filepath variable, this is then used in the netsh command to do the actual import of the wireless profile.


The beauty of the Get-ChildItem | ForEach-Object code is that it doesn’t care whether you have 1 or 100 wireless profiles in the folder, it will work its way through each of them until it is done.


 


Create a Menu


1 function menu{
2 cls
3 Write-Host "1 Export Wireless Profiles"
4 Write-Host
5 Write-Host "2 Import Wireless Profiles"
6 Write-Host
7 Write-Host "0 Exit"
8 Write-Host
9 $menuanswer=Read-Host "Please make Your selection"
10 if ($menuanswer -eq 1){export_wifi_profiles}
11 if ($menuanswer -eq 2){import_wifi_profiles}
12 if ($menuanswer -eq 0){exit}
13 }

The code above create a simple menu with options 1,2 and 0 to exit, the users entry is saved into the $menuanswer variable and evaluated against the options in lines 10,11, and 12, if the entry matches any of the options then that function is called, the {exit} command will terminate the script and completely exit PowerShell.


 


Call the Menu until exit command used (0 Key)


1 do {menu} until ($menuanswer -eq 0)

This code will simply repeat the Menu function until the user enters 0, this allows the user to export and re-import the wireless profiles for testing purposes if that is required. It is the only real line of code which is run when you launch the script, the functions are called as needed.


 


Putting it all together


 


Copy and paste the contents of the next box and edit the $path variable at line 2 to suit your own needs, as I have said this is a basic script and can be made much more elegant but for my needs it was suitable, good luck.



1 #declare variables
2 $path="D:\Wireless"
3 $menuanswer=$null
4 $pathready=$null
5
6 #Declare functions
7 function export_wifi_profiles{
8 #check for folder path
9 $pathready = Test-Path $path
10 if ($pathready -eq $false){
11 mkdir $path
12 }
13 cd $path
14 netsh wlan export profile folder=. key=clear
15 }
16
17 function import_wifi_profiles{
18 #check default path, if not found prompt for location
19 do {
20 $pathready = Test-Path $path
21 if ($pathready -eq $false){
22 $path=Read-Host "Default import path not found, please enter the path your profiles are stored in"
23 }
24 } until ($pathready -eq $true)
25 $path | Get-ChildItem | ForEach-Object{
26 $filepath=$_.FullName
27 netsh wlan add profile filename=$filepath user=all
28 }
29 }
30
31 function menu{
32 cls
33 Write-Host "1 Export Wireless Profiles"
34 Write-Host
35 Write-Host "2 Import Wireless Profiles"
36 Write-Host
37 Write-Host "0 Exit"
38 Write-Host
39 $menuanswer=Read-Host "Please make Your selection"
40 if ($menuanswer -eq 1){export_wifi_profiles}
41 if ($menuanswer -eq 2){import_wifi_profiles}
42 if ($menuanswer -eq 0){exit}
43 }
44 #End Functions
45
46 #Begin main Program Block
47 do {menu} until ($menuanswer -eq 0)
48 #End main program block

Wednesday, 1 August 2012

Creating a more useful Powershell prompt

 

 

As Microsoft are moving more and more functions for their products to be Powershell aware, we are starting to use PS more than VBScript in our daily lives. Although I often use Quest’s Powergui for my coding as the watches built into the interface make debugging easier, I do also make use of the Powershell itself, in these instances I have to import the relevant module manually, by editing your PS profile you can make these tools available every time you click on the little blue icon.  This walkthrough will install the Active Directory and Exchange tools.

 

Here are the steps to achieve this.

 

1. Create a profile (If you don’t already have one)

New-Item -Itemtype file -path $profile -force


2. Edit your profile with notepad

notepad $profile

3. Add the following code

import-module activedirectory

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
Connect-ExchangeServer –auto

4. Save the file and re-open Powershell

 

You will now have a Powershell which loads the AD and Exchange tools by default.

Tuesday, 5 April 2011

Import drivers into MDT maintaining folder structure

I recently got an email from Johan Arwidmark's mailing list at Truesec, this mail contained a powershell script to import drivers into MDT whilst maintaining the folder structure, this will allow multiple driver imports for a range of hardware without dumping all of the drivers in on huge lump in the 'Out Of Box Drivers' folder.


The Drivers folder layout will follow the format as follows

Drivers\
     |------OS 1\
     |          |--------->Manufacturer1\
     |          |                   |----------->Model1\
     |          |                   |----------->Model2\
     |          |--------->Manufacturer2\
     |                              |----------->Model1\
     |                              |----------->Model2\
     |------OS 2\
                |--------->Manufacturer1\
                |                   |----------->Model1\
                |                   |----------->Model2\
                |--------->Manufacturer2\
                                   |----------->Model1\
                                   |----------->Model2\



     
You will need to edit the first three lines of this script to match your environment, otherwise it should be good to go.


$DriverStore = "C:\Drivers"
$MDTDSRoot = "C:\MDTBuildLab"
$PSDriveName = "DS001"

Add-PSSnapIn Microsoft.BDD.PSSnapIn
New-PSDrive -Name "$PSDriveName" -PSProvider MDTProvider -Root $MDTDSRoot
# Proces each of the operating systems folders
Get-ChildItem "$DriverStore" | foreach {

    # Display the folder we are processing
    Write-Host "Processing $($_.FullName)" -ForeGroundColor green;Write-Host ""

    # Create the operating system folder in the MDT 2010 Deployment Workbench
    new-item -path $PSDriveName":\Out-of-Box Drivers" -enable "True" -Name "$($_.Name)" -ItemType "folder" -Verbose

    # Process each of the vendor folders
    $OSFolder = $_
    Get-ChildItem $_.FullName | foreach {
       
        # Display the folder we are processing
        Write-Host "Processing $($_.FullName)" -ForeGroundColor green;Write-Host "" 

        # Create the vendor folder in the MDT 2010 Deployment Workbench
        new-item -path $PSDriveName":\Out-of-Box Drivers\$OSFolder" -enable "True" -Name "$($_.Name)" -ItemType "folder" -Verbose
       
       # Process each of the model folders
       $VendorFolder = $_
       Get-ChildItem $_.FullName | foreach {

            # Display the folder we are processing
            Write-Host "";Write-Host "Processing $($_.FullName)" -ForeGroundColor green;Write-Host "" 
   
           # Create the model folder in the MDT 2010 Deployment Workbench
           new-item -path $PSDriveName":\Out-of-Box Drivers\$OSFolder\$VendorFolder" -enable "True" -Name "$($_.Name)" -ItemType "folder" -Verbose

           # Import the drivers into MDT 2010 Deployment Workbench
           Import-MDTDriver -Path $PSDriveName":\Out-of-Box Drivers\$OSFolder\$VendorFolder\$($_.Name)" -SourcePath "$($_.FullName)" -Verbose
          }
    } 
}

During testing of this script I encountered errors while creating folders, this error appears to be down to the code using  '-enable "True"' powershell spits this out with some ugly red lettering, I have found that by removing this part of the code the script appears to run perfectly and populate MDT as we hope.