Powershell 将变量传递给函数将创建一个数组
我已经了解了Powershell中的Powershell返回值,但我无法理解以下新FolderFromName返回数组的原因-我希望返回一个值、一个路径或一个字符串: 还尝试将以下内容添加到新FolderFromPath,因为此函数似乎是问题所在或正在更改参数:Powershell 将变量传递给函数将创建一个数组,powershell,Powershell,我已经了解了Powershell中的Powershell返回值,但我无法理解以下新FolderFromName返回数组的原因-我希望返回一个值、一个路径或一个字符串: 还尝试将以下内容添加到新FolderFromPath,因为此函数似乎是问题所在或正在更改参数: [OutputType([string])] param( [string]$FolderPath ) 这是因为Powershell函数将返回管道上的所有内容,而不是仅使用return指定的内容 考虑 function New
[OutputType([string])]
param(
[string]$FolderPath
)
这是因为Powershell函数将返回管道上的所有内容,而不是仅使用return指定的内容 考虑
function New-FolderFromName($FolderName){
if($FolderName){
$CurrentFolder=Get-Location
$NewFolder=Join-Path $CurrentFolder -ChildPath $FolderName
$ret = New-FolderFromPath($NewFolder)
write-host "`$ret: $ret"
return $NewFolder
}
}
#output
PS C:\temp> New-FolderFromName 'foobar'
creating a new folder foobar...
$ret: C:\temp\foobar
C:\temp\foobar
请参见,新FolderFromPath返回了一个源于新项的值。消除额外返回值的最简单方法是通过管道将新项设置为null,如下所示
New-Item -Path $FolderPath -ItemType Directory |out-null
另请参见有关行为的信息
New-Item -Path $FolderPath -ItemType Directory |out-null