Author Posts

March 6, 2017 at 8:55 am

Hello everybody,

How can i check remote service status with username and password?

ServiceName=Tomcat7
ComputerName=BuildServer
Username=kahveci
Domainname=testdomain
Password=kahvecitest

March 6, 2017 at 10:18 am

You can create a PSSession and run Invoke-Command against it:

$Splat           = @{
    TypeName     = "System.Management.Automation.PSCredential"
    ArgumentList = $Username, ( $Password | ConvertTo-SecureString -AsPlainText -Force )
}

$Credential = New-Object @Splat
$Session    = New-PSSession -ComputerName $ComputerName -Credential $Credential

Invoke-Command -Session $Session -ScriptBlock { Get-Service -Name $Using:ServiceName }

March 6, 2017 at 11:30 am

Thanks Dudebro for your answer but the codes did not run.

Errors:
[iparabuildersrv] Connecting to remote server failed with the following error message : WinRM cannot process the reques
t. The following error occured while using Kerberos authentication: There are currently no logon servers available to s
ervice the logon request.
Possible causes are:
-The user name or password specified are invalid.
-Kerberos is used when no authentication method and no user name are specified.
-Kerberos accepts domain user names, but not local user names.
-The Service Principal Name (SPN) for the remote computer name and port does not exist.
-The client and remote computers are in different domains and there is no trust between the two domains.
After checking for the above issues, try the following:
-Check the Event Viewer for events related to authentication.
-Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or us
e HTTPS transport.
Note that computers in the TrustedHosts list might not be authenticated.
-For more information about WinRM configuration, run the following command: winrm help config. For more information,
see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [], PSRemotingTransportExc
eption
+ FullyQualifiedErrorId : PSSessionOpenFailed
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Supply an argument tha
t is not null or empty and then try the command again.
At line:7 char:24
+ Invoke-Command -Session <<<< $Session -ScriptBlock { Get-Service -Name Tomcat7 } + CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

March 6, 2017 at 3:38 pm

Ali,

There's a couple of ways I would do this:
Option 1. Since you have the name and password of the user who has the necessary rights, you can simply start a PS session under that credential:

$Username     = 'kahveci'
$DomainName   = 'testdomain'
# Never type in passwords in scripts

Start-Process  'c:\windows\system32\WindowsPowerShell\v1.0\PowerShell.exe' -Credential "$DomainName\$Username"

Then run the Get-Service cmdlet in the new PS window:

whoami
# shows you're running under testdomain\kahveci crdentials

$ServiceName  = 'Tomcat7'
$ComputerName = 'BuildServer'
Get-Service -ComputerName $ComputerName -Name $ServiceName

Option 2. Use PS remoting:

$ServiceName  = 'Tomcat7'
$ComputerName = 'BuildServer'
$Username     = 'kahveci'
$DomainName   = 'testdomain'
# Never type in passwords in scripts

try {
    $Session = New-PSSession -ComputerName $ComputerName -Credential "$DomainName\$Username" -ErrorAction Stop
    Invoke-Command -Session $Session -ScriptBlock { Get-Service -Name $Using:ServiceName }
} catch {
    "Failed to establish PS session with computer '$ComputerName' using the credential '$DomainName\$Username'" 
    $_
}

$Session | Remove-PSSession

March 7, 2017 at 8:15 am

Thank you Sam for your answer,

Option 2. Use PS remoting is running succesfully. I can see service status. But i need to set service status to new variable then i will use service status in if condition. How can i set service status to new variable?

March 7, 2017 at 10:15 am

This PS object will contain the needed information:


$ServiceName  = 'Tomcat7'
$ComputerName = 'BuildServer'
$Username     = 'kahveci'
$DomainName   = 'testdomain'
# Never type in passwords in scripts

try {
    $Session = New-PSSession -ComputerName $ComputerName -Credential "$DomainName\$Username" -ErrorAction Stop
    $Result = Invoke-Command -Session $Session -ScriptBlock { Get-Service -Name $Using:ServiceName }
    New-Object -TypeName PSObject -Property @{
        ComputerName       = $ComputerName
        ServiceName        = $Result.Name
        ServiceDisplayName = $Result.DisplayName
        ServiceStatus      = $Result.Status
    } | select ComputerName, ServiceName, ServiceDisplayName, ServiceStatus 
} catch {
    "Failed to establish PS session with computer '$ComputerName' using the credential '$DomainName\$Username'" 
    $_
}

$Session | Remove-PSSession