question

RobNicholson-1457 avatar image
0 Votes"
RobNicholson-1457 asked RobNicholson-1457 commented

Delay logon script until OneDrive is fully running

We've developed a logon script in PowerShell that runs in the background when user's logon. It works most of the time except one of the tasks it does is to copy a file if it's been updated, i.e. the time stamp on the source is different to the target.

The problem is that the source file is in OneDrive with the "on-demand" feature enabled, i.e. it's only downloaded when it's accessed. Now if OneDrive is up and running before the PowerShell Copy-Item command is run, there is no problem - the file is downloaded and copied fine.

However, if the logon script runs before OneDrive has launched, logged on and initialised, the copy fails. Worse, it sometimes deletes the target file and fails to copy.

So I need a programmatic way of pausing the logon script until OneDrive is fully working... simple request but not a simple answer I suspect :-(

PS. The list of tags around PowerShell and OneDrive is very limited...


windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Have the logon script check to see if the process with the name "onedrive" is running. If it isn't, sleep for a second or two and try again -- just don't loop forever!

If onedrive never starts after a reasonable time, well, you'll have to handle that somehow. Unattended scripts need to be resilient and cope with the unexpected.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RobNicholson-1457 avatar image
0 Votes"
RobNicholson-1457 answered RobNicholson-1457 commented

Waiting for the OneDrive process to exist is a good starting point but it's not perfect as it might still not have logged in and be able to process requests. Considering that OneDrive is a key part of the Windows and M365 empire, the lack of a PowerShell module is telling. There is a 3rd party module but it's not been updated for a year.

All one needs is a Get-OneDriveStatus cmdlet that returns the status of each of the OneDrive accounts.

I must admit that OneDrive is IMO one of the weakest components in the Windows stack. It's still not reliable enough and "on-demand" is still not deeply embedded in the Windows stack. The user interface around settings is horrible. In fact, as OneDrive is so entwined with Windows, it should be managed through the Windows settings program or at least a modern settings interface such as for security settings. But I digress :-)

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Chilkat has 30-day evals for their .Net assemblies. I've used them for Perl, but not PowerShell. Couldn't hurt to look.

1 Vote 1 ·

Thanks for the reference - I'll follow it up. Quite easy to call .NET from PowerShell.

0 Votes 0 ·
RobNicholson-1457 avatar image
0 Votes"
RobNicholson-1457 answered

PS. Thought I'd see if there was a UserVoice post for PowerShell module. There is but it's not had many votes. I thought Microsoft's stance was "Implementing new feature - tick; make sure it's manageable via PowerShell - err"...

Also notice this - not surprised as I find UserVoice singularly difficult to track and use.

Update: Microsoft will be moving away from UserVoice sites on a product-by-product basis throughout the 2021 calendar year. We will leverage 1st party solutions for customer feedback. Learn more

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.