我期待在 " /强 " 电动 Shell 中执行一个 " 强 " 电动 Shell 供应商。
我不断想,如果我只是定义类型, 然后把它们输入我的会话(进口模块), 我应该可以让它们可用。
例如,这个“强”并不起作用 强 ',而是沿着我想执行的道路前进。
我显然错过了相当多... 有人知道这是否可能吗?
# EnvironmentProvider.ps1
$reference_assemblies = (
"System.Management.Automation, Version=1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
# "System.Configuration.Install, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
)
$source = @"
namespace Providers
{
using System.Management.Automation;
using System.Management.Automation.Provider;
[CmdletProvider("Environments", ProviderCapabilities.None)]
public class EnvironmentProvider : DriveCmdletProvider
{
protected override PSDriveInfo NewDrive(PSDriveInfo drive)
{
return new EnvironmentDriveInfo(drive);
}
protected override object NewDriveDynamicParameters()
{
return base.NewDriveDynamicParameters();
}
}
public class EnvironmentDriveInfo : PSDriveInfo
{
public EnvironmentDriveInfo(PSDriveInfo driveInfo) : base(driveInfo)
{
}
}
}
"@
# -ea silentlycontinue in case its already loaded
#
add-type -referencedassemblies $referenced_assemblies -typedefinition $source -language CSharp -erroraction silentlycontinue
进口模块后, 我尝试创建驱动器“ 环境 ” :
new-psdrive -psprovider Environments -name "Environments" -root ""
存在错误 :
New-PSDrive : Cannot find a provider with the name Environments .
假设提供商实际工作,也许可以让它返回一份环境清单:dev、qa、中转、生产。
那么我想通过下列方式重新使用这个:
c:adminlib>import-module .EnvironmentProvider.ps1
c:adminlib>environments:
environments:>ls
dev
qa
staging
production
environments:> cd production
environmentsproduction> [execute actions against production]
environmentsproduction:> cd dev
environmentsdev:> [execute actions against dev, etc]