试图在powershell中将具有多个命令的函数转换为单个别名?

问题描述 投票:0回答:1

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

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即可立即为要启动的所有命令设置别名。

windows powershell powershell-v2.0 alias powershell-v3.0
1个回答
1
投票

您遇到的这个问题是尝试将数组放在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
© www.soinside.com 2019 - 2024. All rights reserved.