Comment créer un service Windows dans Powershell pour le compte" Network Service"?

je veux créer un service Windows en utilisant Powershell. La créer pour un utilisateur donné est facile. J'ai utilisé cette fonction adaptée de ici.

function ReinstallService1 ($serviceName, $binaryPath, $login, $pass)
{  
    Write-Host "installing service"
    # creating credentials which can be used to run my windows service
    $secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
    $mycreds = New-Object System.Management.Automation.PSCredential ($login, $secpasswd)

    # creating widnows service using all provided parameters
    New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic -credential $mycreds
    Write-Host "installation completed"
}

Ma question est: comment créer un compte "SERVICE réseau"?

Si je modifie New-Service ligne et de supprimer les informations d'identification de paramètre, le service est créé pour "Système Local" compte. Près de la miss.

New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic 

j'ai googlé un lot et Je n'ai vu aucun moyen d'indiquer un compte de service. Si je tente d'utiliser le paramètre d'informations d'Identification pour l'utilisateur "NETWORK SSERVICE", Je ne sais pas quel mot de passe mettre et si j'en invente un (juste au cas où cmdlet l'ignore) ça ne marche pas. L'erreur est:

nouveau service: Service ' XXXX (XXXX)' ne peut pas être créé en raison de l'erreur suivante: est Le nom du compte invalide ou n'existe pas, ou le mot de passe est invalide pour le compte nom spécifié

23
demandé sur Oscar Foley 2013-02-05 17:37:25

3 réponses

Le nom du compte est NT AUTHORITY\NETWORK SERVICE.

13
répondu Ansgar Wiechers 2013-02-05 22:35:18

Ceci est la version finale du service de réinstallation pour le bénéfice de tous, spécialement pour Aniket.

function ReinstallService ($serviceName, $binaryPath, $description, $login, $password, $startUpType)
{
        Write-Host "Trying to create service: $serviceName"

        #Check Parameters
        if ((Test-Path $binaryPath)-eq $false)
        {
            Write-Host "BinaryPath to service not found: $binaryPath"
            Write-Host "Service was NOT installed."
            return
        }

        if (("Automatic", "Manual", "Disabled") -notcontains $startUpType)
        {
            Write-Host "Value for startUpType parameter should be (Automatic or Manual or Disabled) and it was $startUpType"
            Write-Host "Service was NOT installed."
            return
        }

        # Verify if the service already exists, and if yes remove it first
        if (Get-Service $serviceName -ErrorAction SilentlyContinue)
        {
            # using WMI to remove Windows service because PowerShell does not have CmdLet for this
            $serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name='$serviceName'"

            $serviceToRemove.delete()
            Write-Host "Service removed: $serviceName"
        }

        # if password is empty, create a dummy one to allow have credentias for system accounts: 
        #NT AUTHORITY\LOCAL SERVICE
        #NT AUTHORITY\NETWORK SERVICE
        if ($password -eq "") 
        {
            $secpassword = (new-object System.Security.SecureString)
        }
        else
        {
            $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
        }
        $mycreds = New-Object System.Management.Automation.PSCredential ($login, $secpasswd)

        # Creating Windows Service using all provided parameters
        Write-Host "Installing service: $serviceName"
        New-Service -name $serviceName -binaryPathName $binaryPath -Description $description -displayName $serviceName -startupType $startUpType -credential $mycreds

        Write-Host "Installation completed: $serviceName"

        # Trying to start new service
        Write-Host "Trying to start new service: $serviceName"
        $serviceToStart = Get-WmiObject -Class Win32_Service -Filter "name='$serviceName'"
        $serviceToStart.startservice()
        Write-Host "Service started: $serviceName"

        #SmokeTest
        Write-Host "Waiting 5 seconds to give time service to start..."
        Start-Sleep -s 5
        $SmokeTestService = Get-Service -Name $serviceName
        if ($SmokeTestService.Status -ne "Running")
        {
            Write-Host "Smoke test: FAILED. (SERVICE FAILED TO START)"
            Throw "Smoke test: FAILED. (SERVICE FAILED TO START)"
        }
        else
        {
            Write-Host "Smoke test: OK."
        }

}
7
répondu Oscar Foley 2018-03-17 01:58:28

Vous pourriez obtenir un cred de services de réseau simple comme ceci:

$login = "NT AUTHORITY\NETWORK SERVICE"
#### #just set a dummy psw since it's just used to get credentials

$psw = "dummy"

$scuritypsw = ConvertTo-SecureString $psw -AsPlainText -Force

$mycreds = New-Object System.Management.Automation.PSCredential($login, $scuritypsw)
#### #then you can use the cred to new a windows service

$serviceName = "Test"
$binaryPath = "C:\Test\Test.exe"

New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic -credential $mycreds
5
répondu Summer 2015-07-22 22:14:53