试图将具有多个命令的函数变成 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

我也尝试过在单独的行上没有管道(这是在函数内),只是尝试了 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"

您收到的错误是因为 -名称 "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