Skip to content

Commit 17f89f5

Browse files
author
Daniele Catanesi
committed
Added new post
1 parent 24c8ff4 commit 17f89f5

File tree

2 files changed

+93
-34
lines changed

2 files changed

+93
-34
lines changed
Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,46 @@
11
---
2-
title: "Cannot bind argument to parameter TokenExpiryTime because it is null - Error Message"
3-
excerpt: "When opening a PowerShell session to both on-prem and Exchange online in the same window cannot bing argument to parameter TokenExpiryTime because is null error message could be displayed. Let's explore how to solve this."
2+
title: "PowerShell - Get day of weekday"
3+
excerpt: "In this short post we will see how we can use PowerShell to get an integer representing the number associated with the day of the week for the current date."
44
categories:
55
- PowerShell
6-
- Exchange
7-
- Office 365
6+
- Howto
87

98
tags:
109
- PowerShell
11-
- Office365
12-
- Exchange
10+
- Tips
11+
- HowTo
1312

1413
toc: true
1514
header:
1615
teaser: "/assets/images/PowerShell_Logo.png"
1716
---
1817

19-
## Exchange Online Certificate Based authentication
18+
## PowerShell Get current date
2019

21-
Microsoft is, _finally_, disabling **basic authentication** (read username and password) in Exchange Online in favor of **Certificate Based authentication**.
20+
PowerShell natively supports getting the current, or specific, date via the following cmdlet:
2221

