Living a SharePoint life

Tuesday, January 17, 2017

Connect to Office365 via Office PnP behind a proxy

How do you connect to your SharePoint Office365 tenant with the PowerShell if you are locked behind a proxy? This hint will probably work for all kind of PowerShell scripts, not only for Office PnP. I tried this with the Microsoft SharePoint Online extensions as well and it worked.

So my problem was, the company I was working for, had a proxy which could not be bypassed. The proxy settings have been set by a GPO and the network settings where fine so far. However the proxy used user authentication to access the internet. Now if I want to use the SharePoint Online tools, I must authenticate my PowerShell session at the proxy so the tools can access the internet too.

The trick to get your PowerShell connected is quite simple. You only need to enter the following line:

[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

Now you should be able to connect to Office365 SharePoint without problems. If you don't want to enter this line every time you open a new PowerShell, you could add it to your PowerShell profile.

ise $PROFILE
then add this to the profile file:

#Set user default credentials for any webrequest
"Setting the users default credentials for WebRequests..." | Write-Host -ForegroundColor White
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

10 comments:

  1. Trying to connect to may site collection ( SP2016) but through a proxy(

    $cred = [System.Net.CredentialCache]::DefaultCredentials
    [System.Net.WebRequest]::DefaultWebProxy.Credentials = $cred
    Connect-PnPOnline -Url https://xxx -UseWebLogin
    he problem is no connection was made:

    Get-PnPWeb or Get-PnPSite is null

    Get-PnPSite : Cannot contact site at the specified URL https://xxx/. Au caractère Ligne:1 : 1 + Get-PnPSite + ~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Get-PnPSite], ClientRequestException + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Site.GetSite

    ReplyDelete
    Replies
    1. As long as you havn't connected to the site thru Connect-PnPOnline, every other PnP cmdlet will fail and raise an exception.
      The PowerShell line [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials will set the context of the user running the PowerShell to be used for any kind of http connection estabished thru System.Net.Webrequest. Possible problems may be the user isn't allowd to login to the proxy, or the proxy does deep packet inspections and SSL termination to block unauthorized traffic.

      Delete
  2. I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. Office video intercom installation

    ReplyDelete
  3. This short article posted only at the web site is truly good.

    ReplyDelete
  4. You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. 1337x

    ReplyDelete
  5. Thanks to innovations in Internet technology, it is a lot easier (and often more profitable) to utilize the services of a remote workforce to handle a business's many tasks mindmap templates

    ReplyDelete
  6. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. via4d

    ReplyDelete
  7. This is highly informatics, crisp and clear. I think that everything has been described in systematic manner so that reader could get maximum information and learn many things. https://europa-road.eu/hu/nemzetkozi-szallitmanyozas-logisztika.php

    ReplyDelete
  8. This is a very interesting web page and I have enjoyed reading many of the articles and posts contained on the website, keep up the good work and hope to read some more interesting content in the future.torrentz2 proxy

    ReplyDelete
  9. If you have been considering upgrading your dial-up connection to an internet service that is faster and more reliable, or if you have been eyeing different internet service providers (ISPs) who are offering different types of internet connection platforms, then one question has surely crossed your mind: what can broadband internet do for me? Broadband internet has become the most popular option for majority of internet users. Studies show that, in the US alone, broadband penetration is at a high 80%. Unblock Kickass

    ReplyDelete

Featured Post

The Retro Powershell - Looking good in 8-Bit | Part 1

I wrote a little script that, when placed in your PowerShell Profile, will print a message similar to the old boot message you got from you...