Script throwing an error after starting the service - windows

I have written the below code to start the service:
invoke-command -cn $server -Credential $cred -ScriptBlock {
param($svc)
if((get-service $svc).status -ne "running") {
get-service $svc| start-service
set-service $svc -StartupType Automatic
(get-service $svc).waitforstatus('running')
}
get-service $svc| select ServiceName
} -ArgumentList $svc
After executing the above script, I am getting below error:
Status : Running
StartType : Automatic
ServiceName : svcname
PSComputerName : host1
+ invoke-command -cn $server -Credential $cred -ScriptBlock {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
+ FullyQualifiedErrorId : StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand
I see service is Running successfully so why it is throwing an error even when the service was started correctly?
I am using poweshell 5

Continuing from my comments, try:
Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock {
param($svc)
$theService = Get-Service -Name $svc
if($theService.Status -ne 'Running') {
if ($theService.Status -ne 'Stopped') { $theService | Stop-Service -Force }
$theService | Set-Service -StartupType Automatic
($theService | Start-Service -PassThru).WaitForStatus('Running')
}
$theService | Select-Object ServiceName
} -ArgumentList $svc

Related

How to find, stop and disable a Windows service using Powershell

I am trying to find a service, stop it and then disable it remotely using Powershell. It can find and stop but cannot disable. For disabling, I have to run the Set-Service command separately. Can it be done in one line?
The following code-snippet will stop the Print Spooler service, but will not disable it:
$ip = "10.10.10.10"
$svc_name = "Spooler"
get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} | Stop-Service | Set-Service -StartupType Disabled
The following code-snippet will stop and disable the Print Spooler service:
$ip = "10.10.10.10"
$svc_name = "Spooler"
get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} | Stop-Service
Set-Service $svc_name -StartupType Disabled
Powershell version is 5.1.14393.2969.
Edit:
The following line will also find and disable. So, it looks like I can give two instructions with pipeline.
get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} | Set-Service -StartupType Disabled
You need to use Set-Service to set the startup type, as outlined in your question:
Set-Service -StartupType Disabled $svc_name
If you want to do it in "one line", you can use the -PassThru argument on Stop-Service to return the service object which can then be sent down the pipeline (you also don't need a Where-Object clause, Get-Service can filter on service name as well):
Get-Service -ComputerName $ip $svc_name | Stop-Service -PassThru | Set-Service -StartupType Disabled
You had this close in your original question, but it didn't work because you didn't use the-PassThru parameter on Stop-Service. As a note, many cmdlets that don't return an object by default do include a -PassThru parameter to return an object that can further processed if necessary, this isn't limited to Stop-Service by any means.
Bender's answer works in PowerShell 5.1, but the -ComputerName parameter was removed from the Get-Service cmdlet in PowerShell 6+. If you're trying to do this in pwsh.exe (i.e. PowerShell 6+), you can use code like the following:
[string[]] $servers = #('server1', 'server2, 'server3')
[scriptblock] $disableServiceScriptBlock = {
[string] $serviceName = 'SERVICE NAME TO DISABLE GOES HERE'
Stop-Service -Name $serviceName
Set-Service -Name $serviceName -StartupType Disabled
}
Invoke-Command -ComputerName $servers -ScriptBlock $disableServiceScriptBlock
Here's a longer code snippet with better error reporting so you know what server an error occurred on:
[string[]] $servers = #('server1', 'server2, 'server3')
[scriptblock] $disableServiceScriptBlock = {
[string] $serviceName = 'SERVICE NAME TO DISABLE GOES HERE'
Stop-Service -Name $serviceName -ErrorVariable stopError -ErrorAction SilentlyContinue
Set-Service -Name $serviceName -StartupType Disabled -ErrorVariable disableError -ErrorAction SilentlyContinue
# If an error occurred, report which server it occurred on with the error message.
[string] $computerName = $Env:ComputerName
if ($stopError)
{
Write-Error "$computerName : Stop Error: $stopError"
}
if ($disableError)
{
Write-Error "$computerName : Disable Error: $disableError"
}
}
Invoke-Command -ComputerName $servers -ScriptBlock $disableServiceScriptBlock
***Stopping both Windows Firewall and Windows Defender Firewall***
$date = get-date -uformat "%m%d%y-%H"
$day = Get-Date -Format yyyyMMdd
$dayold = Get-Date -Format "%M%d%y"
$today = (Get-Date -Format yyyyMMdd)+"-"+(get-date -uformat %H)
$ErrorActionPreference = "SilentlyContinue"
$ServerList = (Get-adcomputer -SearchBase "OU=site,OU=servers,DC=subdomain,DC=domain,DC=root" -filter {name -like "*cont*ext*"} -SearchScope Subtree -Properties Name) |select name
$ServerList=$ServerList.name
(Test-Connection -ComputerName $env:LOGONSERVER.Remove(0,2) -Count 1 -quiet)|Out-Null
foreach ($server in $ServerList){
if(Test-Connection -ComputerName $server -Count 1 -quiet){
$result = (get-service -ComputerName $server -name MpsSvc |select *)
if($result.Status -eq "Running")
{
get-service -ComputerName $server -name MpsSvc |stop-service -Force
get-service -ComputerName $server -name MpsSvc |set-service -ComputerName $server -StartupType Disabled
}
elseif($result.StartType -ne "Disabled"){
set-service -ComputerName $server -name MpsSvc -StartupType "Disabled"
}
$result = (get-service -ComputerName $server -name MpsSvc |select *)
$server+": "+"The "+$result.DisplayName+" is "+$result.Status+" and "+$result.StartType
}
}

Container Build Agents fail saying 'missing dll' TFS 2018 windows

I just set up a few containerized agents and I'm running into build issues with few regression tests which work fine on regular build agents.
Was wondering if anyone had issues with containerized build agents?
Error Log:
Add-Type : (0) : Unable to find messages file 'cscui.dll' (1) : namespace VstsTaskSdk.FS At C:\tfsagent_work_tasks\VSBuild_71a9a2d3-a98a-4caa-96ab-affca411ecda\1.119.0\ps_modules\VstsTaskSdk\LongPathFunctions.ps1:212 char:1 + Add-Type -Debug:$false -TypeDefinition #' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
#
Unable to find type [VstsTaskSdk.TerminationException]. Exit code 1
returned from process: file name
'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe', arguments
'-NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted
-Command ". ([scriptblock]::Create('if (!$PSHOME) { $null = Get-Item -LiteralPath ''variable:PSHOME'' } else { Import-Module -Name ([System.IO.Path]::Combine($PSHOME,
''Modules\Microsoft.PowerShell.Management\Microsoft.PowerShell.Management.psd1''))
; Import-Module -Name ([System.IO.Path]::Combine($PSHOME,
''Modules\Microsoft.PowerShell.Utility\Microsoft.PowerShell.Utility.psd1''))
}')) 2>&1 | ForEach-Object { Write-Verbose $_.Exception.Message
-Verbose } ; Import-Module -Name 'C:\tfsagent_work_tasks\VSBuild_71a9a2d3-a98a-4caa-96ab-affca411ecda\1.119.0\ps_modules\VstsTaskSdk\VstsTaskSdk.psd1'
-ArgumentList #{ NonInteractive = $true } -ErrorAction Stop ; $VerbosePreference = 'SilentlyContinue' ; $DebugPreference =
'SilentlyContinue' ; Invoke-VstsTaskScript -ScriptBlock
([scriptblock]::Create('.
''C:\tfsagent_work_tasks\VSBuild_71a9a2d3-a98a-4caa-96ab-affca411ecda\1.119.0\VSBuild.ps1'''))"'.

Powershell's Invoke-Command won't take in a variable for -ComputerName parameter

I just can't seem to get this to work, and I can't figure out how to google this issue. similar script is working remotly but now i need to made it work localy. But... Please check the script...
Function Local-Install {
$ComputerName = "$env:computername"
$AppName = "Deployment"
Invoke-Command -ComputerName $ComputerName ,$AppName -ScriptBlock `
{
param ($ComputerName,$AppName)
write-host "Getting Parameters for '$AppName' on $ComputerName"}
$Application = Get-WmiObject -computername $ComputerName -Namespace "root\ccm\ClientSDK" -Class CCM_Application | where {$_.Name -like "$AppName"} | Select-Object Id, Revision, IsMachineTarget
$AppID = $Application.Id
$AppRev = $Application.Revision
$AppTarget = $Application.IsMachineTarget
([wmiclass]'ROOT\ccm\ClientSdk:CCM_Application').Install($AppID, $AppRev, $AppTarget, 0, 'Normal', $False)
}
and i get an error like this:
Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects ins
tead of strings.
At line:5 char:1
+ Invoke-Command -ComputerName $ComputerName ,$AppName -ScriptBlock `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
+ FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand
Exception calling "Install" : ""
At line:13 char:1
+ ([wmiclass]'ROOT\ccm\ClientSdk:CCM_Application').Install($AppID, $AppRev, $AppTa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WMIMethodException
Since this is local you could just run the "env:COMPUTERNAME" call in the method. However, if you want to get this to work as is, you just need to add the -ArgumentList argument to the Invoke-Command call:
Invoke-Command -ComputerName $ComputerName, $AppName -ArgumentList $ComputerName, $AppName -ScriptBlock `

Powershell: How to use Invoke-WmiMethod to Beep a Remote Computer?

I am trying to use the following code to make a beep on a remote computer through Powershell:
Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "[console]::beep(500,300)" -ComputerName "mycompname"
In addition I have used [System.Media.SystemSounds]::Beep.Play() in place of the console command.
It doesn't give any error codes and outputs this:
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ProcessId :
ReturnValue : 9
PSComputerName :
I am running this command with elevated Powershell and I am an admin on the network. Using Invoke-Command DOES NOT work on my computer, so I am opting for Invoke-WmiMethod instead. The following code DOES actually work, so I don't understand why the beep one won't:
Invoke-WmiMethod -path Win32_Process -Name Create -ArgumentList "msg * 'hello'" -ComputerName "mycompname"
Final notes: I would like to be able to use Invoke-WmiMethod to do remote shutdown and taskkill, but those functions also do not work, only sending a message works. Any help would be greatly appreciated.
May be can you try this (modify username and password)
$Username = 'labuser'
$Password = 'labuser'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-command –computername "mycompname" -credential $Cred –scriptblock {[console]::beep(500,300)}
TessellatingHeckler was right. I just needed to change the code so that the receiving computer knows that I am using powershell:
Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "powershell.exe [console]::beep(500,300)" -ComputerName "mycompname"
This also works for everything else:
Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "powershell.exe shutdown -s" -ComputerName "mycompname"
Thanks!

'Session' Parameter is null or empty in PowerShell script

I'm very new to PowerShell (forgive my ignorance) and am trying to install a program remotely on multiple computers on my domain. Currently, I'm just trying to get it to work on one computer. The script below was found online and adapted for my needs. Yesterday it worked, but today it's complaining about the session parameter.
I don't fully understand "sessions", but I have ensured on the client machine that the winrm service is running and I have invoked Enable-PSRemoting -force.
Here's the script:
$computers = Get-Content "c:\tmpPS\computers.txt"
$rs = Get-PSSession
Get-PSSession | Get-Member
######
## Functions
################
foreach ($comp in $computers)
{
Write-Host "should work with $comp"
}
PushMSI
RemoteConnect
InstallMSI
Function PushMSI {
Write-Host "------------------------------------------------"
Write-Host "This will copy the MSI file from localhost c:\tmpPS\"
write-Host "------------------------------------------------"
Write-Host ""
Write-Host ""
foreach ($comp in $computers)
{
Copy-Item -path "c:\tmpPS\clientInstall.msi" -Destination \\$comp\c$\tmpPS
}
}
Function RemoteConnect
{
Write-Host "------------------------------------------------"
Write-Host "This will establish a PSSession with all computers in c:\temp\computers.txt"
write-Host "------------------------------------------------"
Write-Host ""
Write-Host ""
Get-Content C:\tmpPS\computers.txt | New-PSSession -ThrottleLimit 50
}
Function InstallMSI
{
Write-Host "------------------------------------------------"
Write-Host "This will Install UPS Update on all computers with an Established PSSession"
write-Host "------------------------------------------------"
Write-Host "After the Install PSSessions will be removed"
Write-Host ""
Invoke-Command -Session $rs -ScriptBlock {invoke-item "c:\tmpPS\ClientInstall.msi"}
}
Get-PSSession | Remove-PSSession
And here's the output:
PS C:\Users\Me> C:\tmpPS\remoteInstall.ps1
Get-Member : No object has been specified to the get-member cmdlet.
At C:\tmpPS\remoteInstall.ps1:3 char:17
+ Get-PSSession | Get-Member
+ ~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException
+ FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand
should work with eSignWin81.informa.local
------------------------------------------------
This will copy the MSI file from localhost c:\tmpPS\
------------------------------------------------
------------------------------------------------
This will establish a PSSession with all computers in c:\temp\computers.txt
------------------------------------------------
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
6 Session6 eSignWin81.i... Opened Microsoft.PowerShell Available
------------------------------------------------
This will Install UPS Update on all computers with an Established PSSession
------------------------------------------------
After the Install PSSessions will be removed
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Supply an argument that is not null or empty and then try
the command again.
At C:\tmpPS\remoteInstall.ps1:49 char:25
+ Invoke-Command -Session $rs -ScriptBlock {invoke-item "c:\tmpPS\ClientInstall.ms ...
+ ~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
Try not to use Write-Host as it brakes the pipeline and kills puppy's.
I've tried to improve your script a little bit, so you understand the logic behind the structure better. It hasn't been tested, but it should do the trick.
# First parameters
[CmdletBinding()]
Param(
[ValidateScript({Test-Path $_ -PathType leaf})]
$ComputerList = "c:\tmpPS\computers.txt",
[ValidateScript({Test-Path $_ -PathType leaf})]
$MSI = "c:\tmpPS\clientInstall.msi"
)
# Then functions
Begin {
Function Copy-MSI {
foreach ($Com in $Computers) {
Copy-Item -path $MSI -Destination "\\$Com\c$\tmpPS"
}
}
Function Install-MSI {
foreach ($Com in $Computers) {
Enter-PSSession -ComputerName $Com
invoke-item "c:\tmpPS\ClientInstall.msi"
Exit-PSSession
}
}
}
# Then the actions
Process {
$Computers = Get-Content $ComputerList
Copy-MSI
Install-MSI
}
If you run this, you'll find the information you're looking for:
Get-Help Enter-PSSession

Resources