How to connect PowerShell with CRM online? - powershell

I am writing a PowerShell script where it needs to be connected to CRM online.
I can connect to CRM using Connect-CrmOnline where it asks for credentials using a login box. I want to automate it. Please guide on how to do it.
Is there a way to connect using connection string? Please share sample code/ script. I am new to PowerShell.
I am using Microsoft.Xrm.Data.PowerShell
Thank you.

You can build a credentials object and use that in the script. For example:
$secpasswd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
You would then use it within your script with -credential $mycreds parameter
Ref: https://blogs.msdn.microsoft.com/koteshb/2010/02/12/powershell-how-to-create-a-pscredential-object/ and https://learn.microsoft.com/en-us/dynamics365/customer-engagement/developer/xrm-tooling/use-powershell-cmdlets-xrm-tooling-connect

Related

How to achieve an automated script that requires Connect-ExchangeOnline command with MFA?

I'm trying to create an automation which involves connecting to exchange online. I'm unable to achieve this because whenever I issue the Connect-Exchange command with valid credentials provided, it asks for MFA (multi-factor authentication) - which cannot be automated. How can I achieve an automation with this? Any ideas? Thanks!
$password = ConvertTo-SecureString 'MySecretPassword' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ('user#company.com', $password)
Connect-ExchangeOnline -Credential $credential
To expand on the point that Scepticalist made, you will do this by creating a Service Principal in AzureAD, assign it the appropriate rights in Exchange Online and then setup certificate based or client secrets based authentication. This approach replaces your existing interactive user that use for running scripts and doesn't involve MFA because it isn't a traditional user.
More information can be found here:
https://www.michev.info/Blog/Post/2997/connecting-to-exchange-online-powershell-via-client-secret-flow

Error when global variable in Orchestrator 2012 is encrypted

I'm trying to use some credentials so I can connect to MicrosoftTeams using powershell with orchestrator. I need a username and its password. The password is stored as a global variable, encrypted. When I try to connect to MicrosoftTeams it says that the password is incorrent. However, when I put the password hard coded there is no problem, and I can connect to Microsoft Teams.
Here is my code:
$Pass = ConvertTo-SecureString '{password}' -AsPlainText -Force
$admin ="admin#admin.com"
$cred = new-object -typename System.Management.Automation.PSCredential -argumentList $admin, $password
connect-MicrosoftTeams -credential $cred
Has someone had the same issue?
I think my problem was because I was using a PowerShell Script activity. It turns out that this activity can't read encrypted variables. To solve this problem I just enctypted the password and stored it in my C: drive, then I just got the content from that file. If someone wants the code for that, please ask.

Automating JiraPS-PowerShell Scripts with Task-Scheduler, Issue on Authentification

I have created a script to bring missed Phonecalls to Jira. For this, I am using the JiraPS-Powershell Module.
The scripts work, if I am run them PowerShell ISE. But after creating a Task in Windows Task-Scheduler, the authentification fails.
JiraPS creates a Websession (Microsoft.PowerShell.Commands.WebRequestSession) and it seems that this Session cannot be accessed via Task-Scheduler.
Is there a way to solve this problem?
(If it is just a Task-Scheduler issue, I am happy to change to a different solution)#
Thank you!
Further information:
How I know that the Session is still existing?
PS C:\FreePBXToJira> Get-JiraSession
Username WebSession
-------- ----------
XX Microsoft.PowerShell.Commands.WebRequestSession
I tried this before and after the scheduled Task and the Session is always available for me, but not for the Task-Scheduler
At least I found a solution, which makes it possible to run the Script via Task-Scheduler. Instead of using the saved session, I create a new session every time. The drawback is, that I have to put my credentials in the script
$secpasswd = ConvertTo-SecureString "myPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("myUsername", $secpasswd)
New-JiraSession -Credential $mycreds

How can I run Test-DbaNetworkLatency without password prompt?

I am running Test-DbaNetworkLatency -SqlCredential sa in powershell to measure sql server network latency. It prompts input password each time I run the command. I am planning to schedule a cronjob on my server to run this command regularly so I am looking for a way to avoid typing password each time. Can anyone tell me how I can achieve that in powershell?
You will need to create a credential object for the function.
The function info shows Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
$password = ConvertTo-SecureString "Password" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("sa", $password)
Test-DbaNetworkLatency -SqlCredential $creds

How to use powershell to deploy model into Analysis services without configure it to be http?

Hi I have been trying to deploy model to analysis service by using XMLA script. I used deployment wizard and it worked fine. However when I tried to use command Invoke-ASCmd to deploy model to my analysis server. It got a error of targetinovation which I figured out to be my credential argument error.
The command I used:
$user = "myemail#outlook.com"
$PWord = ConvertTo-SecureString -String "password" -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $user, $PWord
Invoke-ASCmd -InputFile AW.XMLA -Server asazure://southeastasia.asazure.windows.net/azurejenkins -Credential $Credential
But when I not use the -Credential argument it prompt the window email login which work fine. How do I use this command without prompting the user to enter email and password?
Solution to this problem.
I need to create webapp/api in active directory of azure. Copy its application id, tenant id and authentication key which you need to go to keys section in your app.
Go to SQL management server program which connected to your analysis server. Go to security and add app:# in manual entry (in case you don't see your app in the list).
Then use the same command I used in the question but change some components. Change user email to your app id and password to your authentication key. Add tenant id.
Add-AzureAnalysisServicesAccount -Credential $Credential -ServicePrincipal -TenantId $TenantId -RolloutEnvironment "xxxxxx.asazure.windows.net"
Invoke-ASCmd -InputFile xx.XMLA -Server asazure://southeastasia.asazure.windows.net/xxxxxx -Credential $Credential
Hope this script help someone who has the same problem as mine.