install/uninstall a Windows Service - windows

I have created a Windows Service project using VSTS 2008 Windows Service type project and now I want to write scripts to install/uninstall it using PowerShell.
Any reference samples or documents?

Here's a sanitized version of an install script I wrote. Should demonstrate everything you need to do:
## delete existing service
# have to use WMI for much of this, native cmdlets are incomplete
$service = Get-WmiObject -Class Win32_Service -Filter "Name = 'My Service'"
if ($service -ne $null)
{
$service | stop-service
$service.Delete() | out-null
}
## run installutil
# 'frameworkdir' env var apparently isn't present on Win2003...
$installUtil = join-path $env:SystemRoot Microsoft.NET\Framework\v2.0.50727\installutil.exe
$serviceExe = join-path $messageServerPath MyService.exe
$installUtilLog = join-path $messageServerPath InstallUtil.log
& $installUtil $serviceExe /logfile="$installUtilLog" | write-verbose
$service = Get-WmiObject -Class Win32_Service -Filter "Name = 'My Service'"
# change credentials if necessary
if ($user -ne "" -and $password -ne "")
{ $service.change($null, $null, $null, $null, $null, $null, $user, $password, $null, $null, $null) | out-null }
# activate
$service | set-service -startuptype Automatic -passthru | start-service
write-verbose "Successfully started service $($service.name)"

You didn't mention what language you are using. More than likely, the windows install utility can handle it.

If I understand your question correctly, you first need to create an installer from within VSTS. It's been awhile since I've done one, but it basically looks like this:
http://csharpcomputing.com/Tutorials/Lesson22.htm
Once you have created an installer, you can automate it with PowerShell.
If you really do want PowerShell to be your service installer, there might be a way to automate the windows service installer from PowerShell by using the ServiceInstaller Class.

Related

Enabling remote through CMD or POWERSHELL in WMI

How to enable remote in Windows Management Instrumentation (WMI) on authenticated users on remote computers programmatically through CMD or POWERSHELL?
When I try to write some script then it throws errors like 'access is denied' and 'RPC server is unavailable '.
And when I enable PS Remoting then it is working.
$servers = Get-Content file name
foreach( $server in $servers) {
Write-host "Processing Server $Server"
Get-WmiObject -Class Win32_Product -Impersonation 3 -Credential username -ComputerName $Server | select __SERVER, Name, Version, InstallDate -ErrorAction SilentlyContinue
}
What I want to achieve is, I want to do this through script only not manually. Can anyone please explain?

How to get the full list of applications like in control panel

I have been stuck at this stage of my little project.
What I try to do is list the applications that are installed and choose one of the apps to uninstall, the issue I have is that not all the apps appear, so I can't select them. For example Google chrome is not appearing while I'm using it right now to write this question.
I use this function to get all the apps:
Get-WmiObject Win32_Product -ComputerName $ComputerName | Select-Object -Property Name | Out-GridView -Title "All apps on destination Computer"
and this is whole script:
$ComputerName = Read-Host -Prompt 'Input the computer name' # the name of the computer to remove the app from
Get-WmiObject Win32_Product -ComputerName $ComputerName | Select-Object -Property Name | Out-GridView -Title "All apps on destination Computer"
$Name = Read-Host -Prompt 'Input name of the application (has to be exact name)' #name of the application
$Application = Get-WmiObject Win32_Product -ComputerName $ComputerName | Where-Object {$_.Name -eq $Name} #choose the object, this will be the app that we will delete
if ($Application) {
$Application.Uninstall()
"The removal was successful"
}
else {
$Name + ' is not installed on ' + $ComputerName
}
Start-Sleep -Seconds 10
I'm not that good with PowerShell so excuse me if this is a stupid question
Or get-package, which should be faster. Uninstall-package only works on msi providers. Powershell 5.1 only. Metadata['uninstallstring'] has the uninstall string for Programs providers. It can take wildcards and arrays as arguments. Install-package works with msi files, but without any extra options.
get-package

Install Offline Windows Updates(.msu) to remote servers

