2

I am quite new to powershell. I am trying to remote to PCs in a csv file and extract the status of a registry key. However I get the error

"Unexpected token '-ArgumentList' in expression or statement." when I try to execute. And I am not pretty sure on the syntax of the Invoke-Command, can one of your pls verify if it is correct? Appreciate your help.

So basically, what I intend to do is, get computer names, specify an output path which I will be then using inside the invoke command. Testing the online status of the PC, checking the required registry value and writing it to the file.

$computers = Get-Content "C:\Temp\AutoSug\Computers.txt"

$output_path = "C:\Temp\AutoSug\output.csv"

foreach($computer in $computers)
{
    Test-Connection -computername $computer -Quiet
    If (Test-Connection $computer -count 1 -quiet)
    {
        Invoke-Command -computer $computer -ScriptBlock
        {
            param(
                $output_path
            )
            $hostname = (Get-CIMInstance CIM_ComputerSystem).Name
            $objExcel = New-Object -ComObject Excel.Application
            if ($objExcel.Version -eq "15.0")
            {
                $HKEY_USERS = Get-ChildItem REGISTRY::HKEY_USERS | where-object { ($_.Name -like "*S-1-5-21*") -and ($_.Name -notlike "*_Classes") } 
                $Users = @()
                $value = @()
                foreach ($User in $HKEY_USERS) 
                {  
                    $PROFILESID = Get-ChildItem REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Where-Object { $_.name -like "*" + $USER.PSChildName + "*" }  
                    $SID = $PROFILESID.PSChildName 
                    foreach ($value in $SID)
                    {
                        $key = Get-Item REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\15.0\Outlook\Preferences -ErrorAction SilentlyContinue
                        $gold = $key.property
                        if($gold -like 'ShowAutoSug')
                        {
                            $grail = (Get-ItemProperty REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\15.0\Outlook\Preferences).ShowAutoSug
                            $objSID = New-Object System.Security.Principal.SecurityIdentifier($value)
                            $objUser = $objSID.Translate([System.Security.Principal.NTAccount])
                            $hostname, $objUser, $value , $grail | Add-Content $output_path
                        } 
                        else
                        {
                            $objSID = New-Object System.Security.Principal.SecurityIdentifier($value)
                            $objUser = $objSID.Translate([System.Security.Principal.NTAccount])
                            $hostname,$objUser, $value , "The Auto Complete is not disabled" | Add-Content $output_path
                        } 
                    }
                }
            }

            if ($objExcel.Version -eq "14.0")
            {
                $HKEY_USERS = Get-ChildItem REGISTRY::HKEY_USERS | where-object { ($_.Name -like "*S-1-5-21*") -and ($_.Name -notlike "*_Classes") } 
                $Users = @()
                $value = @()
                foreach ($User in $HKEY_USERS) 
                {  
                    $PROFILESID = Get-ChildItem REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Where-Object { $_.name -like "*" + $USER.PSChildName + "*" }  
                    $SID = $PROFILESID.PSChildName 
                    foreach ($value in $SID)
                    {
                        $key = Get-Item REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\14.0\Outlook\Preferences -ErrorAction SilentlyContinue
                        $gold = $key.property
                        if($gold -like 'ShowAutoSug')
                        {
                            $grail = (Get-ItemProperty REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\14.0\Outlook\Preferences).ShowAutoSug
                            $objSID = New-Object System.Security.Principal.SecurityIdentifier($value)
                            $objUser = $objSID.Translate([System.Security.Principal.NTAccount])
                            $hostname, $objUser, $value , $grail | Add-Content -path $output_path
                        } 
                        else
                        {
                            $objSID = New-Object System.Security.Principal.SecurityIdentifier($value)
                            $objUser = $objSID.Translate([System.Security.Principal.NTAccount])
                            $hostname,$objUser, $value , "The Auto Complete is not disabled" | Add-Content $output_path
                        }
                    }
                }
            }

            if ($objExcel.Version -eq "12.0")
            {
                $HKEY_USERS = Get-ChildItem REGISTRY::HKEY_USERS | where-object { ($_.Name -like "*S-1-5-21*") -and ($_.Name -notlike "*_Classes") } 
                $Users = @()
                $value = @()
                foreach ($User in $HKEY_USERS) 
                {  
                    $PROFILESID = Get-ChildItem REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | Where-Object { $_.name -like "*" + $USER.PSChildName + "*" }  
                    $SID = $PROFILESID.PSChildName 
                    foreach ($value in $SID)
                    {
                        $key = Get-Item REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\12.0\Outlook\Preferences -ErrorAction SilentlyContinue
                        $gold = $key.property
                        if($gold -like 'ShowAutoSug')
                        {
                            $grail = (Get-ItemProperty REGISTRY::HKEY_USERS\$VALUE\Software\Microsoft\Office\12.0\Outlook\Preferences).ShowAutoSug
                            $objSID = New-Object System.Security.Principal.SecurityIdentifier($value)
                            $objUser = $objSID.Translate([System.Security.Principal.NTAccount])
                            $hostname, $objUser, $value , $grail | Add-Content $output_path
                        } 
                        else
                        {
                            $objSID = New-Object System.Security.Principal.SecurityIdentifier($value)
                            $objUser = $objSID.Translate([System.Security.Principal.NTAccount])
                            $hostname,$objUser, $value , "The Auto Complete is not disabled" |Add-Content $output_path
                        }
                    }
                }
            }
        } -ArgumentList $output_path
    }
    else 
    {
        $status = 'Offline'
        $computer , $status | Add-Content $output_path
    }
}
JensG
  • 13,148
  • 4
  • 45
  • 55
Gideon Rufus
  • 29
  • 1
  • 3
  • It is not clear, what your problem is and what you're asking for. Please, elaborate your problem and provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Eduard Malakhov Feb 01 '17 at 15:45

1 Answers1

1

To fix your error, simply cut -ArgumentList $output_path and put it before -ScriptBlock like:

Invoke-Command -computer $computer -ArgumentList $output_path -ScriptBlock ....
halfer
  • 19,824
  • 17
  • 99
  • 186
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • To add to that. Mapping the remote registry as a PSDrive also makes the code cleaner and you dont have to use the `Invoke-Command` at all. – Fairy Feb 01 '17 at 15:33