Создать ярлык «mydocuments» на рабочем столе с помощью powershell - PullRequest
0 голосов
/ 09 марта 2019

Я пытаюсь создать ярлык «MyDocuments» на моем локальном ПК, используя следующий скрипт.Но когда я запускаю сценарий через Azure Intune, он создает ярлык, но целью является «Этот компьютер».

$ShortcutPath = "$env:Public\Desktop\Docu.lnk" 
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = [environment]::GetFolderPath('MyDocuments')
$Shortcut.Save()

1 Ответ

0 голосов
/ 09 марта 2019

Попробуйте это. Вам нужны права администратора для запуска этого скрипта, потому что вы читаете другие профили пользователей.

Add-Type -AssemblyName System.DirectoryServices.AccountManagement

$user = "YourUsername" # User to set link to desktop

# get user sid and profilepath

$sid         = (Get-WmiObject -Class win32_useraccount -Filter "name = '$user'").SID
$profilePath = (Get-WmiObject -Class Win32_UserProfile -Filter "SID = '$sid'").LocalPath

if( $profilePath -ne $null ) {

    $currentUser = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
    $currentSid  = $currentUser.Sid.Value

    # if another user load reg hive

    if( $currentSid -ne $sid ) {
        $pathReg = Resolve-Path "$profilePath\..\$user\NTUSER.DAT"
        reg load "HKU\$sid" $pathReg | Out-Null
    }

    New-PSDrive -Name 'HKUser' -PSProvider Registry -Root "HKEY_USERS" | Out-Null


    # get desktop folder of user

    $shellFolders  = Get-Item "HKUser:\$sid\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
    $desktopFolder = $shellFolders.GetValue("Desktop")
    [void]$shellFolders.Close()
    [void]$shellFolders.Dispose()

    Remove-PSDrive -Name 'HKUser'

    # if another user unload reg hive

    if( $currentSid -ne $sid ) {
        [System.GC]::Collect()
        [System.GC]::WaitForPendingFinalizers()
        try {
            reg unload "HKU\$sid" | Out-Null
        }
        catch {}
    }

    # finally create shortcut

    if( $desktopFolder.Length -gt 0 ) {

        $ShortcutPath = $desktopFolder + '\Docu.lnk'
        $WshShell = New-Object -ComObject WScript.Shell
        $Shortcut = $WshShell.CreateShortcut($ShortcutPath)
        $Shortcut.TargetPath = [environment]::GetFolderPath('MyDocuments')
        [void]$Shortcut.Save()

    }
}
...