如何在 PowerShell 中定义子例程
例如,在 C# 中,RemoveAllFilesByExtenstion
子例程可以像这样声明:
void RemoveAllFilesByExtenstion(string targetFolderPath, string ext)
{
...
}
并像这样使用:
RemoveAllFilesByExtenstion("C:\Logs\", ".log");
如何从 PowerShell 脚本文件 (ps1) 中定义和调用具有相同签名的子例程?
In C# a RemoveAllFilesByExtenstion
subroutine could be, for example, decleard like this:
void RemoveAllFilesByExtenstion(string targetFolderPath, string ext)
{
...
}
and used like:
RemoveAllFilesByExtenstion("C:\Logs\", ".log");
How can I defne and call a subroutine with the same signature from a PowerShell script file (ps1)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其转换为 PowerShell 非常简单:
但是调用必须使用空格分隔的参数,但不需要引号,除非字符串中存在 PowerShell 特殊字符:
OTOH,如果该函数指示您想要执行的操作,则可以是在 PowerShell 中轻松完成:
Pretty simple to convert this to PowerShell:
But the invocation has to use space separated args but doesn't require quotes unless there's a PowerShell special character in the string:
OTOH, if the function is indicative of what you want to do, this can be done in PowerShell easily:
PowerShell 中没有子例程,您需要一个函数:
调用它:
如果该函数不返回任何值,请确保捕获从函数内的命令返回的任何结果。
There are no subroutines in PowerShell, you need a function:
To invoke it :
If you don't the function to return any value make sure you capture any results returned from the commands inside the function.