Get-Date无法将null转换为类型“ System.Datetime”

时间:2019-08-14 07:28:16

标签: powershell

我有一个任务来构建一个工具,该工具将文件(jpeg,mov和heic)的创建时间设置为上次更改日期。我真的是Powershell的新手,就在几天前就开始了。一些代码是由我的老师写的。

$SingleOutput = $true 
$objShell = New-Object -ComObject Shell.Application
$Results = @() 
$folder = "C:\Users\keketz.SCANLOCAL\Desktop\Neuer Ordner"
$files = Get-ChildItem $folder
foreach ($file in $files) {
    $FullName = $folder + "\" + $file.name
    $Result = @()
    $FSItem = Get-Item $FullName

    if ($FSItem -is [System.IO.FileInfo]) {
        $FolderPath = [System.IO.Path]::GetDirectoryName($FSItem.FullName)
        $FileName = $FSItem.Name
        $objFolder = $objShell.Namespace($FolderPath)
        $obJFolderItem = $objFolder.ParseName($FileName)

        for ($attr = 0; $attr -le 2000; $attr++) {
            $attrName = $objFolder.GetDetailsOf($null, $attr)
            $attrValue = $objFolder.GetDetailsOf($obJFolderItem, $attr)
            if ($attrName -eq "Erstelldatum") {
                $Results += New-Object PSObject -Property([ordered]@{
                    ID    = $attr;
                    Name  = $attrName;
                    Value = $attrValue
                })
            }
        }
    }
    $datum = Get-Date $Results.Value
    return
    $Results.Value
    [System.IO.File]::SetLastWriteTime($datum)
}

运行此代码时,出现以下错误消息:

.creationtime : The term '.creationtime' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
At C:\Users\MyUser\Desktop\getFile-MetaData.ps1:25 char:14
+     $datum = .creationtime $Results.Value
+              ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.creationtime:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Get-Date : Cannot bind parameter 'Date' to the target. Exception setting "Date":
"Cannot convert null to type "System.DateTime"."
At C:\Users\keketz.SCANLOCAL\Desktop\getFile-MetaData.ps1:25 char:23
+     $datum = Get-Date $Results.Value
+                       ~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.GetDateCommand

1 个答案:

答案 0 :(得分:0)

我认为您过于复杂了。如果我对问题的理解正确,那么任务是在根文件夹中搜索具有某些扩展名的文件,对于找到的每个文件,将CreationTime属性更改为与LastWriteTime属性相同。 / p>

这应该做到:

# set this $justTesting variable to $false to actually make the changes.
# when $true, like below, the script simply shows what would happen in the console window
$justTesting = $true

$folder = "C:\Users\keketz.SCANLOCAL\Desktop\Neuer Ordner"

# get a list of FileInfo objects for files with any of these extensions: '.jpeg', '.jpg', '.mov', '.heic'
$files = Get-ChildItem -Path $folder -File | Where-Object { '.jpeg', '.jpg', '.mov', '.heic' -contains $_.Extension }
# check your PowerShell version. If it is below 3.0 use this instead:
# $files = Get-ChildItem -Path $folder | Where-Object { !$_.PsIsContainer -and '.jpeg', '.jpg', '.mov', '.heic' -contains $_.Extension }

# loop through the files list and set the CreationTime to the same date as the LastWriteTime
foreach ($file in $files) {
    # create a message for output on screen
    $message = "Setting CreationTime for file '{0}' from {1} to {2}" -f $file.FullName, $file.CreationTime, $file.LastWriteTime
    if ($justTesting) {
        Write-Host ("TEST: $message") -ForegroundColor Cyan
    }
    else {
        Write-Host $message
        $file.CreationTime = $file.LastWriteTime
    }
}

如果根文件夹$folder中有子文件夹,其中可能还有一些文件需要更改其CreationTime,则需要在-Recurse cmdlet上添加Get-ChildItem开关。 在这种情况下,您还可以使用-Include参数将命令简化为:

$files = Get-ChildItem -Path $folder -File -Recurse -Include '*.jpeg', '*.jpg', '*.mov', '*.heic'

希望有帮助