使用导入的 PowerShell 会话时如何对 Live@EDU (outlook.com) 进行身份验证?

发布于 2024-10-25 13:30:02 字数 308 浏览 1 评论 0原文

我正在通过 PowerShell 连接到 Exchange 2010 for Live@edu。我可以使用标准方法进行连接。然而,每次下载和导入会话命令似乎很浪费,特别是因为它不在 LAN 上。此外,有时这些脚本会将数据返回到网页,并且导入时间似乎也很浪费。

我找到了如何使用 Export-PSSession cmdlet 导出会话。如果我使用 Import-Module 导入导出的模块,则一切正常,除了一个问题。当我从模块运行 cmdlet 时,它希望我通过 GUI 以交互方式设置密码。我真正想要的是让我的脚本以非交互式方式运行,但又在本地加载模块。

这可以吗?

I am connecting to Exchange 2010 for Live@edu via PowerShell. I can connect using the standard methods just fine. However, downloading and importing the session commands every time seems wasteful, especially since this isn't on a LAN. Furthermore, occasionally, these scripts are going to be returning data to a web page, and the import time seems wasteful there as well.

I have found how to export the session using the Export-PSSession cmdlet. If I import that exported module with Import-Module, everything works correctly, except for one problem. When I run a cmdlet from the module, it expects me to interactively, via a GUI, set the password. What I really want is for my scripts to run in a non-interactive manner, but yet load the module locally.

Is this possible to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月棠 2024-11-01 13:30:02

您面临的问题是您需要能够隐式地为所有导入的函数设置 PSSession。为此,您需要能够运行 Set-PSImplicitRemotingSession 函数。

不幸的是,该函数未导出,因此您无法访问它。要解决此问题,您需要做的是破解 PSM1 文件并将该函数添加到 $script:ExportModuleMember 的末尾。现在,当您导入模块时,该函数将能够为所有函数设置 PSSession。

以下是您的 powershell 或脚本需要运行的内容才能使用任何导入的模块。

Import-Module "C:\Credentials.psm1"
Import-Module "C:\ExportedPSSession.psm1"
$Cred = Import-Credential -path C:\Cred.xml
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Authentication Basic -AllowRedirection -Credential $Cred
Set-PSImplicitRemotingSession -PSSession $Session -createdByModule $True
#You can now run any of the imported functions.

Credentials.psm1 小心!任何可以加载 xml 文件的人现在都可以冒充您!

function Export-Credential($cred, $path) {    
  $cred = $cred | Select-Object *    
  $cred.password = $cred.Password | ConvertFrom-SecureString
  $obj = New-Object psobject
  $obj | Add-Member -MemberType NoteProperty -Name UserName -Value $cred.username
  $obj | Add-Member -MemberType NoteProperty -Name Password -Value $cred.password
  $obj | Export-Clixml $path
}

function Import-Credential($path) {    
  $obj = Import-Clixml $path    
  $obj.password = $obj.Password | ConvertTo-SecureString
    return New-Object system.Management.Automation.PSCredential( $obj.username, $obj.password)
}

The problem you are facing is that you need to be able to set the PSSession for all of the imported functions implicitly. To do that you need to be able run the Set-PSImplicitRemotingSessionfunction.

Unfortionatly that function is not being exported so you cannot access it. What you need to do to resolve this is crack open the PSM1 file and add that function to the end of $script:ExportModuleMember. Now when you import the module that function will be abilable to set your PSSession for all of the functions.

Here is what your powershell or scripts will need to run to be able to use any of the imported modules.

Import-Module "C:\Credentials.psm1"
Import-Module "C:\ExportedPSSession.psm1"
$Cred = Import-Credential -path C:\Cred.xml
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Authentication Basic -AllowRedirection -Credential $Cred
Set-PSImplicitRemotingSession -PSSession $Session -createdByModule $True
#You can now run any of the imported functions.

Credentials.psm1 Beware! Anyone who can load the xml file can now impersonate you!

function Export-Credential($cred, $path) {    
  $cred = $cred | Select-Object *    
  $cred.password = $cred.Password | ConvertFrom-SecureString
  $obj = New-Object psobject
  $obj | Add-Member -MemberType NoteProperty -Name UserName -Value $cred.username
  $obj | Add-Member -MemberType NoteProperty -Name Password -Value $cred.password
  $obj | Export-Clixml $path
}

function Import-Credential($path) {    
  $obj = Import-Clixml $path    
  $obj.password = $obj.Password | ConvertTo-SecureString
    return New-Object system.Management.Automation.PSCredential( $obj.username, $obj.password)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文