Import exchange management session into powershell - 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

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.

Remote execution on Exchange server failed

I want to remote enable the email-address-policy for a single mailbox on a exchange server (2010).
I can do this:
$samaccountname = $args[0] # gets sam from command line
$EncryptedPassword = Get-Content -Path "C:\temp\password.txt"
$SecurePassword = ConvertTo-SecureString -String $EncryptedPassword
$Credential = New-Object System.Management.Automation.PSCredential "xyzdom\sco_admin", $SecurePassword
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://xyzexcas01/PowerShell/ -Authentication Kerberos -Credential $Credential
Import-PSSession $Session -AllowClobber -CommandName Set-Mailbox
Get-Mailbox -Identity $samaccountname | Set-Mailbox -EmailAddressPolicyEnabled $True
Remove-PSSession $Session
It works if I open a powershell as administrator on the Orchestrator server. Then it executes the command on the exchange server as it should.
But the script does not work if Orchestrator tries to execute it. I do not know what settings Orchestrator uses when executing it. But I have a similar script, which is working with Orchestrator.
$samaccountname = $args[0] # gets sam from command line
$EncryptedPassword = Get-Content -Path "C:\temp\password.txt"
$SecurePassword = ConvertTo-SecureString -String $EncryptedPassword
$Credential = New-Object System.Management.Automation.PSCredential "xyzdom\sco_admin", $SecurePassword
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://xyzexcas01/PowerShell/ -Authentication Kerberos -Credential $Credential
Import-PSSession $Session -AllowClobber -CommandName enable-mailbox
$username_param = $samaccountname
$emailalias_param = $samaccountname
Invoke-Command -ArgumentList $username_param,$emailalias_param –session $Session -scriptblock {
param($username_exc, $alias_exc)
Enable-Mailbox -Identity $username_exc -Alias $alias_exc -DomainController 'xyzdc01.zfpdom.zfp'
}
Remove-PSSession $Session
This script makes a new mailbox. It is working.
Can anyone show me a solution for the first script? I am totally new in powershell so I can't figure it out. Maybe someone can change my first script to do it with this Invoke-Command scriptblock. I am sure, then it will work.
Thank you.
Greetings
Replace the commands inside the invoke-command, and the variables accordingly. Also, add the get-mailbox commandlet to the session. I am not able to try it, though, so I added the -verbose and -whatif switches as a failsafe. Note that if the samaccountname variable is empty, the Set-Mailbox will run on all mailboxes. The script can be tested standalone before running it in the Orchestrator.
$samaccountname = $args[0] # gets sam from command line
$EncryptedPassword = Get-Content -Path "C:\temp\password.txt"
$SecurePassword = ConvertTo-SecureString -String $EncryptedPassword
$Credential = New-Object System.Management.Automation.PSCredential "xyzdom\sco_admin", $SecurePassword
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://xyzexcas01/PowerShell/ -Authentication Kerberos -Credential $Credential
Import-PSSession $Session -AllowClobber -CommandName Set-Mailbox,Get-Mailbox
Invoke-Command -ArgumentList $samaccountname –session $Session -scriptblock {
param($username_exc)
Get-Mailbox -Identity $username_exc| Set-Mailbox -EmailAddressPolicyEnabled $True -verbose -whatif # remove the -whatif to perform changes
}
Remove-PSSession $Session
Another working solution I have found out:
#Parameter Laden
$samaccountname = $args[0] # $samaccountname wird übergeben
$EncryptedPassword = Get-Content -Path "C:\temp\password.txt"
$SecurePassword = ConvertTo-SecureString -String $EncryptedPassword
$Credential = New-Object System.Management.Automation.PSCredential "xyzdom\sco_admin", $SecurePassword
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://xyzexcas01/PowerShell/ -Authentication Kerberos -Credential $Credential
Import-PSSession $Session -AllowClobber -CommandName Set-Mailbox,Get-Mailbox
Get-Mailbox -Identity $samaccountname | Set-Mailbox -EmailAddressPolicyEnabled $True
Remove-PSSession $Session

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

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-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