locked
Powershell script deployment options in CM2012 RRS feed

  • Question

  • Hello all,

    I really don't know anything about PowerShell, but I found this interesting script to help with client issues. It references an input file that I can modify. My silly question is, what is the best way to use this script in CM2012? Try to create a package and use this, create a task sequence or query? I am unsure how to use this. Any help/guidance/links would be greatly appreciated! Thanks! 

    #ERROR REPORTING ALL
    Set-StrictMode -Version latest
    
    #----------------------------------------------------------
    #STATIC VARIABLES
    #----------------------------------------------------------
    $SCRIPT_PARENT   = Split-Path -Parent $MyInvocation.MyCommand.Definition
    
    #----------------------------------------------------------
    #FUNCTION RepairSCCM
    #----------------------------------------------------------
    Function Repair_SCCM
    {
      Write-Host "[INFO] Get the list of computers from the input file and store it in an array."
      $arrComputer = Get-Content ($SCRIPT_PARENT + "\input.txt")
      Foreach ($strComputer In $arrComputer)
      {
    	#Put an asterisk (*) in front of the lines that need to be skipped in the input file.
        If ($strComputer.substring(0,1) -ne "*")
    	{
    	  Write-Host "[INFO] Starting trigger script for $strComputer."
    	  Try
    	  {
    		$getProcess = Get-Process -Name ccmrepair* -ComputerName $strComputer
    		If ($getProcess)
    		{
    		  Write-Host "[WARNING] SCCM Repair is already running. Script will end."
    		  Exit 1
    		}
    		Else
    		{
    		  Write-Host "[INFO] Connect to the WMI Namespace on $strComputer."
    		  $SMSCli = [wmiclass] "\\$strComputer\root\ccm:sms_client"
    		  Write-Host "[INFO] Trigger the SCCM Repair on $strComputer."
    		  # The actual repair is put in a variable, to trap unwanted output.
    		  $repair = $SMSCli.RepairClient()
    		  Write-Host "[INFO] Successfully connected to the WMI Namespace and triggered the SCCM Repair on $strComputer."
    		  ########## START - PROCESS / PROGRESS CHECK AND RUN
    		  # Comment the lines below if it is unwanted to wait for each repair to finish and trigger multiple repairs quickly.
    		  Write-Host "[INFO] Wait (a maximum of 7 minutes) for the repair to actually finish."
    		  For ($i = 0; $i -le 470; $i++)
    		  {
    			$checkProcess = Get-Process -Name ccmrepair* -ComputerName $strComputer
    			Start-Sleep 1
    			Write-Progress -Activity "Repairing client $strComputer ..." -Status "Repair running for $i seconds ..."
    			
    		    If ($checkProcess -eq $Null)
    			{
    			  Write-Host "[INFO] SCCM Client repair ran for $i seconds."
    			  Write-Host "[INFO] SCCM Client repair process ran successfully on $strComputer."
    			  Write-Host "[INFO] Check \\$strComputer\c$\Windows\SysWOW64\CCM\Logs\repair-msi%.log to make sure it was successful."
    			}
    			ElseIf ($i -eq 470)
    			{
    			  Write-Host "[ERROR] Repair ran for more than 7 minutes. Script will end and process will be stopped."
    			  Invoke-Command -Computer $strComputer { Get-Process -Name ccmrepair* | Stop-Process -Force }
    			  Exit 1
    			}
    		  }
    		  ########## END - PROCESS / PROGRESS CHECK AND RUN
    
    		}
    	  }
    	  Catch
    	  {
    		Write-Host "[WARNING] Either the WMI Namespace connect or the SCCM Repair trigger returned an error."
    		Write-Host "[WARNING] This is most likely caused, because there is already a repair trigger running."
    		Write-Host "[WARNING] Wait a couple of minutes and try again."
    		# If the script keeps throwing errors, the WMI Namespace on $strComputer might be corrupt.
    	  }
        }
      }
    }
    # RUN SCRIPT 
    Repair_SCCM 
    #Finished
    


    DM

    Wednesday, November 19, 2014 6:28 PM

Answers

  • Copy, thanks. I thought since this was scripting I might try here. Just run it. Does the script and text file need to be in the same folder for the script to reference it?

    Thanks again.


    DM

    Yep, the input file should be input.txt and it should be in the same folder as the script itself.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    • Marked as answer by DM2012MS Wednesday, November 19, 2014 8:36 PM
    Wednesday, November 19, 2014 8:13 PM

All replies