将函数的值拉入变量

时间:2014-10-19 20:07:28

标签: powershell

Get-loggedOnUser函数显示3个项目,代码可在此处找到LoggedInUser

  1. 计算机名称
  2. 登录用户
  3. 用户的Sid
  4. 我希望能够获取LoggedOnUser函数的结果(只需要登录用户)并将结果传递给下面名为Username的变量。

        Import-Module ActiveDirectory
        function Get-LoggedOnUser
    
        $credential = Get-Credential
        $computers = "MachineName"
    
        foreach ($machine in $computers)
        {
        Get-LoggedOnUser -ComputerName $machine
        $username = $_.LoggedOn
        $sid = ((get-aduser $username).SID).Value
    
        Invoke-Command -ComputerName $machine -Credential $credential -ScriptBlock { New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS; New-ItemProperty -Path "HKU:\$($args[0])\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -PropertyType String -Name AutoConfigURL -Value "http://proxy.domain.com/proxies/proxy.pac" -Force } -argumentlist $sid
        }
    

1 个答案:

答案 0 :(得分:0)

$loggedOn=Get-LoggedOnUser -computername $machine
$username = $loggedOn.LoggedOn
$sid = $loggedOn.Sid #as this is already returned from above funtion
$sid = ((get-aduser $username).sid).value #if you want to still get it from AD