How to Install windows service using Powershell

How to Install windows service using Powershell

How to install windows service using powershell


$currentpath = $PSScriptRoot;

$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)

# Get the security for the Administrator
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)

# Get the security  for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator

# Check to see if powershell currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
   {
   # We are running "as Administrator" - so change the title and background color to indicate this
   $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
   $Host.UI.RawUI.BackgroundColor = "DarkBlue"
   clear-host
   }
else
   {
   # We are not running "as Administrator" - so relaunch as administrator
 
   # Create a new process object that starts PowerShell
   $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
 
   # Specify the current script path and name as a parameter
   $newProcess.Arguments = $myInvocation.MyCommand.Definition;
 
   # Indicate that the process should be elevated
   $newProcess.Verb = "runas";
 
   # Start the new process
   [System.Diagnostics.Process]::Start($newProcess);
 
   # Exit from the current, unelevated, process
   exit
   }

   $service = $currentpath + ".\ServiceName.exe";
 
 C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe $service


# Run your code that needs to be elevated here
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")




 



EF - The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet

when you try to generate the EF context for SQL database using the "Scaffold-Context" command in EFCore. But you're getting the following error :
Scaffold-DbContext : The term 'Scaffold-DbContext' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.



Solution:

Just install the following "Microsoft.EntityFrameworkCore.Tools" from Nuget by using the following command

Install-Package Microsoft.EntityFrameworkCore.Tools


or right click on the project and select "Manage Nuget Packages" and type in search "Microsoft.EntityFrameworkCore.Tools" select Microsoft.EntityFrameworkCore.Tools like the below image and install.



Featured Post

Convert Json to Classes

Followers