在没有用户交互的情况下运行 powershell

时间:2021-06-02 12:04:07

标签: powershell script

start "odopen://sync/?siteId=$siteid17&webId=$webid17&listId=$listid17&userEmail=$upn&webUrl=$URL17&webtitle=$webtitle17&listtitle=$listtitle17"

如何在 Powershell 中运行以下命令而不出现弹出窗口或任何用户交互?我试过添加 /ArgumentList "/S"、"/Background"。最后还尝试了 -WindowStyle Hidden 。感谢一些帮助:)

2 个答案:

答案 0 :(得分:0)

TL;DR:你不能。

使用 odopen 将始终显示登录窗口(如此处所述:https://docs.microsoft.com/en-us/onedrive/deploy-on-windows#help-users-sign-in),您可以做的只是用数据填充它,这是您已经在做的。

如果你想默默地做,有关于它的文档:https://docs.microsoft.com/en-us/onedrive/use-silent-account-configuration

答案 1 :(得分:0)

您的命令基本上是“启动打开 odopen:// (OneDrive) 链接的程序”,并且不能真正给出任何无提示样式的说明。配置这种东西的正确方法是通过OneDrive Group Policies,但我们可以欺骗和设置注册表项。

上面的链接详细介绍了如何配置组策略,但也告诉我们“配置团队网站库以自动同步”的特定组策略设置设置了此注册表项:

[HKCU\Software\Policies\Microsoft\OneDrive\TenantAutoMount]"LibraryName"="LibraryID"

而且你的 LibraryID 是这种格式,看起来很熟悉:

tenantId=xxx&siteId=xxx&webId=xxx&listId=xxx&webUrl=httpsxxx&version=1

所以要把它放在一个脚本中,我会使用这样的东西,改编自Nicola Suter's blog post here

$tenantAutoMountRegKey = "HKLM:\SOFTWARE\Policies\Microsoft\OneDrive\TenantAutoMount"

$autoMountTeamSitesList= @{
    #Enter your SharePoint libraries to configure here as key/value pairs
    MySharePoint="odopen://sync/?siteId=$siteid17&webId=$webid17&listId=$listid17&userEmail=$upn&webUrl=$URL17&webtitle=$webtitle17&listtitle=$listtitle17"
}

# Check if the key exists and create if missing:
if (-not (Test-Path $tenantAutoMountRegKey)){ New-Item -Path $tenantAutoMountRegKey -Force }

# Add the sites for automatic mounting
$autoMountTeamSitesList | Set-ItemProperty -Path $tenantAutoMountRegKey -Name $_.Key -Value $_.Value

这通常会在用户下次登录 OneDrive 时生效,但 Microsoft 警告说可能需要长达 8 小时才能开始同步(防止数百名用户同时同步同一个库)