1

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,

Sridevi
  • 10,599
  • 1
  • 4
  • 17
Y.T
  • 23
  • 4
  • 1
    This is a common misunderstanding, [`Get-Content`](https://learn.microsoft.com/powershell/module/microsoft.powershell.management/get-content) doesn't indeed return a (single) string but an array of strings (`string[]`) for each line. Try: `Get-Content -Raw` or `$Data | Foreach-Object { Do-YourThingWith $_ }`. See also [PowerShell gotcha `#8`](https://stackoverflow.com/a/69644807/1701026) – iRon Jul 22 '23 at 06:22
  • Yes, the out put type is an array, that's what I figure too. but then this sample command from MS is not right? or they assume using certain version.. Thank you, – Y.T Jul 22 '23 at 10:39
  • Related: [`#4242` Consistently document a scalar -InputObject parameter as an implementation detail or make item-by-item processing cmdlets explicitly iterate over collections](https://github.com/PowerShell/PowerShell/issues/4242) – iRon Jul 22 '23 at 12:34
  • So it's depends on the version of Powershell? I don't really get it yet. – Y.T Jul 25 '23 at 11:27
  • I don't believe it is a PowerShell version issue (please add both versions to the question). If look to the [`-advancedrule` parameter for the `new-dlpcompliancerule` cmdlet](https://learn.microsoft.com/powershell/module/exchange/new-dlpcompliancerule?view=exchange-ps#-advancedrule), you will see that the input `Type` is `String` and if you check ($Data.GetType()`) you will see that this is: `String[]` (a String *Array*). Confirm these types on both your and your colleagues PC. Btw. It could also be a module (`get-Command New-DlpComplianceRule`) version difference. – iRon Jul 25 '23 at 12:15

1 Answers1

2

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 user@xxxxxxxxxxx.onmicrosoft.com

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

Sridevi
  • 10,599
  • 1
  • 4
  • 17
  • I will ask the colleague running that command and see what's the difference.. Thank you, – Y.T Jul 22 '23 at 10:41
  • Mine is 5.1.19041.3031, Colleague's one is 5.1.19041.2673. Just a revision difference..... – Y.T Jul 22 '23 at 12:26