Proxy setting for powershell connecting to outlook.office365 - powershell

I'm working on PowerShell script that connect to outlook office365(exchange online) as follows:
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication Basic -AllowRedirection
Now the problem is I want to connect via a proxy server with authentication, so did the following
$proxy = New-Object System.Net.WebProxy "http://myproxy:80"
$proxy.Credentials = $cred
[System.Net.WebRequest]::DefaultWebProxy = $proxy
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential
$credential -Authentication Basic -AllowRedirection
But it doesn't connect via proxy that I set. So I did the following:
$proxy = New-Object System.Net.WebProxy "http://myproxy:80"
$proxy.Credentials = $cred
[System.Net.WebRequest]::DefaultWebProxy = $proxy
$sessionOption = New-PSSessionOption -ProxyAccessType IEConfig
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential
$credential -Authentication Basic -AllowRedirection -SessionOption $sessionOption
But still doesn't pass through proxy.
I also tried to use netsh winhttp set proxy "myproxy:80" and it passed through the proxy server but it seems it doesn't have authentication.
Is there any way to explicitly set the proxy for New-PSSession cmdlet?
Note: I don't want to set proxy setting on IE, just want to explicitly set proxy per session.

Did you tried the following:
1.) Set proxy via NETSH
2.) Inside your powershell approach use:
$webclient=New-Object System.Net.WebClient
$creds=Get-Credential
$webclient.Proxy.Credentials=$creds

This is what I did and it seems working.
$proxyAddress = $proxyHost + ":" + $proxyPort
netsh winhttp set proxy $proxyAddress
$proxysecpasswd = ConvertTo-SecureString $proxyPassword -AsPlainText -Force
$proxycred = New-Object System.Management.Automation.PSCredential($proxyUser, $proxysecpasswd)
$sessionOpts = New-PSSessionOption -ProxyAccessType WinHttpConfig -ProxyCredential $proxycred -ProxyAuthentication Basic
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($userId, $secpasswd)
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $credential -Authentication "Basic" -AllowRedirection -SessionOption $sessionOpts
Please make some comments if there are any other better ways or if this will pose any other problems that I should be aware of.

Related

Hide specific emails From Address Lists

I try to hide specific emails From Address Lists in 365.
Import-Module MSOnline
$TenantUname = "****"
$TenantPass = cat "C:\****.key" | ConvertTo-SecureString
$TenantCredentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $TenantUname, $TenantPass
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $TenantCredentials -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
Connect-MsolService -Credential $TenantCredentials
ForEach ($mailbox in (Import-Csv c:\email.csv))
{
Set-Mailbox -Identity $mailbox.Identity -HiddenFromAddressListsEnabled $True
}
In my CSV file identity is the emails of users.
But from some reason, this is not work
I will be very happy to help.

Microsoft Powershell script running problem

I try to run following script:
Get-ExecutionPolicy
$UserCredential = Get-Credential
Create session to: https://outlook.office365.com/powershell-liveid/ with following:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange
-ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential
-Authentication Basic -AllowRedirection
After I run the last script, it returns with following error:
"New-PSSession : [outlook.office365.com] Connecting to remote server outlook.office365.com failed with the following err
or message : Adgang nægtet. For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:12
+ $Session = New-PSSession -ConfigurationName Microsoft.Exchange -Conne ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
gTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed"
If you're just trying to connect to Exchange the module ExchangeOnlineManagement , you can use this:
$creds = (Get-Credential)
Connect-ExchangeOnline -UserPrincipalName $creds.UserName
I've also had success using this:
$creds = (Get-Credential)
$exoSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $creds -Authentication Basic -AllowRedirection
Import-PSSession $exoSession -DisableNameChecking

You must call the Connect-MsolService cmdlet before calling any other cmdlets

I have powershell script that connects to the Msol service .When i tried to run the script it works locally(powershell commandline Vscode) but when i try to run script via Active batch it is giving me error "You must call the Connect-MsolService cmdlet before calling any other cmdlets" .
$secpasswd = ConvertTo-SecureString "" -AsPlainText -Force
$EXOCred= New-Object System.Management.Automation.PSCredential ("",$secpasswd)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $EXOCred -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
$secpasswd = ConvertTo-SecureString "" -AsPlainText -Force
$MSOCred= New-Object System.Management.Automation.PSCredential ("",$secpasswd)
Connect-MsolService -Credential $MSOCred
$RoleObject = Get-MsolRole | Where-Object{$_.Name -match $roles}
$members= Get-MsolRoleMember -RoleObjectId $RoleObject.ObjectId
Can anyone help as i am getting error when executing the script in Active Batch

Import exchange management session into powershell

I get user credential from user using gui
$userid =$email.text
$password = $Password.text
$Session = New-PSSession -Authentication default -credential($userid,$passowrd) -ConnectionUri https://$ip/powershell -ConfigurationName Microsoft.Exchange -SessionOption (New-PSSessionOption -SkipCNCheck -SkipCACheck)
Import-PSSession $Session
when i pass user name and password to session variable it's throw an error
how can i pass this two value so that can import exchange management session to powershell
try this
$userid = $email.text
$password = $Password.text | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList $userid, $password
$Session = New-PSSession -Authentication default -Credential $credential -ConnectionUri https://$ip/powershell -ConfigurationName Microsoft.Exchange -SessionOption (New-PSSessionOption -SkipCNCheck -SkipCACheck)
Import-PSSession $Session

Import-Pssession is not importing cmdlets when used in a custom module

I have a PowerShell script/function that works great when I use it in my PowerShell profile or manually copy/paste the function in the PowerShell window.
I'm trying to make the function accessible to other members of my team as a module. I want to have the module stored in a central place so we can all add it to our PSModulePath.
Here is a copy of the basic function:
Function Connect-O365{
$o365cred = Get-Credential username#domain.onmicrosoft.com
$session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365cred -Authentication Basic -AllowRedirection
Import-PSSession $session365 -AllowClobber
}
If I save this function in my PowerShell profile it works fine. I can dot source a *.ps1 script with this function in it and it works as well.
The issue is when I save the function as a *.psm1 PowerShell script module. The function runs fine but none of the exported commands from the Import-PSSession are available. I think this may have something to do with the module scope.
I'm looking for suggestions on how to get around this.
EDIT
When I create the following module and run Connect-O365 the imported cmdlets will not be available.
$scriptblock = {
Function Connect-O365 {
$o365cred = Get-Credential username#domain.onmicrosoft.com
$session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $o365cred -Authentication Basic -AllowRedirection
Import-PSSession $session365 -AllowClobber
}
}
New-Module -Name "Office 365" -ScriptBlock $scriptblock
When I import the next module without the Connect-O365 function the imported cmdlets are available.
$scriptblock = {
$o365cred = Get-Credential username#domain.onmicrosoft.com
$session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $o365cred -Authentication Basic -AllowRedirection
Import-PSSession $session365 -AllowClobber
}
New-Module -Name "Office 365" -ScriptBlock $scriptblock
This appears to be a scope issue of some sort, just not sure how to get around it.
With some assistance from TechNet I was able to modify the script module so it worked the way I expected.
function Connect-O365 {
$o365cred = Get-Credential username#domain.onmicrosoft.com
$session365 = New-PSSession `
-ConfigurationName Microsoft.Exchange `
-ConnectionUri "https://ps.outlook.com/powershell/" `
-Credential $o365cred `
-Authentication Basic `
-AllowRedirection
Import-Module (Import-PSSession $session365 -AllowClobber) -Global
}
TechNet Post