mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-24 02:37:06 +00:00
* Update TestService * Update WebListener * Update Controllers * Update ExpTest * Update MyApp * Update Logic * Update Logic * Update MyApp * Update Microsoft.Management.Infrastructure.CimCmdlets * Update Microsoft.PowerShell.Commands.Diagnostics * Update Microsoft.PowerShell.ScheduledJob * Update Microsoft.WSMan.Management * Update Microsoft.WSMan.Runtime * Update ResGen * Update TypeCatalogGen * Update commands * Update Eventing * Update Reader * Update utility * Update ShowCommand * Update trace * Update WebCmdlet * Update Common * Update CoreCLR * Update common * Update format-object * Update format-wide * Update out-file * Update out-printer * Update out-string * Update OutGridView * Update LocalAccounts * Update Commands * Update security * Update CoreCLR * Update DscSupport * Update engine * Update help * Update logging * Update namespaces * Update security * Update utils * Update config * Update perfCounters * Update tracing * Update cmdletization * Update other * Update cim * Update xml * Update CoreCLR * Update common * Update DefaultFormatters * Update out-console * Update out-textInterface * Update DisplayDatabase * Update Utilities * Update COM * Update ComInterop * Update CommandCompletion * Update debugger * Update ExperimentalFeature * Update hostifaces * Update interpreter * Update lang * Update Modules * Update parser * Update runtime * Update client * Update commands * Update common * Update fanin * Update server * Update WireDataFormat * Update Binding * Update Operations * Update interface * Update cmdletization * Update cim * Update management * Update WindowsTaskbarJumpList * Update msh
141 lines
5.3 KiB
C#
141 lines
5.3 KiB
C#
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
|
|
using System;
|
|
using System.Management.Automation;
|
|
using System.Management.Automation.Remoting;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Collections;
|
|
using System.Management.Automation.Internal;
|
|
|
|
namespace Microsoft.PowerShell.Commands
|
|
{
|
|
/// <summary>
|
|
/// Test-PSSessionConfigurationFile command implementation
|
|
///
|
|
/// See Declarative Initial Session Config (DISC)
|
|
/// </summary>
|
|
[Cmdlet(VerbsDiagnostic.Test, "PSSessionConfigurationFile", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217039")]
|
|
[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
|
|
{
|
|
get { return _path; }
|
|
|
|
set { _path = value; }
|
|
}
|
|
|
|
private string _path;
|
|
|
|
#endregion
|
|
|
|
#region Overrides
|
|
|
|
/// <summary>
|
|
/// </summary>
|
|
protected override void ProcessRecord()
|
|
{
|
|
ProviderInfo provider = null;
|
|
Collection<string> filePaths;
|
|
|
|
try
|
|
{
|
|
if (this.Context.EngineSessionState.IsProviderLoaded(Context.ProviderNames.FileSystem))
|
|
{
|
|
filePaths = SessionState.Path.GetResolvedProviderPathFromPSPath(_path, out provider);
|
|
}
|
|
else
|
|
{
|
|
filePaths = new Collection<string>();
|
|
filePaths.Add(_path);
|
|
}
|
|
}
|
|
catch (ItemNotFoundException)
|
|
{
|
|
string message = StringUtil.Format(RemotingErrorIdStrings.PSSessionConfigurationFileNotFound, _path);
|
|
FileNotFoundException fnf = new FileNotFoundException(message);
|
|
ErrorRecord er = new ErrorRecord(fnf, "PSSessionConfigurationFileNotFound",
|
|
ErrorCategory.ResourceUnavailable, _path);
|
|
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"
|
|
throw InterpreterError.NewInterpreterException(_path, typeof(RuntimeException),
|
|
null, "FileOpenError", ParserStrings.FileOpenError, provider.FullName);
|
|
}
|
|
|
|
// Make sure at least one file was found...
|
|
if (filePaths == null || filePaths.Count < 1)
|
|
{
|
|
string message = StringUtil.Format(RemotingErrorIdStrings.PSSessionConfigurationFileNotFound, _path);
|
|
FileNotFoundException fnf = new FileNotFoundException(message);
|
|
ErrorRecord er = new ErrorRecord(fnf, "PSSessionConfigurationFileNotFound",
|
|
ErrorCategory.ResourceUnavailable, _path);
|
|
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",
|
|
ErrorCategory.InvalidArgument, _path);
|
|
ThrowTerminatingError(er);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|