4

I am working on some utility functions to ease the administration of our O365 tenant by importing some modules as part of my profile. The thing I'm working on currently is being able to provision One Drive for Business sites for users. I have the following code that works properly so far outside of a function but once I try to make it a function, I get the error in the title. What am I doing wrong here?

Works:

$SecurePassword = Read-Host -Prompt "Please enter your password" -AsSecureString
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext("https://mytenant-my.sharepoint.com")
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($yourEmail,$SecurePassword)
    $Context.Credentials = $Credentials

    $Users = $Context.Web.SiteUsers
    $Context.Load($Users)
    $Context.ExecuteQuery()
    write-host $Users.Count

Fails:

Function New-MySite{
    #Not Yet Implemented
    Param(
        [Parameter(Mandatory=$true,HelpMessage="A comma delimited string of email addresses",Position=1)][ValidateNotNull()]
        [string]$users,
        [Parameter(Mandatory=$true,HelpMessage="Your email address",Position=0)][ValidateNotNull()]
        [string]$yourEmail
        )    
    $SecurePassword = Read-Host -Prompt "Please enter your password" -AsSecureString
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext("https://mytenant-my.sharepoint.com")
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($yourEmail,$SecurePassword)
    $Context.Credentials = $Credentials

    $Users = $Context.Web.SiteUsers
    $Context.Load($Users)
    $Context.ExecuteQuery()
    write-host $Users.Count
}

On Powershell 3 using the Powershell ISE client. Desired usage:

New-Mysite -yourEmail "eric@domain.com" -users "user1@domain.com"
  • Have you tried the method in this answer from SO? – wjervis Feb 27 '15 at 13:57
  • I did see that, I'm on powershell v3, and honestly I'm not grasping what it is doing. Does the Powershell ISE run a different version? – Eric Alexander Feb 27 '15 at 14:05
  • Same problem on Powershell ISE Host Version 4.0 – BioGeneZ Feb 27 '15 at 14:09
2

I ran into this same thing and was able to get this to work by replacing the commented out lines with the others.

# Add-Type -Path ([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client").location)
# Add-Type -Path ([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.runtime").location)
[Reflection.Assembly]::LoadFile(([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client").location))
[Reflection.Assembly]::LoadFile(([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.runtime").location))

$host.version is 4

0

As far as I can tell, I was referencing the wrong DLLs. After altering my ISE profile to pull the 16 DLLs, this resolved itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.