Jump to content

Powershell newbie question with combining two scripts into one!


Recommended Posts

Posted

Hi,

I have a script that I run to remove rogue Hotfixes when SCCM will not remove them quickly or has issues...

 

function Uninstall-Hotfix {
[cmdletbinding()]
param(
$computername = $env:computername,
[string] $HotfixID
)            

$hotfixes = Get-WmiObject -ComputerName $computername -Class Win32_QuickFixEngineering | select hotfixid     

echo $computername      
echo $HotfixID

if($hotfixes -match $hotfixID) {
   $hotfixID = $HotfixID.Replace("KB","")
   Write-host "Found the hotfix KB" + $HotfixID
   Write-Host "Uninstalling the hotfix"
   $UninstallString = "cmd.exe /c wusa.exe /uninstall /KB:$hotfixID /quiet /norestart"
   ([WMICLASS]"\\$computername\ROOT\CIMV2:win32_process").Create($UninstallString) | out-null            


       Start-Sleep 6
       Write-Host "Waiting for update removal to finish ..."

write-host "Completed the uninstallation of $hotfixID"
}
else {            

write-host "Given hotfix($hotfixID) not found"
return
}            

}

# Remove hash below to run on certain asset and remove certain kb
Uninstall-HotFix -ComputerName TeacherPC -HotfixID KB3008923

 

I am trying to run this against a list of computers from a text file, so I found this one as an example that gets bios info...

 

$a = Get-Content "H:\Scripts\Powershell\Run command from text list\MachinesWithHotfix.txt"

foreach ($i in $a)
   {$i + "`n" + "=========================="; Get-WMIObject Win32_BIOS -computername $i}

 

So I tried to cram these two together to get the hotfix removal to run against a list like this...

 

function Uninstall-Hotfix {
[cmdletbinding()]
param(
$computername = $env:computername,
[string] $HotfixID
)            

$hotfixes = Get-WmiObject -ComputerName $computername -Class Win32_QuickFixEngineering | select hotfixid     

echo $computername      
echo $HotfixID

if($hotfixes -match $hotfixID) {
   $hotfixID = $HotfixID.Replace("KB","")
   Write-host "Found the hotfix KB" + $HotfixID
   Write-Host "Uninstalling the hotfix"
   $UninstallString = "cmd.exe /c wusa.exe /uninstall /KB:$hotfixID /quiet /norestart"
   ([WMICLASS]"\\$computername\ROOT\CIMV2:win32_process").Create($UninstallString) | out-null            


       Start-Sleep 6
       Write-Host "Waiting for update removal to finish ..."

write-host "Completed the uninstallation of $hotfixID"
}
else {            

write-host "Given hotfix($hotfixID) not found"
return
}            

}


$a = Get-Content "H:\Scripts\Powershell\Run command from text list\MachinesWithHotfix.txt"

foreach ($i in $a)
   {$i + "`n" + "=========================="; Uninstall-HotFix -ComputerName $i -HotfixID KB3008923 > H:\Scripts\Powershell\Run command from text list\HotfixRemoved.txt
   }

 

But this gives me an error ...

 

Uninstall-Hotfix : A positional parameter cannot be found that accepts argument 'command'.

At H:\Scripts\Powershell\Run command from text list\RemoveHotfixFromList.ps1:39 char:6

+ {Uninstall-HotFix -ComputerName $i -HotfixID KB3008923 > H:\Scripts\Powershe ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidArgument: (:) [uninstall-Hotfix], ParameterBindingException

+ FullyQualifiedErrorId : PositionalParameterNotFound,Uninstall-Hotfix

 

Am I going about this the wrong way?

 

Can anyone suggest another way to carry out this task?

 

Thanks :D

Posted

foreach ($i in $a)
   {$i + "`n" + "=========================="; Uninstall-HotFix -ComputerName $i -HotfixID KB3008923 > H:\Scripts\Powershell\Run command from text list\HotfixRemoved.txt
   }

 

Should have the output redirect string in quotation marks as the path has spaces in it.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...