none
trying to uninstall a application on remote computers RRS feed

  • Question

  • Hello,

     foreach ($Device in $Devices)
        {

               invoke-command -ComputerName $name -ScriptBlock 
             {        
               $sysAidApp = Get-ChildItem -Path HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
                    Get-ItemProperty |
                        Where-Object {$_.DisplayName -match "acb" } |
                            Select-Object -Property DisplayName, UninstallString

                ForEach ($ver in $sysAidApp) {

                    If ($ver.UninstallString) {

                        $uninst = $ver.UninstallString
                        & cmd /c $uninst /quiet /norestart
                    }

                }

             }

            This always gives me an error Missing an argument for parameter 'ScriptBlock'. Specify a parameter of type 'System.Management.Automation.ScriptBlock' and try again.

           How do I fix that?

    Thanks, 
    Aman

       
    • Moved by Garth JonesMVP Monday, January 15, 2018 8:42 PM Not a CM12 Question
    Monday, January 15, 2018 7:49 PM

Answers

  • If it's not just because you copied the code here and didn't use the code formatting tool from the post editor you have a line break after -ScriptBlock where it's not allowed.
     foreach ($Device in $Devices){
        Invoke-Command -ComputerName $name -ScriptBlock {        
            $sysAidApp = Get-ChildItem -Path HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
                Get-ItemProperty |
                    Where-Object {$_.DisplayName -match "acb" } |
                        Select-Object -Property DisplayName, UninstallString
            foreach ($ver in $sysAidApp) {
                If ($ver.UninstallString){
                    $uninst = $ver.UninstallString
                    & cmd /c $uninst /quiet /norestart
                }
            }
        }
    }
    
    If you would have formatted your code nicely you could have noticed that by yourself.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by AmanKahlon Thursday, January 25, 2018 11:02 PM
    Monday, January 15, 2018 8:53 PM

All replies

  • Is this a CM12 question or a general PoSh question?


    Garth Jones

    Blog: http://www.enhansoft.com/blog Old Blog: http://smsug.ca/blogs/garth_jones/default.aspx

    Twitter: @GarthMJ Book: System Center Configuration Manager Reporting Unleased

    Monday, January 15, 2018 8:03 PM
  • General Powershell Ques.. Can you suggest that what i am missing here?

    Thanks.

    Monday, January 15, 2018 8:09 PM
  • Hello,

     foreach ($Device in $Devices)
        {

               invoke-command -ComputerName $name -ScriptBlock 
             {        
               $sysAidApp = Get-ChildItem -Path HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
                    Get-ItemProperty |
                        Where-Object {$_.DisplayName -match "acb" } |
                            Select-Object -Property DisplayName, UninstallString

                ForEach ($ver in $sysAidApp) {

                    If ($ver.UninstallString) {

                        $uninst = $ver.UninstallString
                        & cmd /c $uninst /quiet /norestart
                    }

                }

             }

            This always gives me an error Missing an argument for parameter 'ScriptBlock'. Specify a parameter of type 'System.Management.Automation.ScriptBlock' and try again.

           How do I fix that?

    Thanks, 
    Aman

    • Merged by jrv Monday, January 15, 2018 9:52 PM DUPLIcate
    Monday, January 15, 2018 8:10 PM
  • If it's not just because you copied the code here and didn't use the code formatting tool from the post editor you have a line break after -ScriptBlock where it's not allowed.
     foreach ($Device in $Devices){
        Invoke-Command -ComputerName $name -ScriptBlock {        
            $sysAidApp = Get-ChildItem -Path HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
                Get-ItemProperty |
                    Where-Object {$_.DisplayName -match "acb" } |
                        Select-Object -Property DisplayName, UninstallString
            foreach ($ver in $sysAidApp) {
                If ($ver.UninstallString){
                    $uninst = $ver.UninstallString
                    & cmd /c $uninst /quiet /norestart
                }
            }
        }
    }
    
    If you would have formatted your code nicely you could have noticed that by yourself.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by AmanKahlon Thursday, January 25, 2018 11:02 PM
    Monday, January 15, 2018 8:53 PM
  • Good Day

    How many opening brackets you have ? i believe you are missing 1 closing bracket
    Please add another closing bracket at the end and try again
    Regards

    foreach ($Device in $Devices)
        {
    
               invoke-command -ComputerName $name -ScriptBlock 
             {        
               $sysAidApp = Get-ChildItem -Path HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
                    Get-ItemProperty |
                        Where-Object {$_.DisplayName -match "acb" } |
                            Select-Object -Property DisplayName, UninstallString
    
                ForEach ($ver in $sysAidApp) {
    
                    If ($ver.UninstallString) {
    
                        $uninst = $ver.UninstallString
                        & cmd /c $uninst /quiet /norestart
                    }
    
                }
    
             }
        }

    Monday, January 15, 2018 9:53 PM
  • $sb = {
    	$sysAidApp = Get-ChildItem -Path HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
    	Get-ItemProperty | Where-Object {
    		$_.DisplayName -match 'acb'
    	} 
    	ForEach ($ver in $sysAidApp) {
    			If ($ver.UninstallString) {
    				$uninst = $ver.UninstallString
    				& cmd /c $uninst /quiet /norestart
    			}
    		}
    }
    
    foreach ($device in $devices) {
    	invoke-command -ComputerName $device -ScriptBlock $sb
    }
    
    This way is easier to test and understand.

    \_(ツ)_/

    Monday, January 15, 2018 9:59 PM