2018-02-13 09:23:53 -08:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
2016-07-29 13:02:49 -07:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
using System;
|
2019-04-30 10:25:11 +05:00
|
|
|
using System.Collections;
|
2016-03-30 14:20:52 -07:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.IO;
|
2019-04-30 10:25:11 +05:00
|
|
|
using System.Management.Automation;
|
2016-03-30 14:20:52 -07:00
|
|
|
using System.Management.Automation.Internal;
|
2019-04-30 10:25:11 +05:00
|
|
|
using System.Management.Automation.Remoting;
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
namespace Microsoft.PowerShell.Commands
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Test-PSSessionConfigurationFile command implementation
|
2017-01-16 13:31:14 -08:00
|
|
|
///
|
2016-03-30 14:20:52 -07:00
|
|
|
/// See Declarative Initial Session Config (DISC)
|
|
|
|
|
/// </summary>
|
2016-08-23 21:21:31 -04:00
|
|
|
[Cmdlet(VerbsDiagnostic.Test, "PSSessionConfigurationFile", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217039")]
|
2016-03-30 14:20:52 -07:00
|
|
|
[OutputType(typeof(bool))]
|
|
|
|
|
public class TestPSSessionConfigurationFileCommand : PSCmdlet
|
|
|
|
|
{
|
|
|
|
|
#region Parameters
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The output path for the generated file...
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0, ValueFromPipelineByPropertyName = true)]
|
|
|
|
|
public string Path
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
get { return _path; }
|
2018-12-23 22:20:06 -08:00
|
|
|
|
2016-07-29 13:02:49 -07:00
|
|
|
set { _path = value; }
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
2018-12-23 22:20:06 -08:00
|
|
|
|
2016-07-29 13:02:49 -07:00
|
|
|
private string _path;
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Overrides
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override void ProcessRecord()
|
|
|
|
|
{
|
|
|
|
|
ProviderInfo provider = null;
|
|
|
|
|
Collection<string> filePaths;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (this.Context.EngineSessionState.IsProviderLoaded(Context.ProviderNames.FileSystem))
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
filePaths = SessionState.Path.GetResolvedProviderPathFromPSPath(_path, out provider);
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
filePaths = new Collection<string>();
|
2016-07-29 13:02:49 -07:00
|
|
|
filePaths.Add(_path);
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (ItemNotFoundException)
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
string message = StringUtil.Format(RemotingErrorIdStrings.PSSessionConfigurationFileNotFound, _path);
|
2016-03-30 14:20:52 -07:00
|
|
|
FileNotFoundException fnf = new FileNotFoundException(message);
|
|
|
|
|
ErrorRecord er = new ErrorRecord(fnf, "PSSessionConfigurationFileNotFound",
|
2016-07-29 13:02:49 -07:00
|
|
|
ErrorCategory.ResourceUnavailable, _path);
|
2016-03-30 14:20:52 -07:00
|
|
|
WriteError(er);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure that the path is in the file system - that's all we can handle currently...
|
|
|
|
|
if (!provider.NameEquals(this.Context.ProviderNames.FileSystem))
|
|
|
|
|
{
|
|
|
|
|
// "The current provider ({0}) cannot open a file"
|
2016-07-29 13:02:49 -07:00
|
|
|
throw InterpreterError.NewInterpreterException(_path, typeof(RuntimeException),
|
2016-03-30 14:20:52 -07:00
|
|
|
null, "FileOpenError", ParserStrings.FileOpenError, provider.FullName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure at least one file was found...
|
|
|
|
|
if (filePaths == null || filePaths.Count < 1)
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
string message = StringUtil.Format(RemotingErrorIdStrings.PSSessionConfigurationFileNotFound, _path);
|
2016-03-30 14:20:52 -07:00
|
|
|
FileNotFoundException fnf = new FileNotFoundException(message);
|
|
|
|
|
ErrorRecord er = new ErrorRecord(fnf, "PSSessionConfigurationFileNotFound",
|
2016-07-29 13:02:49 -07:00
|
|
|
ErrorCategory.ResourceUnavailable, _path);
|
2016-03-30 14:20:52 -07:00
|
|
|
WriteError(er);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filePaths.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
// "The path resolved to more than one file; can only process one file at a time."
|
|
|
|
|
throw InterpreterError.NewInterpreterException(filePaths, typeof(RuntimeException),
|
|
|
|
|
null, "AmbiguousPath", ParserStrings.AmbiguousPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string filePath = filePaths[0];
|
|
|
|
|
ExternalScriptInfo scriptInfo = null;
|
|
|
|
|
string ext = System.IO.Path.GetExtension(filePath);
|
|
|
|
|
if (ext.Equals(StringLiterals.PowerShellDISCFileExtension, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
// Create a script info for loading the file...
|
|
|
|
|
string scriptName;
|
|
|
|
|
scriptInfo = DISCUtils.GetScriptInfoForFile(this.Context, filePath, out scriptName);
|
|
|
|
|
|
|
|
|
|
Hashtable configTable = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
configTable = DISCUtils.LoadConfigFile(this.Context, scriptInfo);
|
|
|
|
|
}
|
|
|
|
|
catch (RuntimeException e)
|
|
|
|
|
{
|
|
|
|
|
WriteVerbose(StringUtil.Format(RemotingErrorIdStrings.DISCErrorParsingConfigFile, filePath, e.Message));
|
|
|
|
|
WriteObject(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (configTable == null)
|
|
|
|
|
{
|
|
|
|
|
WriteObject(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DISCUtils.ExecutionPolicyType = typeof(ExecutionPolicy);
|
|
|
|
|
WriteObject(DISCUtils.VerifyConfigTable(configTable, this, filePath));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string message = StringUtil.Format(RemotingErrorIdStrings.InvalidPSSessionConfigurationFilePath, filePath);
|
|
|
|
|
InvalidOperationException ioe = new InvalidOperationException(message);
|
|
|
|
|
ErrorRecord er = new ErrorRecord(ioe, "InvalidPSSessionConfigurationFilePath",
|
2016-07-29 13:02:49 -07:00
|
|
|
ErrorCategory.InvalidArgument, _path);
|
2016-03-30 14:20:52 -07:00
|
|
|
ThrowTerminatingError(er);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|