繁体   English   中英

试图在Powershell中将具有多个命令的功能转换为单个别名?

[英]Trying to make a function with multiple commands into a single alias in powershell?

这是代码和我得到的错误:

function CSheridanStruct {
    New-Item -Path "C:\Users\Admininistrator\" -Name "Sheridan" -ItemType "directory" |
        New-Item -Path "C:\Users\Admininistrators\Sheridan\" -ItemType "directory" -Name "SYST23551", "Notes"
}

Set-Alias Sheridan CSheridanStruct

Sheridan
New-Item : Cannot convert 'System.Object[]' to the type 'System.String' 
required by parameter 'Name'. Specified method is not supported.
At line:2 char:167
+ ... rators\Sheridan\" -ItemType "directory" -Name "SYST23551", "Notes" }
+                                                   ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-Item], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewItemCommand

我也尝试了在没有单独行的pipline(这在函数内)的情况下尝试了Set-Alias Sheridan CSheridanStruct相同的错误。 我尝试做Set-Alias -Name "Sheridan" -Value CSheridanStruct 输出相同。 我已经检查并工作了功能命令,并创建了目录。 我只需要为所有要启动的命令设置别名,只需在PowerShell中键入别名Sheridan。

您遇到的这个问题试图将数组放入new-item -name字段中

New-Item -Path "C:\Users\Admininistrators\Sheridan\" -ItemType "directory" -Name "SYST23551", "Notes"

您收到的错误是由于-Name“ SYST23551”,“ Notes”

其次,无需传递这些命令,因为它们彼此无关

这是您脚本的有效版本

function CSheridanStruct {
    New-Item -Path "C:\Users\Admininistrator\" -Name "Sheridan" -ItemType "directory"
    New-Item -Path "C:\Users\Admininistrator\Sheridan" -Name "SYST23551" -ItemType "directory"
    New-Item -Path "C:\Users\Admininistrator\Sheridan" -Name "Notes" -ItemType "directory"
}

Set-Alias Sheridan CSheridanStruct

Sheridan

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM