New-DlpComplianceRule: parameter 'AdvancedRule'. Cannot convert value to type System.String

415 Views Asked by At

I am trying to follow MS article, https://learn.microsoft.com/en-us/powershell/module/exchange/new-dlpcompliancerule?view=exchange-ps

Example 3

$data = Get-Content -Path "C:\Data\Sensitive Type.txt" -ReadCount 0
New-DLPComplianceRule -Name "Contoso Rule 1" -Policy "Contoso Policy 1" -AdvancedRule $data -NotifyUer

But getting an error saying that $data is not string. My colleagues PC works fine but mine requires out-string in the first line. What is the difference? Any advice is appreciated.

Thank you,

1

There are 1 best solutions below

2
Sridevi On

I agree with @iRon, Get-Content cmdlet returns an array of strings for each line.

But -AdvancedRule parameter of the New-DLPComplianceRule cmdlet expects a single string as input.

When I ran the same code in my environment, I got same error saying Connot convert value to type System.String:

Connect-IPPSSession
$data = Get-Content -Path "C:\Data\Sensitive Type.txt" -ReadCount 0
New-DLPComplianceRule -Name "Contoso Rule 1" -Policy "Contoso Policy 1" -AdvancedRule $data -NotifyUer

Response:

enter image description here

To resolve the error, you need to add Out-String at the end that converts the array of strings returned by the Get-Content cmdlet to a single string.

When I added Out-String at the end, I got the response successfully like below:

$data = Get-Content -Path "C:\Data\Sensitive Type.txt" -ReadCount 0 | Out-String
New-DLPComplianceRule -Name "Contoso Rule 1" -Policy "Contoso Policy 1" -AdvancedRule $data -NotifyUser [email protected]

Response:

enter image description here

To confirm that, I checked the same in Microsoft Purview Portal where Data loss prevention compliance rule named Contoso Rule 1 created successfully:

enter image description here

If it's working in your colleagues PC without adding Out-String, verify whether you are using different version of PowerShell compared to them by running below command:

$PSVersionTable.PSVersion

enter image description here