我想在我可以更新的文件中创建一组别名,然后调用别名aa
以便文件执行,并在当前会话中为我提供新的别名。最终我希望在PS启动时自动提供这些别名,所以我使用C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
将它们放入。现在它看起来像:
$psdir="C:\Users\Administrator\Documents\WindowsPowerShell"
function Reload-Profile{
# . ${profile} DOES NOT WORK
# Write-Host ${profile}
# Write-Host $profile
# powershell $profile DOES NOT WORK
# Get-ChildItem "${psdir}\*.ps1" | %{.$_} DOES NOT WORK
Write-Host "Custom PowerShell Environment Loaded"
}
function Edit-Profile
{
powershell_ise.exe $profile
}
Set-Alias show Get-ChildItem
Set-Alias show2 Get-ChildItem
Set-Alias aa Reload-Profile
Set-Alias ep Edit-Profile
我该怎么做才能在启动时加载别名,但我还可以使用aa
别名更新它们并让它们进入当前会话?
答案 0 :(得分:0)
如果包含以下代码的评论的原作者决定将其作为答案发布,只需在此答案中添加评论即可将其删除。因为已经两天了,我真的不指望他。同时,这应该让人们更好地了解实际发生的事情。
public MyDayOfWeek Days { get; set; }
public string DaysFriendlyName => this.Days.ToFriendlyName();`
public enum MyDayOfWeek
{
Sunday = DayOfWeek.Sunday,
Monday = DayOfWeek.Monday,
// ..
SunTilFir = DayOfWeek.Sunday + DayOfWeek.Monday + DayOfWeek.Tuesday,//+...
public static string ToFriendlyName(this MyDayOfWeek days)
{
switch (days)
{
case MyDayOfWeek.SunTilFir:
return @Resources.FormResources.SunTilFri;
为了更好的可读性/突出显示C#代码独立:
# https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/add-type
# Create a new cmdlet Reload-Profile using C# code and import it
Add-Type '
using System.Management.Automation;
using System.Management.Automation.Runspaces;
// https://msdn.microsoft.com/en-us/library/dd878294(v=vs.85).aspx
[Cmdlet("Reload", "Profile")]
public class ReloadProfileCmdlet : PSCmdlet {
protected override void EndProcessing()
{
// https://msdn.microsoft.com/en-us/library/ms568378(v=vs.85).aspx
// Runs $profile without parameters in the current context and displays the output and error
InvokeCommand.InvokeScript(". $profile", false, PipelineResultTypes.Output | PipelineResultTypes.Error, null);
}
}' -PassThru | Select -First 1 -ExpandProperty Assembly | Import-Module;
# Setup an alias for the new cmdlet
Set-Alias aa Reload-Profile
答案 1 :(得分:0)
您的代码存在的问题是,Reload-Profile
是一个函数,当您调用它时,它将为自己创建新的范围。然后,当您调用. $profile
时,它不会为配置文件创建新范围,但仍会在Reload-Profile
范围内调用它。因此,当Reload-Profile
结束时,范围将被丢弃。因此,如果使用别名,还需要使用点调用运算符调用Reload-Profile
:. Reload-Profile
或. aa
。
我认为,你真正的问题是"如何以某种方式发出aa
命令,这不需要使用点调用操作符?"
答案是使用已编译的cmdlet而不是PowerShell函数,因为PowerShell不会为cmdlet调用创建新的范围。然后,该cmdlet可以在当前范围内调用. $profile
。
Add-Type @‘
using System.Management.Automation;
using System.Management.Automation.Runspaces;
[Cmdlet("Reload", "Profile")]
public class ReloadProfileCmdlet : PSCmdlet {
protected override void EndProcessing() {
InvokeCommand.InvokeScript(
". $profile",
false,
PipelineResultTypes.Output | PipelineResultTypes.Error,
null
);
}
}
’@ -PassThru | Select -First 1 -ExpandProperty Assembly | Import-Module
Set-Alias aa Reload-Profile
P.S。
我建议您使用不同的动词而不是Reload
,因为Reload
未包含在推荐动词列表中,因此当Import-Module
导入Reload-Profile
时,@Theme("mytheme")
public class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
layout.setSpacing(true);
setContent(layout);
//cache the beans
ArrayList<MyBean> beans = getBeans();
BeanItemContainer container = new BeanItemContainer<>(MyBean.class, beans);
Table table = new Table();
table.setSelectable(true);
table.setImmediate(true);
table.setWidth("200px");
table.setPageLength(5);
table.setContainerDataSource(container);
//select programmatically
table.select(beans.get(1));//this is the key idea! Provide the same bean from cache, for selection.
layout.addComponent(table);
}
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
public class MyBean {
private int id;
private String field;
public MyBean(int id, String field) {
this.id = id;
this.field = field;
}
public int getId() {
return id;
}
public String getField() {
return field;
}
}
public ArrayList<MyBean> getBeans() {
ArrayList<MyBean> beans = new ArrayList<>();
MyBean bean = new MyBean(1, "Vikrant");
beans.add(bean);
bean = new MyBean(2, "John");
beans.add(bean);
bean = new MyBean(3, "Rahul");
beans.add(bean);
return beans;
}
会发出警告你的会议。