Microsoft Powershell script running problem - powershell

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

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.

Cannot validate argument on parameter 'SourceMailbox'

Set-ExecutionPolicy RemoteSigned
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
$group = Read-Host 'What is the Group Mailbox Name?'
$shared = Read-Host 'What is the Shared Mailbox Name?'
$deletedGroup=get-mailbox -SoftDeletedMailbox -GroupMailbox -Filter {name -like "$group*"}
$targetuser=get-mailbox -Identity "$shared"
New-MailboxRestoreRequest -SourceMailbox $deletedGroup.DistinguishedName -TargetMailbox $targetuser.DistinguishedName -AllowLegacyDNMismatch -Verbose
Get-MailboxRestoreRequest
I get the following error returned:
Cannot validate argument on parameter 'SourceMailbox'. The argument is null. Provide a valid value for the argument, and then try running the command again.
+ CategoryInfo : InvalidData: (:) [New-MailboxRestoreRequest], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,New-MailboxRestoreRequest
+ PSComputerName : outlook.office365.com

Proxy setting for powershell connecting to outlook.office365

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.

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