【问题标题】:Configuring powershell core from .net core to provision a one drive从 .net 核心配置 powershell 核心以配置一个驱动器
【发布时间】:2018-11-30 18:52:06
【问题描述】:

我正在尝试使用 powershell 核心在 dotnet 核心应用程序中配置一个驱动器。运行 powershell 我已经能够按照以下提供的说明从 powershell 命令行成功配置一个驱动器:

https://docs.microsoft.com/en-us/onedrive/pre-provision-accounts

在 .net core 中以编程方式运行它,但它看起来像使用捆绑到 .net core 2.1 中的单独的 powershell

我认为应用程序运行不成功是由于未正确设置与核心捆绑的 powershell,即上面链接中的前 3 个步骤:

1.下载最新的 SharePoint Online 命令行管理程序。

2.下载并安装 SharePoint Online 客户端组件 SDK。

3.以全局管理员或 Office 365 中的 SharePoint 管理员身份连接到 SharePoint Online。要了解如何操作,请参阅 SharePoint Online 命令行管理程序入门。

如何设置由我的应用程序运行的 powershell 以反映上述步骤?

我的代码如下所示:

using System.IO;
using System.Management.Automation;

namespace PowerShellApp
{
    class Program
    {
        public static int Main(string[] args)
        {
            using (PowerShell ps = PowerShell.Create(
            {
                ps.AddScript(File.ReadAllText(<scriptLocation>))
                    .Invoke();
            }
        }
        return 0;
    }
}

在 .net 核心应用程序中执行时如何实现这些步骤

我正在运行的 powershell 脚本在下面以及上面的链接中:

<#
.SYNOPSIS
 This script adds an entry for each user specified in the input file 
 into the OneDrive provisioning queue

.DESCRIPTION
 This script reads a text file with a line for each user. 
 Provide the User Principal Name of each user on a new line.
 An entry will be made in the OneDrive provisioning queue for each
 user up to 200 users.
.EXAMPLE
 .\BulkEnqueueOneDriveSite.ps1 -SPOAdminUrl https://contoso-    admin.sharepoint.com -InputfilePath C:\users.txt 
.PARAMETER SPOAdminUrl
 The URL for the SharePoint Admin center
 https://contoso-admin.sharepoint.com
.PARAMETER InputFilePath
 The path to the input file.
 The file must contain 1 to 200 users
 C:\users.txt
.NOTES
 This script needs to be run by a global or SharePoint administrator in Office 365
 This script will prompt for the username and password of the administrator
#>
param
(
    #Must be SharePoint Administrator URL
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [string] $SPOAdminUrl,

    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [string] $InputFilePath
)
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")     | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.R    untime") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.U    serProfiles") | Out-Null
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SPOAdminUrl)
$Users = Get-Content -Path $InputFilePath
if ($Users.Count -eq 0 -or $Users.Count -gt 200)
{
    Write-Host $("Unexpected user count: [{0}]" -f $Users.Count) -    ForegroundColor Red
    return 
}
$web = $ctx.Web
Write-Host "Enter an admin username" -ForegroundColor Green
$username = Read-Host
Write-Host "Enter your password" -ForegroundColor Green
$password = Read-Host -AsSecureString
$ctx.Credentials = New-Object     Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$password )
$ctx.Load($web)
$ctx.ExecuteQuery()
$loader =     [Microsoft.SharePoint.Client.UserProfiles.ProfileLoader]::GetProfileLoader($ctx)
$ctx.ExecuteQuery()
$loader.CreatePersonalSiteEnqueueBulk($Users)
$loader.Context.ExecuteQuery()
Write-Host "Script Completed"

【问题讨论】:

  • 你想把powershell翻译成.net吗?
  • 我们实际上有一个实现,但是遇到了 dll 无法正确加载的问题,这就是我们切换到 Powershell Core 执行的原因。如果我能弄清楚如何配置 .net 核心执行的 powershell 将解决我们的问题。
  • 我仍然不是 100% 清楚问题是什么。是关于将你的脚本/sn-p 从 powershell 切换到 powershell 核心吗?
  • 正确,所以上面的链接有几个步骤,看起来像是在我的本地机器、sharepoint 在线管理 shell 和 sharepoint 客户端 sdks 上安装了一些东西。在该安装中,它将我机器上的 powershell 配置为使用新库。第三步是将该 shell 与管理员帐户相关联。然而,当我执行我的代码时,看起来 .net 核心有它自己的未配置的 powershell 实例。我知道这一点是因为当我运行 a 命令以在本地获取 powershell 的版本与在 dotnet 代码中时,它们返回不同的版本。
  • 你打算在 linux 上运行这个应用程序吗?想知道为什么要切换到核心

标签: powershell .net-core onedrive powershell-core


【解决方案1】:

恐怕 SP 在线管理 Shell 具有来自 .Net Framework 的依赖项,并且无法与 Core (check this) 一起使用。 另一方面,该模块似乎是其 REST API 之上的包装器。因此,如果您想将其与 Core 应用程序集成,您可以尝试将其替换为 HTTP 请求。检查这个documentation

此外,下面是一个基本的 powershell 脚本,用于处理这些 REST API 端点。我在我的网站上测试了这个:

$baseUrl = "http://sharepoint.com/sites/yourSite/_api"
$cred = Get-Credential

# retreive digest
$r = Invoke-WebRequest -Uri "$baseUrl/contextinfo" -Method Post -Credential $cred -SessionVariable sp
$digest = ([xml]$r.content).GetContextWebInformation.FormDigestvalue

# calling endpoint

$endpoint = "sp.userprofiles.profileloader.getprofileloader/getuserprofile"

$head = @{
 "Accept" = "application/json;odata=verbose"
 "X-RequestDigest" = $digest
 }

$re = Invoke-WebRequest -Uri "$baseUrl/$endpoint" -Headers $head -Method Post -WebSession $sp

Write-Host $re.Content

这是 createpersonalsiteenqueuebulk 的 sn-p,但由于我不是域管理员,因此无法对其进行测试。希望对你有用

 #--- sample 2 (didn't test it since I'm not domain admin). Might need separate session/digest

$endpoint2 = "https://<domain>-admin.sharepoint.com/_api/sp.userprofiles.profileloader.getprofileloader/createpersonalsiteenqueuebulk"

$head = @{
 "Accept" = "application/json;odata=verbose"
 "X-RequestDigest" = $digest
 }

$body =  "{ 'emailIDs': ['usera@domain.onmicrosoft.com', 'userb@domain.onmicrosoft.com'] }"
$re2 = Invoke-WebRequest -Uri "$endpoint2" -Headers $head -body $body -Method Post -WebSession $sp
Write-Host $re2.Content

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-08
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2018-11-24
    • 1970-01-01
    相关资源
    最近更新 更多