如何从注册表获取O365登录状态?

问题描述 投票:0回答:2

我想获取下面的某个注册表项名称

HKCU:\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identities

根据下面的屏幕截图,我确实有两个子键,并且我喜欢字母数字键的子键名称,名称末尾带有 _ADAL。

当我的用户没有登录Office365时,这个键中会有一个属性“SignedOut”。 这就是我需要消息框吐出的信息

我首先研究了 MSOline cmdlet,但它将于 2024 年 3 月被弃用,因此不再有选择。 GraphAPI 会太复杂,因为我必须注册脚本才能根据公司政策对 API 进行身份验证。

现在,我们正在研究注册表项,它似乎包含我需要的信息:

当我排除第二个键末尾的“_AD”时,我得到了所需键的所有子项。 我的问题是,返回的“PSCustomObject”包含名称、属性以及相应的值。

Get-ChildItem -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identities' -Exclude *_AD

如何检查具有字母数字名称的注册表项是否存在“SignedOut”属性?

powershell office365 registry
2个回答
0
投票

使用

Get-ItemProperty
,并假设只有 one 键匹配
*_ADAL

$prop = 
  Get-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identities\*_ADAL' -Name SignedOut
  • 如果属性(注册表值)不存在,

    $prop
    将为
    $null
    ,因此可以使用
    $null -eq $prop
    来判断是否存在。

  • 如果它确实存在并且您想从返回的 [pscustomobject] 实例中提取属性

    value
    ,请使用
    $prop.SignedOut


如果有可能多个键匹配和/或您想知道匹配的的确切名称,您可以使用

在自定义对象中收集它们的名称和
SignedOut值,如下所示Select-Object
计算属性:

Get-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identities\*_ADAL' -Name SignedOut | Select-Object @{ n='Identity'; e='PSChildName' }, @{ n='SignedOut'; e={ $_.SignedOut } }
    

0
投票
非常感谢您的宝贵意见。我是这样解决的:

## Check for the availability of the SignedOut property underneath the ..\Identities\*_ADAL registry key ## and catch the exception when this property does not exist try { $prop = Get-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Office\16.0\Common\Identity\Identities\*_ADAL' -Name SignedOut -ErrorAction Stop } catch [System.Management.Automation.PSArgumentException] { $prop = 0 } if($prop -eq 0) { Write-Host "User is logged-in" } else { Write-Host "User is not logged-in" # Determine current user for error message $CurrentUser = (Get-WMIObject -ClassName Win32_ComputerSystem).Username.Split('\')[1] # Load the PresentationFramework module required for the message box Add-Type -AssemblyName PresentationFramework # Display Messagebox when User is not logged into O365 # User shall decide whether or not login into Office is desired. $msgBoxInput = [System.Windows.MessageBox]::Show("You (" + $CurrentUser + ") are currently not authenticated in Office365.`nThis may cause issues in COMOS release workflows.`n`nWould you like to launch Excel to (re-)authenticate?",'Warning: O365 Authentication missing','YesNo','Warning') switch ($msgBoxInput) { 'Yes' { ## We launch Excel; the user shall now authenticate $Excel = New-Object -ComObject Excel.Application $Excel.Visible = $true } 'No' { ## We do nothing and respect the user decision! } } }

© www.soinside.com 2019 - 2024. All rights reserved.