CreateWinServices

From Terminal23wiki

Jump to: navigation, search
function CreateWinServices
{
      #function creates services on local server for PCentral (or possibly others)
      #strServiceName has no special chars, no spaces, and is also used as displayname
      #strStartupType should be Automatic or Manual or Disabled
      #strFilePath should be in local context to target server; absolute path
      #strAccountName should be like DOMAIN\testaccount
	
   param ([string]$strServiceName,
   [string]$strFilePath,
   [string]$strStartupType,
   [string]$strAccountName,
   [string]$strPlainPassword)
	
      #may want to validate inputs and check if service already exists
      #however, if it already exists, the error is non-terminating; the function will finish
	
   $strSecurePassword = convertto-securestring $strPlainPassword -asplaintext -force
      #if this securestring is passed into us, we just use it in the $cred = new-object below
	
   $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $strAccountName,$strSecurePassword
   new-service $strServiceName $strFilePath -startuptype $strStartupType -credential $cred
	
   if ($strStartupType -eq "Automatic")
   {start-service $strServiceName}
}
Personal tools