Help with powershell script to fetch multistrings

This topic contains 19 replies, has 2 voices, and was last updated by Profile photo of Alexander Johansson Alexander Johansson 2 years, 1 month ago.

  • Author
    Posts
  • #19791
    Profile photo of Brian Petersen
    Brian Petersen
    Participant

    Hi,

    I have made a powershell script, which does some add/delete things locally on a client with MS onedrive problems. Please see the script attached.
    I succesfull tested the script on my own client and that fine, but now i found out that the local sync location path is different and uses different locations and pathnames on all company clients and therefore i need to add some code to my script which does the following:

    1) Read the following stringdata regkey

    [i]Path: HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation
    Type: REG_MULTI_SZ
    Data: C:\Users\dfbdp\OneDrive – Danske Fragtmænd A-S[/i]

    2) Insert this datavalue (path of folder) into the "Move-Item "C:\Users\$env:username\OneDrive – Danske Fragtmænd A-S" "C:\Users\$env:username\Desktop\Onedrive_Backup"...
    in the script so i basicly read the location path from the regdatabase instead.

    I have tried to solve this problem my selv with the Get-RegistryKey etc but have not been able to make it work.

    I am not that skilled in this, so can anyone help me?

    Best regards
    BDP

  • #19793
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    If you type in this:

    Set-Location -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation"

    What do you get then?

    Best regards Alexander

  • #19794
    Profile photo of Brian Petersen
    Brian Petersen
    Participant

    I am not that skilled, so i dont quite follow you. Where do i insert this? into the script or?

  • #19795
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    Execute it from a Powershell console with elevated privileges.

    /Alexander

  • #19796
    Profile photo of Brian Petersen
    Brian Petersen
    Participant

    Ok i get this:

    [b]PS C:\Users\dfbdp> Set-Location -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation"
    Set-Location : Cannot find path 'HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation' because it does not exist.
    At line:1 char:1
    + Set-Location -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSy ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: [HKCU:\Software\...entDiskLocation:String] [Set-Location], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand[/b]

  • #19797
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    Ok, then change the path to "HKCU:\Software\Microsoft\Office\15.0\Common\Internet" instead.
    What is returned now?

    /Alexander

  • #19798
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    Also I would like to suggest that you try this command and tell me what you get in return:

    Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation"

    /Alexander

  • #19799
    Profile photo of Brian Petersen
    Brian Petersen
    Participant

    Returns this:

    [b]PS HKCU:\Software\Microsoft\Office\15.0\Common\Internet> [/b]

  • #19800
    Profile photo of Brian Petersen
    Brian Petersen
    Participant

    The Get-Item... returns this:

    [b]Get-ItemProperty : Cannot find path 'HKCU:\Software\Microsoft\Office\15.0\Common\Internet\LocalSyncClientDiskLocation' because it does not exist.
    At line:1 char:1
    + Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet\Loc ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: [HKCU:\Software\...entDiskLocation:String] [Get-ItemProperty], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand[/b]

    and the Get-Item ... without "LocalSyncClientDiskLocation" returns this:

    UseRWHlinkNavigation : mailto:aer@axcess.dk
    UseRWOSHlinkNavigation : 0
    UseOnlineContent : 2
    LcfemCleanShutdown : 0
    DoNotCheckIfWordIsDefaultHTMLEditor : 1
    LocalSyncClientDiskLocation : {C:\Users\dfbdp\OneDrive – Danske Fragtmænd A-S}
    PSPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Common\Internet
    PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Common
    PSChildName : Internet
    PSDrive : HKCU
    PSProvider : Microsoft.PowerShell.Core\Registry

    ——————-

    Where i can see the line i need in LocalSyncClientDiskLocation : {C:\Users\dfbdp\OneDrive – Danske Fragtmænd A-S}

    🙂

    what then?

  • #19801
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    Ok, now try the following command:

    Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet" | Select-Object  -ExpandProperty LocalSyncClientDiskLocation

    Alternatively this:

    (Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet").LocalSyncClientDiskLocation

    /Alexander

  • #19802
    Profile photo of Brian Petersen
    Brian Petersen
    Participant

    It returns:

    [b]C:\Users\dfbdp\OneDrive – Danske Fragtmænd A-S[/b]

  • #19803
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    Thats great, now you got the path that you want!
    Do you require any further assistance?

    /Alexander

  • #19804
    Profile photo of Brian Petersen
    Brian Petersen
    Participant

    Yes, how do i insert this path into the script then?

    into this line marked bold: Move-Item "[b]C:\Users\$env:username\OneDrive – Danske Fragtmænd A-S[/b]" "C:\Users\$env:username\Desktop\Onedrive_Backup"

  • #19805
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    There is a couple of ways you could do it, I would recommend you to try the following:

    $sourcePath = (Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet").LocalSyncClientDiskLocation
    Move-Item -Path "C:\Users\$env:username\$sourcePath" -Destination "C:\Users\$env:username\Desktop\Onedrive_Backup"
    

    /Alexander

  • #19806
    Profile photo of Brian Petersen
    Brian Petersen
    Participant

    Hi Alexander,

    Ok, i will test this right now and reply on the results asap. Thx

  • #19807
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    I need to correct a mistake from my side though.

    You would like to try this instead:

    $sourcePath = (Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet").LocalSyncClientDiskLocation
    Move-Item -Path $sourcePath -Destination "C:\Users\$env:username\Desktop\Onedrive_Backup"
    

    Since it returns the full path. Sorry!

    /Alexander

  • #19808
    Profile photo of Brian Petersen
    Brian Petersen
    Participant

    Hi Alxeander,

    It does not work, but as i read it the exampel you made does this, right?:

    $sourcePath = (Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet").LocalSyncClientDiskLocation
    Move-Item -Path "C:\Users\$env:username\[b]C:\Users\dfbdp\OneDrive – Danske Fragtmænd A-S[/b]" -Destination "C:\Users\$env:username\Desktop\Onedrive_Backup"

    Should it not be like this or:

    $sourcePath = (Get-ItemProperty -Path "HKCU:\Software\Microsoft\Office\15.0\Common\Internet").LocalSyncClientDiskLocation
    Move-Item -Path "[b]$sourcePath[/b]" -Destination "C:\Users\$env:username\Desktop\Onedrive_Backup"

    ?

  • #19809
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    Read my latest answer 🙂

    /Alexander

  • #19810
    Profile photo of Brian Petersen
    Brian Petersen
    Participant

    Hi alexander.

    I allmost there. 🙂
    It works, but when i test the script it maked a new folder called "OneDrive – Danske Fragtmænd A-S 1" with a 1 number at the end... and i cant figure out why this is happening.. I will test some more and get back to you.
    Thank you so much for your help until now.

  • #19811
    Profile photo of Alexander Johansson
    Alexander Johansson
    Participant

    No problem, if you need further assistance you know where to find me!

    /Alexander

You must be logged in to reply to this topic.