I am trying to execute the following command in PowerShell:
Invoke-Command -ComputerName <computerName> -FilePath script.ps1 -Credential $cred
Using Invoke-Command I am executing a PowerShell script "script.ps1" providing credentials in $cred.
Is there any way I can use the passed variable $cred in the above Invoke-Command, in the script again without asking a user about credentials?
i.e. something like below:
script.ps1:
New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
You can have the script accept a credential parameter:
script.ps1:
Param([PSCredential]$Credential)
New-PSSession ... -Credential $Credential
and invoke it while passing $cred as an argument like this:
Invoke-Command -Computer $computer -FilePath script.ps1 -ArgumentList $cred -Credential $cred
Related
With the following command with my user admin and role impersonation can access to my account:
$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 -AllowClobber
Once the connection is made, can I switch to another account?
I am trying to create folders and filters to execute the commands using New-MailboxFolder but doesn't work directly from my account. It's possible?
Thanks
Yes you can change current user. You will create another script to do what you want to do as another user. Then in your current script use this:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Start-Process powershell.exe -Credential $UserCredential -NoNewWindow -ArgumentList "-noexit -command TheNewCreatedScriptFullPath.ps1"
I get connected to Exchange in the cloud by issuing a series of powershell commands:
$credential = new-object -typename ... -argumentList ...
$session = New-PSSession -configurationName ... -connectionUri ... -credential $credential ...
Import-PSSession $session ...
I can then issue commands to do things I need to do, e.g.,
get-mailbox | ? {$_.aliast -like "*[.]*"} | select alias
alias
-----
john.public
jane.doe
...
However, the cmdlets for obtaining a PSSession are long, and typing them is error-prone even if I could manage to memorize them correctly. So I saved all the three long command lines verbatim in a function:
function get-365session() {
$credential = new-object -typename ... -argumentList ...
$session = New-PSSession -configurationName ... -connectionUri ... -credential $credential ...
Import-PSSession $session ...
}
But it does not work out as expected:
PS> get-365session
ModuleType Version Name ExportedCommands
---------- ------- ---- -----------------
...
PS> get-mailbox
get-mailbox: The term 'get-mailbox' is not recognized as the name of a cmdlt, function, script file, ....
I thought the session was obtained but was gone with the function's "sub-shell" as soon as the function has completed its run. I therefore tried
PS> . get-365session
But it was to no avail.
Hope there is a way and someone can help me out. Many thanks!
You need to import the session in the current scope using Import-Module with the -Global flag.
Your Import-PSSession line should look like
Import-Module (Import-PSSession $session365 -AllowClobber) -Global
Here is a working example:
function Connect-O365{
$o365cred = Get-Credential username#domain.onmicrosoft.com
$session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $o365cred -Authentication Basic -AllowRedirection
Import-Module (Import-PSSession $session365 -AllowClobber) -Global
}
Connect-O365
Reference
This technet forum thread from 2012
Local functions and variables are not available in other sessions. However, you can use the following trick to specify your function as a script block:
Invoke-Command -Session $session -ScriptBlock ${Function:My-FunctionName}
The article I linked to above goes into more detail about more advanced use-cases for this, but as your script doesn't seem to require any parameters. Do note that this requires the use of Invoke-Command to run the code in the other session, from the session where the function is defined, so I'm not sure how, if you could, get the function body if you have already done Enter-PSSession and are currently in the remote session.
I'm trying to export a remote Microsoft Exchange session using Export-PSSession however when I do this none of the Exchange specific cmdlets have been imported when I import the module, only the Exchange specific counters work. This is what I have so far:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://my.exchange.server.name.and.domain/PowerShell/ -Authentication Kerberos -Credential administrator
Export-PSSession -Session $Session -OutputModule ExchangeCmdlets -CommandType All -CommandName * -AllowClobber -Force
Import-Module -name ExchangeCmdlets
Get-ExchangeServer
I feel like I'm close but missing something.
You can try -PSSession parameter in Import-Module cmdlet.
Import-Module -PSSession $Session
What are the modules that has to be imported for this error in powershell ?
The term 'remove-distributiongroupmember' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Connect to Exchange Online PowerShell
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
#Your code here
Remove-PSSession $Session
Connect to Exchange servers using remote PowerShell
Note: Substitute mailbox01.contoso.com for your own exchange server.
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mailbox01.contoso.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session
#Your code here
Remove-PSSession $Session
I am trying to perform some operations on Exchange online(Office 365) through powershell.
First I created a new powershell session and exported the modules to local as "o365", so that on any later operation I no need to use Import-PsSession to download the required modules
$cred = Get-Credential
$s = New-PSSession -ConfigurationName "Microsoft.Exchange" -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection
Export-PsSession -session $s -outputModule o365
Now, I am creating new session and importing the existing module "o365".
$cred = Get-Credential
$s = New-PSSession -ConfigurationName "Microsoft.Exchange" -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $cred -Authentication Basic -AllowRedirection
Import-Module o365
Get-DistributionGroup
While running the command "Get-DistributionGroup", powershell prompts me to enter the office 365 credentials once again. Is it possible to avoid entering the credentials once again? I don't want to use Import-PsSession, since it takes more time.
It's prompting because you're asking it to each time. I would set up $cred by creating a new object.
#Initiate Get-Credential cmdlet and store inputs
$getcred = Get-Credential
$username = $getcred.UserName
$password = $getcred.Password
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
If you place the above in its own function, you wont have the issue of re-defining the $getcred variable. (That to most is obvious, but thought I'd cover that base)