我正在开发一个 Web 控制器来显示和(最终)修改用户的域信息。理想情况下,我想要用户名、全名、状态(锁定?)以及他们是否已登录。
我已经走到这一步了
# Define the target domain controller
$domainController = "myController"
# Hardcoded credentials (for demonstration purposes only, not recommended in production)
$username = "[email protected]"
$password = ConvertTo-SecureString "MyP@ssw03d!*" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
# Connect to the specified domain controller remotely using hardcoded credentials
$sessionQuery = Get-WmiObject -Class Win32_LogonSession -ComputerName $domainController -Credential $credential
$sessionQuery | ForEach-Object {
Write-Host $_.Properties | ForEach-Object {
$propertyData=[System.Management.PropertyData]$_
Write-Host $($propertyData.Name) $($propertyData.Value)
Write-Host "----------------------"
}
}
但它从 Powershell 返回的唯一数据是一遍又一遍重复的 System.Management.PropertyData。甚至连分隔线都没有被打印。
我完全不熟悉 PowerShell 脚本,但我一直无法找到通过 C# 来管理它的方法。我正在寻找此脚本的解决方案或从 C# 中检索我需要的内容的参考。
谢谢你。
Write-Host
是该作业的错误 cmdlet - 它将输出直接打印到屏幕,并且不会生成下游 cmdlet 可以使用的任何标准输出。彻底放弃:
$dcCimSession = New-CimSession -ComputerName $domainController -Credential $credential
$sessionQuery = Get-CimInstance -ClassName Win32_LogonSession -CimSession $dcCimSession
$sessionQuery | ForEach-Object {
# when using the CIM cmdlets, the meta-property you want is `CimInstanceProperties` -
$_.CimInstanceProperties | ForEach-Object {
Write-Host "$($_.Name) $($_.Value)"
Write-Host '----------------------'
}
}