I'm trying to get a script together to remotely install some windows updates on some remote servers that are connected in an offline domain.
I have tried regular PS Remoting and after some research, I think what I am trying to do isnt supported by microsoft. When checking my event logs I have a bunch of these errors.
Edit
I wanted to add that I have tried running the .\Install2012R2.ps1 script from my local computer, modified to have the Invoke-Command in that and have it run the update portion of the original Install2012R2.ps1 and I would get the same errors.
I was hoping that by placing the script on each server that it would like that more.
End Edit
Windows update could not be installed because of error 2147942405 "Access is denied."
(Command line: ""C:\Windows\System32\wusa.exe" "C:\Updates\windows8.1-kb4556853-x64.msu" /quiet /norestart")
I have tried running Invoke-Command as credentialed to an administrator account on the servers but I have been having no luck and was looking for some advice if someone has maybe tried/done this before.
$Servers = #("V101-Test1","V101-Test2")
$Username = 'admin'
$Password = 'Password'#not actual password
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Get-PSSession | Remove-PSSession
New-PSSession -ComputerName $Servers
foreach($Server in $Servers){
Get-ChildItem -Path C:\Source\Temp -Recurse | Copy-Item -Destination "\\$Server\c$\Updates\" -Force
}
Invoke-Command $Servers -Credential $Cred -ScriptBlock{
& "C:\Updates\Install2012R2.ps1"
}
EDIT 2
Here is the actual install code of the Install2012R2.ps1 script
$updatedir= "./"
$files = Get-ChildItem $updatedir -Recurse
$msus = $files | ? {$_.extension -eq ".msu"}
$exes = $files | ? {$_.extension -eq ".exe"}
foreach ($file in $msus){
$KBCtr++
$fullname = $file.fullname
# Need to wrap in quotes as folder path may contain space
$fullname = "`"" + $fullname + "`""
$KBN = $fullname.split('-')[1]
# Need to wrap in quotes as folder path may contain space
$fullname = "`"" + $fullname + "`""
# Specify the command line parameters for wusa.exe
$parameters = $fullname + " /quiet /norestart"
# Start services and pass in the parameters
$install = [System.Diagnostics.Process]::Start( "wusa",$parameters )
$install.WaitForExit()
}
I'm not sure why wusa.exe is failing here with Access Denied, but here is a PowerShell-native approach you can try. If nothing else, it should give you a clearer indication via the captured error information as to what the underlying issue is:
Add-WindowsPackage -Path C:\Updates\OurHeroicUpdate.msu -Online -PreventPending -NoRestart
-Path is the path to the msu file
-Online tells Add-WindowsPackage to modify the currently "mounted image" (the running version) of Windows (as opposed to an offline disk image you could also apply it to)
-PreventPending prevents installing the msu if there is already a pending change, like needing to reboot for updates.
Add-WindowsPackage is part of the DISM module available under Windows PowerShell, and is the functional equivalent of dism /packagepath:"cabfile", although it can take an msu where dism.exe only allows a cab.

Powershell - how to show which computers from a list are running a specific process?

Powershell beginner here working in an air-gapped, Win7 environment with Powershell 4.0 so unable to import any modules or do anything sophisticated but wondering how I can achieve generating a txt file of computers on the network that are running a specific process, say wusa.exe for Windows Updates?
I have a txt list of all the computer names already and so far have this:
$computers = gc "C:\PCList.txt"
foreach ($computer in $computers) {Get-process | out-file -Path "C:\TheseAreRunningWusa.txt"}
But obviously that displays ALL processes, any way to cut out everything aside from a specific one but also only list the ones running said process?
Thanks in advance.
The Get-Process command allows you to specify both a remote computer to run on, and what service you are looking for.
$computers = Get-Content "C:\PCList.txt"
$output = #()
foreach ($computer in $computers) {
if(Get-Process "myProcessName" -ComputerName $computer -ErrorAction SilentlyContinue) {
$output += $computer
}
}
$output | Set-Content "C:\TheseAreRunningMyProcess.txt"
Note: I used -ErrorAction SilentlyContinue as Get-Process throws an error if the process is not found.

PowerShell: Restart Service By Executable Name

all
I implemented my first PowerShell script, that does some setup, sets registry keys and at then end needs to restart services. The problem is that I have only have name of the executable, but not service name. Restart-Service can work only with name of the service. Googling (well Binging also) around didn't give me much result.
I was wondering whether there is a way to restart service by executable name?
I know that I can get process by executable name, but just killing the process and starting it again is NOT good choice, since service Start/Stop functions are not called and it may not work properly.
Thanks.
You can try using wmi and do something like this:
(gwmi win32_service | ?{$_.pathname -match "\\executable.exe "}) | Restart-Service
Get-WmiObject -Class Win32_Service -Filter "PathName LIKE '%PartOfTheName%'" -ComputerName PC1 | Foreach-Object{
$_.StopService()
$_.StartService()
}
You can do this using WMI:
$process = Get-Process sqlservr| select -ExpandProperty Id
Get-WmiObject win32_Service|
where {$process -contains $_.ProcessId}|
foreach {Restart-Service $_.Name}
Edit: Changed script to restart service, not just stop it.
#set by logic to determine if the service will restart or not
$global:ServerWillRestart=$true
#can be found using the name column of Get-services cmdlet
$serviceName="Set name of the service"
if($global:ServerWillRestart){
$service =Get-Service | where{ $_.Name -eq $serviceName}
do{
Write-output "The service $ServiceName will is being stopped"
Stop-Service $service
Start-Sleep -s 2
}
while($service.WaitForStatus("Stopped"))
do{
Write-Output "The service $ServiceName will is being started"
Start-Service $service
Start-Sleep -s 2
}
while($service.WaitForStatus("Running"))

Resources