Как создать desktop.ini в PowerShell?

Моя цель - настроить значок всех папок, если имя фодера оканчивается на "_S". Поэтому я написал функцию PowerShell, но настройка не работает.

Я черпал вдохновение из:
Сайт для установки атрибута на desktop.ini
Функция, которая устанавливает пользовательский значок папки

function Set-icon_Folder_S($path) 
{

$ini = '[.ShellClassInfo]
        IconIndex =  0
        IconResource=C:\private.ico,0
        ConfirmFileOp = 0       
        DefaultDropEffect = 1'


    Set-Location $path
    #List all folder end with _S
    $items = Get-ChildItem -Recurse | Where-Object {($_.Attributes -match "Directory") -and ($_.Name.EndsWith("_S"))} | Select-Object -ExpandProperty FullName

    #For each folder _S ...
    foreach ($item in $items)
    {
        Set-Location $item

        #Init. Delete desktop.ini if exist
        try {
            Remove-Item desktop.ini -Force -erroraction stop
        }
        catch {         
        }

        #Write desktop.ini
        Write-Host "Go to $($item)"
        $ini | Out-File desktop.ini -Force
        Set-ItemProperty desktop.ini -Name Attributes -Value “ReadOnly,System,Hidden”

    }
}

Я не нахожу ошибки.

1 ответ

Решение

У меня работает следующий настроенный скрипт: добавление пустой строки в desktop.ini Хвост должен сделать сам по себе attrib.exe Я попробовал сначала, так что это может быть избыточным):

function Set-icon_Folder_S($path) 
{
$ini = '[.ShellClassInfo]
        IconIndex =  0
        IconResource=D:\Remote\icons\folderBlue.ico,0
        ConfirmFileOp = 0       
        DefaultDropEffect = 1
        '                          ### empty line: important

    Push-Location "$path"          ### Push-Location instead of Set-Location
    #List all folder end with _S
    $items = Get-ChildItem -Recurse | 
        Where-Object {
          ($_.Attributes -match "Directory") -and ($_.Name.EndsWith("es"))} | 
        Select-Object -ExpandProperty FullName                   ### ↑↑  change!!!

    #For each folder _S ...
    foreach ($item in $items)
    {
        Push-Location "$item"      ### Push-Location instead of Set-Location

        #Init. Delete desktop.ini if exist
        try {
            Remove-Item desktop.ini -Force -erroraction stop
        }
        catch {                    ### Write-Host "error removing $($item)"
        }

        #Write desktop.ini
        Write-Host "Go to $($item)"
        $ini | Out-File desktop.ini -Force
        Set-ItemProperty desktop.ini -Name Attributes -Value “ReadOnly,System,Hidden”
        Pop-Location               ### Pop-Location for corresponding Push-Location
        attrib.exe +R +S "$item"   ### Follow DESKTOP.INI CUSTOMIZATIONS DO NOT …
    }
    Pop-Location                   ### Pop-Location for corresponding Push-Location
}

Set-icon_Folder_S "D:\PShell"      ### call function declared above

Изменения описаны в приведенном выше коде с использованием ### комментариев (кроме IconResource):

### another IconResource used to match my testing environment
### empty line: important
### Push-Location instead of Set-Location
### used `"es"` suffix instead of `"_S"` one to match my testing environment
### Pop-Location for corresponding Push-Location
attrib.exe +R +S "$item"           ### Follow DESKTOP.INI CUSTOMIZATIONS DO NOT TAKE EFFECT
Set-icon_Folder_S "D:\PShell"      ### call function declared above
Другие вопросы по тегам