23-
Once this change is fully implemented, around mid February at least for some tenants, connecting via username and passwords to Exchange Online will not be possible anymore.
24-
25-
You can read my article on how to implement _Certificate Based authentication_ for Exchange Online [here](https://pscustomobject.github.io/powershell/office365/exchange/Exchange-Online-Certificate-Based-Authentication/).
26-
27-
As a result of this change I started updating one of our automations, responsible for the whole life-cycle of our mailboxes, to ditch old credential objects in favor of the more secure Certificate Authentication.
28-
29-
This is when I encountered the _cannot bind argument to parameter 'token expiry time' because it is null._ error message.
30-
31-
## Multiple PowerShell Exchange Sessions
32-
33-
When operating an hybrid environment it is pretty common to open, in the same window/session, a PowerShell connection to both Exchange on-prem and Exchange Online.
22+
```powershell
23+
Get-Date
24+
```
25+
Which, by default, will produce the following output:
3426

35-
This is required as part of the configuration, usually creation of the mailbox, takes place in on-prem for example via the _New-RemoteMailbox_ cmdlet while other parts of the configuration are performed directly online for example when delegating mailbox permissions.
27+
```powershell
28+
Monday, February 21, 2022 10:11:39 PM
29+
```
3630

37-
While debugging my workflow I have noticed that, while trying to retreive mailbox information from the on-prem server, an exception was being thrown
31+
Cmdlet supports methods which can be used to display/return the day of the week for the selected date
3832

3933
```powershell
40-
# Cmdlet I was running
41-
Get-RemoteMailbox -Identity $userUpn
42-
43-
# Part of the exception message
44-
Cannot bind argument to parameter 'token expiry time' because it is null.
34+
(Get-Date).DayOfWeek
4535
```
4636

47-
It took me a bit to figure this out as no exception was thrown during the connection *phase* either nor there was any other obvious pointer.
37+
There are situations where rather than returning a string with the day name it can be useful to return the **number** (1 to 7) associated with the day's name, this can easily be accomplished with the following command
4838

49-
When I was about to give up and open a ticket with Microsoft, which is usually as helpful as freezer in the North Pole, I discovered that establishing a connection to Exchange Online followed by a connection to the on-prem server was yielding the desired result. In my workflow I had this the other way around, first local Exchange and then Online service, which was causing the issue.
50-
51-
**Note:** I have experienced/tested this with version 2.0.4 and 2.0.5 of Exchange Online PowerShell module but other versions could be affected as well.
52-
{: .notice--warning}
39+
```powershell
40+
(Get-Date).DayOfWeek.value__
5341
54-
I did not dig deep into the root cause of the issue but plan to do this tomorrow and already sent my feedback to exocmdletpreview {at} service {dot} microsoft {dot} com but I doubt I will hear anything from that channel.
55-
I will anyhow open a ticket with support to at least have an official statement/clarification on this.
42+
# Output
43+
1
44+
```
5645

57-
As soon as I have any news I will update the post until then I hope you can find the information useful.
46+
This is specially handy when inserting data into SQL datbase supporting dates as *TinyInt* data types.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: "Exchange Online Management Module - Could not use the certificate for signing"
3+
excerpt: "When trying to establish a connection to Exchange Online Could not use the certificate for signing error message is displayed."
4+
categories:
5+
- PowerShell
6+
- Exchange
7+
- Office 365
8+
9+
tags:
10+
- PowerShell
11+
- Office365
12+
- Exchange
13+
14+
toc: true
15+
header:
16+
teaser: "/assets/images/PowerShell_Logo.png"
17+
---
18+
19+
## Exchange Online Certificate Based authentication
20+
21+
As I have written in my [previous post about TokenExpiry error message Microsoft is retiring ability to connect to Exchange Online via basic authentication](https://pscustomobject.github.io/powershell/exchange/office%20365/Cannot-bind-argument-to-parameter-Token-Expiry/).
22+
23+
You can read my article on how to implement _Certificate Based authentication_ for Exchange Online [here](https://pscustomobject.github.io/powershell/office365/exchange/Exchange-Online-Certificate-Based-Authentication/).
24+
25+
## Could not use the certificate for signing error message
26+
27+
Today while I was updating code for one of our automations I created a request for a new certificate to use for authentication purposes.
28+
29+
Once I deployed code to our test environment automation was failing the connection to Exchange Online with the following error
30+
31+
```powershell
32+
[System.Management.Automation.RuntimeException] One or more errors occurred.
33+
[Microsoft.Identity.Client.MsalClientException] Could not use the certificate for signing. See inner exception for details. Possible cause: this may be a known issue with apps build against .NET Desktop 4.6 or lower. Either target a higher version of .NET desktop - 4.6.1 and above, or use a different certificate type (non-CNG) or sign your own assertion as described at aka.ms/msal-net-signed-assertion.
34+
[System.Security.Cryptography.CryptographicException] Invalid provider type specified.
35+
````
36+
37+
Funnily enough the same certificate and cmdlets were working fine with PowerShell 7.
38+
39+
After quite some troubleshooting I've found out the problem was caused by the certificate's private key using [*Cryptography Next Generate (CNG)*](https://docs.microsoft.com/en-us/mem/configmgr/core/plan-design/network/cng-certificates-overview) template rather than RSA.
40+
41+
Not having direct access to the CA releasing the certificate I could not change this so I had to resort on either running the automation in PowerShell 7 or update the certificate itself.
42+
43+
Luckily this is easily done via OpenSSL. Let's see how.
44+
45+
## Convert Certificate private key from CNG to RSA
46+
47+
If you have installed Git, cygwin or Windows Subsystem for Linux you just need to fire a bash prompt and use the following commands:
48+
49+
```bash
50+
# Extract the public key from the cert
51+
OpenSSL pkcs12 -in "CNGCertificate.pfx" -nokeys -out "temp.cer"
52+
53+
# Extract the private key
54+
OpenSSL pkcs12 -in "CNGCertificate.pfx" -nocerts -out "temp.pem"
55+
56+
# Convert key to RSA
57+
OpenSSL rsa -inform PEM -in "temp.pem" -out "temp.rsa"
58+
59+
# Finally create a new pfx file
60+
OpenSSL pkcs12 -export -in "temp.cer" -inkey "temp.rsa" -out "RSACertificate.pfx"
61+
````
62+
63+
**Note:** In the above commands I am not using a password for the certificate as everything is local to my machine but a password is definitely *required* when exporting a certificate together with the private key.
64+
{: .notice--warning}
65+
66+
Once the new pfx file has been created all *temporary* certificates can be safely removed form the system and connection to Exchange Online will go through just fine.
67+
68+
Again if you can use PowerShell 7 you will not face this issue but in case you're stuck with version 5.1 and facing this error message hopefully this post can save you some headaches.
69+
70+
Full credit for the solution goes to this [StackOverFlow thread](https://stackoverflow.com/questions/22581811/invalid-provider-type-specified-cryptographicexception-when-trying-to-load-pri/34103154#34103154)

0 commit comments

Comments
 (0)