【发布时间】: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