I call Powershell commend in c# to get file status. It works fine, but if I called Get-RMSTemplate before Get-AIPFileStatus Powershell.Invoke returns null.
My methods to call Powershell commands:
public async Task<IEnumerable<TemplateInfo>> GetRMSTemplates()
{
return await Task.Run(() =>
{
using (var rs = RunspaceFactory.CreateRunspace())
{
rs.Open();
using (var ps = PowerShell.Create())
{
ps.Runspace = rs;
ps.AddScript("$s =" + "Get-RMSTemplate;" + "$s;");
try
{
Collection<PSObject> psOutput = ps.Invoke();
var templates = psOutput.Where(p => p != null).Select(p => new TemplateInfo(p));
_loggingService.LogPowershellExecution(ps);
return templates;
}
catch (Exception ex)
{
_loggingService.Log(ex);
return null;
}
}
}
});
}
public async Task<IEnumerable<FileStatus>> GetFileStatuses(IEnumerable<string> files)
{
using (var rs = RunspaceFactory.CreateRunspace())
{
try
{
rs.Open();
using (var ps = PowerShell.Create(RunspaceMode.NewRunspace))
{
var result = await Task.Run(async () =>
{
using (token.Register(() =>
{
ps.Stop();
}, true))
{
ps.Runspace = rs;
string scriptContent = string.Join(", ", files.Select(f => $"\"{f}\""));
string script = "$s =" + string.Format(Constants.Commands.GetAIPFileStatusCommandTemplate, scriptContent) + "$s;";
ps.AddScript(script);
try
{
token.ThrowIfCancellationRequested();
var psOutput = ps.Invoke();
var statuses = psOutput.Select(p => new FileStatus(p));
await _loggingService.LogPowershellExecution(ps);
return statuses;
}
catch (Exception ex)
{
await _loggingService.Log(ex);
return null;
}
}
});
token.ThrowIfCancellationRequested();
return result;
}
}
catch
{
throw;
}
finally
{
rs.Close();
}
}
}
Command to get file status that will be built (for example): $s =Get-AIPFileStatus -path "C:\Users\User\Desktop\New Microsoft Word Document.docx" | Select FileName, IsLabeled, MainLabelName, SubLabelId, MainLabelId;$s;
Exception that occures:
The type initializer for 'Microsoft.InformationProtection.Powershell.AIP.Commandlets.AipBaseCmdlet' threw an exception.
Inner exception:
The application tried to incorrectly set a property when calling the Rights Management service. Contact the application support for further assistance. HRESULT: 0x80040210
UPDATE:
The same happens even in Powershell console:
If I called Get-RMSTemplate first, I get the following error while trying to define a file status:
Otherwise, if GetAIPFileStatus was called as first - the following error while GetRMSTemplate is being performed:
Why it happens?
User contributions licensed under CC BY-SA 3.0