StartCCMexec.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 33c1e0df-70b0-4dd6-ac60-dc7d9e5b9ecc
 
.AUTHOR Bindusar_Kushwaha
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 This is to help SCCM Admins
 
#>
 

<# DISCLAIMER STARTS
This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.
THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO #THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and #distribute the object code form of the Sample Code, provided that You agree:
(i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded;
(ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and
(iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.
This sample script is not supported under any Microsoft standard support program or service.
The sample script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or #documentation, even if Microsoft has been advised of the possibility of such damages"
DISCLAIMER ENDS #>


#enter the location of machine list text file below
$MachinelistLocation='C:\Temp\MachineList.txt'

#creating Log file at above location
$loglocation= Split-Path $MachinelistLocation
$logFile=$loglocation+"\CCMexecStart-"+(Get-Date -Format yyyyMMdd-HHmmssffff)+".log"
$Error.Clear()

#Starting Logging
(Get-Date -Format yyyyMMdd-HHmmssffff)+ " Starting Script..." >> $logFile
(Get-Date -Format yyyyMMdd-HHmmssffff)+ " "+$Error[0]+ " ..." >> $logFile

$Machinelist=Get-Content -Path $MachinelistLocation

(Get-Date -Format yyyyMMdd-HHmmssffff)+ " done with Reading text file... executing them one by one" >> $logFile

foreach($machine in $Machinelist)
{
    $flag=0
    (Get-Date -Format yyyyMMdd-HHmmssffff)+ " Checking status for $machine..." >> $logFile

    if ((get-service -name ccmexec -ComputerName $machine).StartType -eq 'Disabled')
    {
        (Get-Date -Format yyyyMMdd-HHmmssffff)+ " CCMexec Service on $machine is DISABLED... Changing to Automatic." >> $logFile
        Set-service -Name CcmExec -StartupType Automatic -Status Running -ComputerName $machine
 
        (Get-Date -Format yyyyMMdd-HHmmssffff)+ " Starting the Service as well" >> $logFile
        $flag=1
    }


    elseif ((get-service -name ccmexec -ComputerName $machine).Status -ne 'Running')
    {
        (Get-Date -Format yyyyMMdd-HHmmssffff)+ " CCMexec Service on $machine is NOT Running... Starting Now." >> $logFile
        Set-service -Name CcmExec -Status Running -ComputerName $machine
        $flag=1
    }


    elseif ((get-service -name ccmexec -ComputerName $machine).Status -eq 'Running')
    {
        (Get-Date -Format yyyyMMdd-HHmmssffff)+ " CCMexec Service on $machine is ALREADY Running... SKIPPING IT." >> $logFile
    }

    (Get-Date -Format yyyyMMdd-HHmmssffff)+ " "+$Error[0]+ " ..." >> $logFile

    if(($flag -eq 1) -and (get-service -name ccmexec -ComputerName $machine).Status -eq 'Running')
    {
        (Get-Date -Format yyyyMMdd-HHmmssffff)+ " Successfully started CCMexec Service on $machine..." >> $logFile
    }
}