gpt4 book ai didi

powershell - 使用 doskey 宏在 PowerShell 中设置别名

转载 作者:行者123 更新时间:2023-12-01 01:54:24 27 4
gpt4 key购买 nike

这可能吗?我试过this blog ,但它不起作用。
我有一个大约 50 个左右的宏文件 doskey宏,用于cmd.exe .

我运行类似:

doskey /exename=powershell.exe /macrofile=C:\macrofile.txt

或者
doskey /exename=powershell.exe blah=echo blah

但尝试命令 blah给出一个错误:

blah : The term 'blah' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.



如果我用 doskey /macros:powershell.exe 列出它们但是,它们都在那里。

有没有办法可以在 PowerShell 中创建 50 个别名来映射到这些宏? Set-Alias cmdlet 没有多大帮助,因为这些命令比调用简单的 PowerShell cmdlet 更复杂。有些帖子建议在 .ps1 脚本中创建函数,但是,只有当我为文件中的 50 个宏中的每一个手动编写 50 个不同的函数时,这才有效。此外,这将涉及维护两个文件 - 用于 cmd 的宏文件和将这些文件转换为函数的 PowerShell 脚本。

最佳答案

PSReadLine模块(随 PowerShell 5.0 或更高版本提供)与 doskey 不兼容,它依赖于 native 控制台输入功能。我在 Windows 10/PowerShell 5 上进行了测试:

PS C:\> Remove-Module PSReadLine
PS C:\> doskey /exename=powershell.exe g=Get-Location
PS C:\> g

Path
----
C:\

关于powershell - 使用 doskey 宏在 PowerShell 中设置别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41733192/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com