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"))
Related
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?
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
I am writing a script where I need to check specific applications if installed or not on a remote server. I am fairly new to PowerShell, but here is the script I have written
ForEach ($computers in $computer){
Get-WMIObject -Class win32_product -Filter {Name like "%Microsoft%"} -ComputerName $computers -ErrorAction STOP | Select-Object -Property Name,Version | export-csv "C:\progrms.csv"
}
The problem I am facing here is I can get details of only Microsoft application which are installed and if I give multiple names for filter parameter like "Microsoft", "SQL", "app1" so on. This script does not seem to work.
Please let me know what has gone wrong and what needs to be done in order to get the list of specific software's that are installed. Also, note that I will be using this script for both Windows 2008 and 2012 servers.
Remove the -Filter {Name like "%Microsoft%"} part of the script to have it return all installed software.
You probably want to be doing ForEach ($computer in $computers) rather than the other way around, assuming you're creating a $computers variable somewhere above this code with the list of computer names.
You also need to -Append to the Export-CSV command as otherwise each computers output will overwrite the output of the previous, however another issue you'll then have is knowing which software comes from which computer. You can address this by using a Calculated Property to add in the computer name to the output, as follows:
ForEach ($Computer in $Computers){
Get-WMIObject -Class win32_product -ComputerName $Computer -ErrorAction Stop |
Select-Object -Property #{N='Computer';E={$Computer}},Name,Version |
Export-Csv "C:\progrms.csv" -Append
}
I'm running a script to gather info for running apps on a terminal server. I would like to get the remote ComputerName of the user running the app but I'm only able to get the ComputerName of the Terminal Server instead. Any idea on how I could achieve that? Here's the part of my script to gather the info.
Get-WmiObject Win32_Process -ComputerName MYTSSERVER | Where-Object {
$_.mainwindowhandle -ne 0 -and
$_.Name -eq "WINWORD.exe"
} | Select-Object CSName, ProcessName
You can't get the client hostname or IP address from the Word process, because that process is running locally on the Remote Desktop server. Remote Desktop login information is recorded in the eventlog, so you need to extract it from there. However, you need the remote username for finding the correct logon session, so you still need to run the WMI query first.
Example for a single Word process running on the Remote Desktop server:
$server = '...'
$p = Get-WmiObject Win32_Process -Computer $server -Filter "Name='WINWORD.exe'"
$u = $p.GetOwner().User
$fltr = "*logon type:`t`t`t10*new logon:*account name:`t`t$u"
$addr = Get-EventLog -Computer $server -LogName Security -InstanceId 4624 -Message $fltr -Newest 1 |
Where-Object { $_.Message -match 'source network address:\s*(.*)' } |
ForEach-Object { $matches[1] }
The filter for the eventlog query matches the most recent Remote Desktop login (event ID 4626, logon type 10) of the user who started the Word process (account name:`t`t$u).
That will just give you the client's IP address, though. If you require the hostname you need to resolve it via a reverse DNS lookup or get it from the host itself with another WMI query:
$hostname = Get-WmiObject Win32_OperatingSystem -Computer $addr |
Select-Object -Expand CSName
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.