【问题标题】:PowerShell New-Item output is inconsistent - Why?PowerShell 新项目输出不一致 - 为什么?
【发布时间】:2015-06-24 05:12:03
【问题描述】:

对于同一个 powershell 命令,我得到了不同的结果,我不知道为什么。

当它在函数中运行时,我得到输出 1,当我自己运行时,我得到输出 2

New-Item -Path C:\DEPL\nichdwww\deployments\Full\bob3 -type directory

输出 1

PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\DEPL\nichdwww\deployments\Full\bob3
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\DEPL\nichdwww\deployments\Full
PSChildName       : bob3
PSDrive           : C
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : True
Name              : bob3
Parent            : Full
Exists            : True
Root              : C:\
FullName          : C:\DEPL\nichdwww\deployments\Full\bob3
Extension         : 
CreationTime      : 6/23/2015 2:14:39 PM
CreationTimeUtc   : 6/23/2015 6:14:39 PM
LastAccessTime    : 6/23/2015 2:14:39 PM
LastAccessTimeUtc : 6/23/2015 6:14:39 PM
LastWriteTime     : 6/23/2015 2:14:39 PM
LastWriteTimeUtc  : 6/23/2015 6:14:39 PM
Attributes        : Directory
BaseName          : bob3
Mode              : d----

输出 2

Directory: C:\DEPL\nichdwww\deployments\Full


Mode                LastWriteTime     Length Name                                                                                                                         
----                -------------     ------ ----                                                                                                                         
d----         6/23/2015   2:45 PM            bob3

这里是函数

function ArchiveFullSolution($wsp)
{
    $ParentPath=Split-Path $wsp.DirectoryName -Parent
    $FullPath=$ParentPath+'\Full\'
    $Fullfilename=$FullPath+$wsp.Name
    #$Fullfilename

    #does file exist    
    if(Test-Path -path $Fullfilename)
    {
        #Make Full Archive folder
        #$script:Makefolder
        if($script:Makefolder)
        {
            #does folder exists
            $DayFormat=Get-Date -f yyyy-MM-dd
            if(Test-Path -path $FullPath$DayFormat)
            {
                write-host "folder $FullPath$DayFormat exists"
                $DayTimeFormat=Get-Date -f yyyy-MM-dd-hh-mm
                 write-host "Creating folder $FullPath$DayTimeFormat"
                New-Item -Path $FullPath$DayTimeFormat -type directory

                $script:Makefolder=$false
                $script:FullArchivePath=$FullPath+$DayTimeFormat
            }
            else
            {
                write-host "no folder exists"
                write-host "Creating folder $FullPath$DayFormat"
                New-Item -Path $FullPath$DayFormat -type directory 

                $script:Makefolder=$false
                $script:FullArchivePath=$FullPath+$DayFormat
            }
        }

        #move file into archive
        Move-Item $Fullfilename $script:FullArchivePath
        write-host "Moving file $($Fullfilename) to $FullArchivePath"        
    }
    #copy file into full
    Copy-Item $wsp.FullName $FullPath
    write-host "Copying file $($wsp.FullName) to $FullPath"
}

【问题讨论】:

  • 我得到了两者的第二个输出(即:如果我只是运行 cmd 或者如果我调用一个带有 cmd 的函数)。
  • 在我看来像Format-List *。 PowerShell 以某种方式显示对象...除非您覆盖它。

标签: powershell filesystems


【解决方案1】:

检查您的功能。很可能它首先输出另一个对象。在 PowerShell F&O(格式化和输出)引擎看到一种类型的对象后,它希望像该类型一样格式化之后的所有内容。如果它随后看到另一种类型的对象,它将回退到使用 Format-List IIRC。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多