locked
OneDrive on Demand doing by PowerShell-Script RRS feed

  • Question

  • Would like to use a PS-Script to keep some files and folders Always online and some offline. But it does not work properly. E.g. to keep it offline, here my Script:

    
    
    $Offline = @(
        "$env:OneDrive\Dokumente\folder\*",
        "$env:OneDrive\Dokumente\file.xlsx"
        )
    ForEach ($listOff in $Offline) {
        attrib +p -u $listOff /s                       
    }

    When I change it to  "attrib +s +h +p -u" then it worked, but with some negative side-effects. some files then are hidden or become system files.

    Thanks for any help

    Monday, March 2, 2020 4:36 PM

Answers

  • If it is not working and those are the files that complain then yu have issues with your OneDrive.

    Did you try it again or are you just saying that you think it won't work?

    You cannot sync or pin the Windows folders.

    Run this and tell me what you see:

    $offline = "$env:OneDrive\Dokumente\folder","$env:OneDrive\Dokumente\file.xlsx"
    $offline |
        ForEach-Object{
            if((Get-Item $_).PsIsContainer){
                Write-Host $_ -fore red
                #attrib $_ +p -u /S
            }else{
                Write-Host $_ -fore green
                #attrib $_ +p -u
            }
        }
    I hope those are the folders/files you are testing with.  Perhaps you have changed teh code to something else.


    \_(ツ)_/

    • Marked as answer by joschi2 Tuesday, March 3, 2020 9:27 PM
    Tuesday, March 3, 2020 4:42 PM

All replies

  • There is no way to know what you are asking from what you have posted.  How does a PS script "keep" files offline?  What does that word mean.  "offline/online" are not Windows technical terms but are terms of reference.  It also can be that state of a file in subsystems like OneDrive.  Changing an attribute cannot change this.  "Pinned" is about a file shortcut being pinned.

    You cannot set the "pinned" attribute on a file that has not been downloaded.  If you unpin a local copy then the file will not be removed unless disk space purges it.

    You need to ask these questions in the OneDrive forum as they can further explain how this is intended to work.

    It sounds like your issue is with the installation of OneDrive or, perhaps, a network issue.

    I can also note that the "/s? switch should nver be used on files directly.  It applies only to folders.  Folders are always replicated and pinned but the files are not pinned automatically.

    The correct way to code this is like this:

    $offline = "$env:OneDrive\Dokumente\folder\*","$env:OneDrive\Dokumente\file.xlsx"
    Get-ChildItem $offline -Recurse -File |
        ForEach-Object{attrib $_ +p -u}
    


    \_(ツ)_/

    Monday, March 2, 2020 10:39 PM
  • You can also do it this way which might be a little bit faster.

    $offline = "$env:OneDrive\Dokumente\folder\*","$env:OneDrive\Dokumente\file.xlsx"
    $offline |
        ForEach-Object{
            if((Get-Item $_).PsIsContainer){
                attrib $_ +p -u /S
            }else{
                attrib $_ +p -u
            }
        }
    If this doesn't fix your issues then you have network or OneDrive issues.


    \_(ツ)_/


    • Edited by jrv Monday, March 2, 2020 10:43 PM
    Monday, March 2, 2020 10:43 PM
  • Thanks. The script does not work, maybe a typo?

    Don't have networt or OneDrive issues. Doing it by hand (via windows contextmenu) works fine.

    Tuesday, March 3, 2020 9:19 AM
  • You might have to do it this way:

    $offline = "$env:OneDrive\Dokumente\folder\*","$env:OneDrive\Dokumente\file.xlsx"
    $offline |
        ForEach-Object{
            if((Get-Item $_).PsIsContainer){
                attrib $_.Fullname +p -u /S
            }else{
                attrib $_.Fullname +p -u
            }
        }


    \_(ツ)_/

    Tuesday, March 3, 2020 9:44 AM
  • the script now is running but with a lots of denies and in the wrong part : C:\Windows\

    output extract:

    Zugriff verweigert - C:\Windows\system32\cscript.exe
    Zugriff verweigert - C:\Windows\system32\CspCellularSettings.dll

    Tuesday, March 3, 2020 10:30 AM
  • Sorry.  Remove the "\*" from the folder name:

    $offline = "$env:OneDrive\Dokumente\folder","$env:OneDrive\Dokumente\file.xlsx"



    \_(ツ)_/

    Tuesday, March 3, 2020 1:01 PM
  • Since this is a one time thing you shouldn't use a script.

    attrib +p -u "$env:OneDrive\Dokumente\folder" /S
    attrib +p -u "$env:OneDrive\Dokumente\file.xlsx"


    \_(ツ)_/

    Tuesday, March 3, 2020 1:06 PM
  • this works, but this is not I want. Would like to use a list / array. Otherwise I have to put many times the same code: attrib...
    Tuesday, March 3, 2020 1:28 PM
  • Then use the code I posted with the updated array.  The array can contain folders or files or both and teh code will choose the correct command.


    \_(ツ)_/

    Tuesday, March 3, 2020 1:33 PM
  • can you please past again which code? I tried all of them.
    Tuesday, March 3, 2020 3:51 PM
  • $offline = "$env:OneDrive\Dokumente\folder","$env:OneDrive\Dokumente\file.xlsx"
    $offline |
        ForEach-Object{
            if((Get-Item $_).PsIsContainer){
                attrib $_ +p -u /S
            }else{
                attrib $_ +p -u
            }
        }


    \_(ツ)_/

    Tuesday, March 3, 2020 4:00 PM
  • Told you that this was not working. :)

    Zugriff verweigert - C:\Windows\system32\cscript.exe
    Zugriff verweigert - C:\Windows\system32\CspCellularSettings.dll

    Tuesday, March 3, 2020 4:33 PM
  • If it is not working and those are the files that complain then yu have issues with your OneDrive.

    Did you try it again or are you just saying that you think it won't work?

    You cannot sync or pin the Windows folders.

    Run this and tell me what you see:

    $offline = "$env:OneDrive\Dokumente\folder","$env:OneDrive\Dokumente\file.xlsx"
    $offline |
        ForEach-Object{
            if((Get-Item $_).PsIsContainer){
                Write-Host $_ -fore red
                #attrib $_ +p -u /S
            }else{
                Write-Host $_ -fore green
                #attrib $_ +p -u
            }
        }
    I hope those are the folders/files you are testing with.  Perhaps you have changed teh code to something else.


    \_(ツ)_/

    • Marked as answer by joschi2 Tuesday, March 3, 2020 9:27 PM
    Tuesday, March 3, 2020 4:42 PM
  • Thanks, if works now, resp. almost. had to change
    "$env:OneDrive\Dokumente\folder"
    as I had before to:
    "$env:OneDrive\Dokumente\folder\*"
    Tuesday, March 3, 2020 8:46 PM