将变量设置为等于powershell脚本的输出

将变量设置为等于powershell脚本的输出,powershell,exchangewebservices,contacts,Powershell,Exchangewebservices,Contacts,这有点难以解释,但我会尽力的。我正在编写一些代码,使用Powershell通过EWS将广告联系人导入用户邮箱 我有一个Main.ps1文件,它调用在后台工作的所有其他脚本(例如,1导入AD模块),另一个导入O365模块 我有一个连接到EWS的脚本容器。代码如下所示: #CONFIGURE ADMIN CREDENTIALS $userUPN = "User@domain.com" $AESKeyFilePath = ($pwd.ProviderPath) + "\

这有点难以解释,但我会尽力的。我正在编写一些代码,使用Powershell通过EWS将广告联系人导入用户邮箱

我有一个Main.ps1文件,它调用在后台工作的所有其他脚本(例如,1导入AD模块),另一个导入O365模块

我有一个连接到EWS的脚本容器。代码如下所示:

#CONFIGURE ADMIN CREDENTIALS
$userUPN = "User@domain.com" 
$AESKeyFilePath = ($pwd.ProviderPath) + "\ConnectToEWS\aeskey.txt"
$SecurePwdFilePath =  ($pwd.ProviderPath) + "\ConnectToEWS\password.txt"
$AESKey = Get-Content -Path $AESKeyFilePath -Force
$securePass = Get-Content -Path $SecurePwdFilePath -Force | ConvertTo-SecureString -Key $AESKey

#create a new psCredential object with required username and password
$adminCreds = New-Object System.Management.Automation.PSCredential($userUPN, $securePass)

Try
{
    [Reflection.Assembly]::LoadFile("\\MBX-Server\c$\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll") | Out-Null
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
    $service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($userUPN,$adminCreds.GetNetworkCredential().Password)
    $service.Url = new-object Uri("https://outlook.office365.com/EWS/Exchange.asmx");
    
    return $service
}
Catch
{
    Write-Output "Unable to connect to EWS. Make sure the path to the DLL or URL is correct"
}
该代码的输出打印出服务连接,但我希望该输出的信息存储在变量中,例如$Service

然后我会将该变量传递给另一个绑定到我想要的邮箱的脚本。。。 我遇到的问题是$service似乎没有存储这些信息。当我从上面的脚本返回它时,它只打印一次,但它不会将该信息附加到主脚本中。当我打印$service时,它只打印一次,但随后它会自行清除

这是我的主要剧本

CLS

#Root Path
$rootPath = $pwd.ProviderPath #$PSScriptRoot #$pwd.ProviderPath

Write-Host "Importing all necessary modules."

#******************************************************************
#                            PREREQUISITES
#******************************************************************
#Nuget - Needed for O365 Module to work properly

if(!(Get-Module -ListAvailable -Name NuGet))
{
    #Install NuGet (Prerequisite) first
    Install-PackageProvider -Name NuGet -Scope CurrentUser -Force -Confirm:$False
}

#******************************************************************

#Connect w\ Active Directory Module
& $rootPath\AD-Module\AD-module.ps1

#Load the O365 Module
& $rootPath\O365-Module\O365-module.ps1

#Clear screen after loading all the modules/sessions
CLS

#******************************************************************
#                         PUT CODE BELOW
#******************************************************************

#GLOBAL VARIABLES
$global:FolderName = $MailboxToConnect = $Service = $NULL

#Connect to EWS
& $rootPath\ConnectToEWS\ConnectToEWS.ps1

#Debug
$Service
    
#Create the Contacts Folder

& $rootPath\CreateContactsFolder\CreateContactsFolder.ps1 

#Debug
$service
$ContactsFolder

#Clean up Sessions after use

if($NULL -ne (Get-PSSession))
{
    Remove-PSSession *
}

[GC]::Collect()
第一次输出$service变量时,它打印得很好。在第二次调试输出中,它不再打印,我相信这就是为什么我启动“CreateContactsFolder.ps1”时脚本失败的原因

以下是“CreateContactsFolder.ps1”的内容


在主脚本中,从EWS脚本捕获返回的变量,如

$service = & $rootPath\ConnectToEWS\ConnectToEWS.ps1
或者将该脚本放入主脚本中,因此EWS.ps1中的变量是主脚本的本地变量,因此不需要在其中执行
返回$service

. $rootPath\ConnectToEWS\ConnectToEWS.ps1
并对CreateContactsFolder.ps1脚本执行相同的操作

使用全局范围定义被调用脚本中的重要变量
$global:service
$global:ContactsFolder

谢谢。这就是我在看到你的答案之前所做的。我有一个“脸掌心”的时刻,但在做了你提到的事情之后,一切都开始起作用了。非常感谢。另一方面(与同一脚本相关),是否有办法以多线程模式或其他方式运行它,以便更快地导入联系人?我只是在自己身上测试了一下,上传大约500个联系人只花了我5分钟。因此,如果我必须为所有用户执行此操作,则需要超过41个小时。
. $rootPath\ConnectToEWS\ConnectToEWS.ps1