Riaad
Riaad

Reputation: 1

Powershell - Office 365 security and compliance

I am trying to get this PowerShell script to auto generate the key parts of the command to run with minimal interaction.

Script as follows:

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

#Create Search with parameters
$SearchName = Read-Host "Enter Search name"
$date = Get-Date -format "MM/dd/yyyy"
$Subject = Read-Host "Enter Subject"
New-ComplianceSearch -Name "$SearchName" -ExchangeLocation all -ContentMatchQuery "'sent>=$date AND sent<=$date AND subject:"$Subject"'"

Error...

New-ComplianceSearch -Name "$SearchName" -ExchangeLocation all -ContentMatchQuery "'sent>=$date AND sent<=$date AND subject:"$Subject"'" A positional parameter cannot be found that accepts argument 't''. + CategoryInfo : InvalidArgument: (:) [New-ComplianceSearch], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,New-ComplianceSearch + PSComputerName : nam01b.ps.compliance.protection.outlook.com

Upvotes: 0

Views: 1255

Answers (1)

Greg Blair
Greg Blair

Reputation: 1

you need to add the Powershell gravemarker ` in order to not have your double quotes break the string

this should work:

New-ComplianceSearch -Name "$SearchName" -ExchangeLocation all -ContentMatchQuery "'sent>=$date AND sent<=$date AND subject:`"$Subject`"'"

Upvotes: 0

Related Questions