0

我想知道是否有人可以帮助我?我已经删除了一些我在网上找到的 powershell 脚本,它们可以创建从 $source 到 $destination 的快捷方式。但是,它似乎每次都会覆盖,我只希望它在 new 上创建一个 .lnk。

脚本的原始来源在这里,这是我当前的“非工作”脚本。我添加了以下内容,但它似乎不起作用。我想我需要以某种方式让它检查 $destination 然后如果 $file.lnk 不存在则继续

If ($status -eq $false) {($WshShell.fso.FileExists("$Destination") + "*.lnk")

完整脚本:

    function Create-ShortcutForEachFile {

    Param(
        [ValidateNotNullOrEmpty()][string]$Source,
        [ValidateNotNullOrEmpty()][string]$Destination,
        [switch]$Recurse
    )

    # set recurse if present
    if ($Recurse.IsPresent) { $splat = @{ Recurse = $true } }

    # Getting all the source files and source folder
    $gci = gci $Source @splat
    $Files = $gci | ? { !$_.PSisContainer }
    $Folders = $gci | ? { $_.PsisContainer }

    # Creating all the folders
    if (!(Test-Path $Destination)) { mkdir $Destination -ea SilentlyContinue > $null }
    $Folders | % {
        $Target = $_.FullName -replace [regex]::escape($Source), $Destination
        mkdir $Target -ea SilentlyContinue > $null
    }

    # Creating Wscript object
    $WshShell = New-Object -comObject WScript.Shell

    # Creating all the Links
    If ($status -eq $false) {($WshShell.fso.FileExists("$Destination") + "*.lnk")

      $Files | % {
          $InkName = "{0}.lnk" -f $_.sBaseName
          $Target = ($_.DirectoryName -replace [regex]::escape($Source), $Destination) + "\" + $InkName
          $Shortcut = $WshShell.CreateShortcut($Target)
          $Shortcut.TargetPath = $_.FullName
          $Shortcut.Save()
          }
        }
}
Create-ShortcutForEachFile -Source \\myserver.domain.local\Folder1\Folder2\Test -Destination \\myserver2.domain.local\Folder1\Folder2\Test -Recurse

希望任何人都可以帮助我,为成为powershell /脚本菜鸟而道歉。

4

1 回答 1

0

我的兄弟好心地修改了脚本以更好地满足我的需要。

这里是:

    #################################################
<#
    CREATE-SHORTCUT - creates shortcut for all files from a source folder
    version       : 1.0
    Author        : 
    Creation Date : 
    Modified Date : 
#>

#------------------------------------------------------------[ variables ]----------------------------------------------------------

$sourceDir="D:\scripts\create-shortcut\source"
$targetDir="D:\scripts\create-shortcut\dest"

#-------------------------------------------------------------[ Script ]-----------------------------------------------------------
# get files/files from folder
$src_gci=Get-Childitem -path $sourceDir -Recurse
$src_files=$src_gci | ? { !$_.PSisContainer }
$src_folders=$src_gci | ? { $_.PSisContainer }

# create subfolders
$src_folders | Copy-Item -Destination { join-path $targetDir $_.Parent.FullName.Substring($sourceDir.Length) } -Force

# create shortcuts
$WshShell = New-Object -comObject WScript.Shell
$src_files | % {
    $lnkName="{0}.lnk" -f $_.BaseName
    $Target = ($_.DirectoryName -replace [regex]::escape($sourceDir), $targetDir) + "\" + $lnkName
    $Shortcut = $WshShell.CreateShortcut($Target)
    $Shortcut.TargetPath = $_.FullName
    $Shortcut.Save()
    # change to SourceFiles ModifiedDate #
    $src_date=$_.LastWriteTime
    Get-ChildItem $Target | % { $_.LastWriteTime = "$src_date" }
}
于 2020-10-07T19:14:43.200 回答