0

我创建了一个脚本来循环浏览具有两列的电子表格。我遇到的问题是本地变量在我运行时不起作用invoke-command。我已经添加了-ArgumentList参数,但我仍然收到有关$null参数的错误。有人知道我在这里做错了什么吗?

foreach ($list in (Import-Csv C:\Users\joerod\Desktop\remove_users.csv)) {
    Write-Output "Connecting to $($list.computer)..."
    $myses = New-PSSession -ComputerName $list.computer
    Invoke-Command -Session $myses -ScriptBlock {
        Write-Output "Searching for software..."
        Write-Output $list.user

        $find_java = gwmi Win32_Product -Filter "Name LIKE '%Java'" | select -ExpandProperty IdentifyingNumber
        if ($find_java -ne $null) {
            Write-Output "Software found... Uninstalling..."
            foreach ($i in $find_mktx) {
                msiexec.exe /x $i /qn /passive /l*v! c:\uninst.log 
            }
            Write-Output "Adding $($list.user) to security group"
            $group = "CN=Java_removed,OU=Groups,OU=Resources,DC=Contoso,DC=LOCAL"
            Add-ADGroupMember $group -Identity $list.user
        }
        else {
            Write-Output "Could not find Java software installed"
        } 
    } -ArgumentList $list
}    
4

1 回答 1

1

问题是您试图将争论列表传递到您的else. 查看您是否正确格式化代码要容易得多。

foreach($list in (Import-Csv C:\Users\joerod\Desktop\remove_users.csv)){
    Write-Output "Connecting to $($list.computer)..."
    $myses = New-PSSession -ComputerName $list.computer
    Invoke-Command -Session $myses -ScriptBlock {
    Param($list)
        Write-Output "Searching for software..."
        Write-Output $list.user

        $find_java = gwmi win32_product -filter "Name LIKE '%Java'" | select -ExpandProperty     IdentifyingNumber
        if($find_java -ne $null){
            Write-Output "Software found... Uninstalling..."
            foreach($i in $find_mktx){
                msiexec.exe /x $i /qn /passive /l*v! c:\uninst.log 
            }
            Write-Output "Adding $($list.user) to security group"
            $group = "CN=Java_removed,OU=Groups,OU=Resources,DC=Contoso,DC=LOCAL"
            Add-ADGroupMember $group -Identity $list.user
        }
        else{
            Write-Output "Could not find Java software installed"
        } 
    } -ArgumentList $list

}
于 2014-04-24T15:34:43.057 回答