我想编写一个函数,以指定文件夹内的指定名称创建一个文件夹(如果不存在)。事实证明,根据调用New-Item函数返回不同的值。而且我不知道它与New-Item实际的关系。
$folderPath = "C:\tmp"
function CreateFolder([string] $name, [string] $parentFolder)
{
$path = "$parentFolder\$name"
if(!(Test-Path $path))
{
New-Item -path $parentFolder -name $name -itemtype Directory
}
return $path
}
$FOLDER_NAME = "folder1"
$destination = CreateFolder $FOLDER_NAME $folderPath
echo $destination.GetType()
Run Code Online (Sandbox Code Playgroud)
如果folder1不存在,它将返回:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
Run Code Online (Sandbox Code Playgroud)
除此以外:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
Run Code Online (Sandbox Code Playgroud)
除非作为参数Move-Item支持Object[],否则不会有问题-destination。
echo $destination 返回:
PSPath …Run Code Online (Sandbox Code Playgroud)