【问题标题】:Argument Passed into ScriptBlock Doesn't Work on Last Iteration传递给 ScriptBlock 的参数在最后一次迭代中不起作用
【发布时间】:2022-01-19 02:58:53
【问题描述】:

我正在使用以下脚本运行指定 Active Directory OU 中的所有服务器并注销指定用户。这在前 35 台服务器上运行得非常好,但在它迭代的最后一台服务器上总是出错。错误是:

Program 'quser.exe' failed to run: Object reference not set to an instance of an object. At line:6 char:24
+             $result = (quser $userAccount)
+                        ~~~~~~~~~~~~~~~~~~.
At \\path\to\script.ps1:79 char:5
+     invoke-command -ComputerName $server -ScriptBlock $scriptBlock -A ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (Program 'quser....~~~~~~~~~~~~~~.:String) [], RuntimeException
    + FullyQualifiedErrorId : Program 'quser.exe' failed to run: Object reference not set to an instance of an object.At line:6 char:24
+             $result = (quser $userAccount)
+                        ~~~~~~~~~~~~~~~~~~.

我对错误的解读是它认为 $userAccount 没有价值。这是正确的,如果是这样,任何人都可以指出我所缺少的吗?提前谢谢!

Write-Host " "
Write-Host "This script will log out all active sessions for the specified user. Proceed with caution." -ForegroundColor Black -BackgroundColor Yellow
Write-Host " "

$servers = [System.Collections.ArrayList]::new()
foreach ($server in (Get-ADComputer -SearchBase "OU=FictionalDepartment,DC=Company,DC=net" -Filter "OperatingSystem -like 'Windows Server*'" -Properties Name | select -ExpandProperty name))
{
    $servers.add($server) | Out-Null
}
$servers.Sort()

$userAccount = Read-Host "Enter account to log out"
Write-Host " "

foreach ($server in $servers)
{
    $scriptBlock = {
        param($userAccount)

        $ErrorActionPreference = 'Stop'
        try
        {
            $result = (quser $userAccount)
            $session = ((quser $userAccount)[1] -split "\s+")[2]
            logoff $session
            Write-Host "   The user was logged into session #$session. They have been LOGGED OFF." -ForegroundColor Yellow
        }
        catch
        {
            if ($_.Exception.Message -match 'No user exists')
            {
                Write-Host "   User is not logged in." -ForegroundColor Green
            }
            else
            {
                throw $_.Exception.Message
            }
        }
    }
    Write-Host "$server"
    Invoke-Command -ComputerName $server -ScriptBlock $scriptBlock -ArgumentList $userAccount
    $session = $null
}

【问题讨论】:

  • 如果您从 Invoke-Command 中删除 -ArgumentList 参数并在您的脚本块中删除 param(...) 块并使用 $using:userAccount 而不是 $userAccount 会发生什么?另外,所有$scriptblock 变量都应该在循环之外。
  • 你能查询那个特定的系统,看看有没有返回吗?
  • @SantiagoSquarzon,使用该方法的效果相同。感谢您的提示——我已将 $scriptBlock 移到 for 循环之外。
  • @AbrahamZinala 如果我运行“quser /SERVER:”,我不会收到错误消息。也许我会修改脚本以像那样运行它而不是使用 Invoke-Command。
  • 我建议这样做。特别是如果您不能 100% 确定 quser.exe 不在远程系统上。

标签: powershell for-loop invoke-command


【解决方案1】:

根据反馈,我修改了脚本,使其不再使用Invoke-Command,而是通过运行quser <user> /SERVER:<server> 来解析活动会话,如下所示:

Write-Host "Will log specified user out of all servers..." -ForegroundColor Black -BackgroundColor Yellow
Write-Host " "

$servers = [System.Collections.ArrayList]::new()
foreach ($server in (Get-ADComputer -SearchBase "OU=Fictional,DC=Company,DC=net" -Filter "OperatingSystem -like 'Windows Server*'" -Properties Name | select -ExpandProperty name))
    {
    $servers.add($server) | Out-Null
    }
$servers.Sort()

$userAccount = Read-Host "Enter account to scan for"
Write-Host ""

foreach ($server in $servers)
    {
    Write-Host "$server"
    $ErrorActionPreference = 'Stop'
    try
        {
        $session = ((quser $userAccount /SERVER:$server)[1] -split "\s+")[3]
        logoff /SERVER:$server $session
        Write-Host "   The user was logged into session #$session. They have been LOGGED OFF." -ForegroundColor Yellow
        }
    catch
        {
        if ($_.Exception.Message -match 'No user exists')
            {
            Write-Host "   User is not logged in." -ForegroundColor Green
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-01
    • 2017-01-17
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    相关资源
    最近更新 更多