none
Needs help for log output. RRS feed

  • Question

  • Hello 

    I am having problem with my script, I need help to had logging for each server. 

    Now I only get Name, start type, and status. And it is not in .txt file. I also need the computername of the server where the service is stopped. 

    This is my script: 

    #Settings
    $Service = "xSERVICEx"
    $SrvNames = Get-Content -Path "C:\admin\servername.txt"
    
    #Script
    foreach ($Server in $SrvNames)
    {
    	Get-Service -Name $Service -ComputerName $Server | Stop-Service -PassThru | Set-Service -StartupType Disabled
        Get-Service -Name $Service | Select-Object name, Starttype, Status
    }

    Can you guys help me? And yes, I am very new to powershell. 

    From Vegard

    Monday, June 17, 2019 12:11 PM

Answers

  • Found out that this is how we do it. 

    #Settings
    $Service = "xSERVICEx"
    $SrvNames = Get-Content -Path "C:\admin\Servername.txt"
    
    #Script
    $Result = @()
    
    $Result = foreach ($Server in $SrvNames)
    {
    Get-Service -Name $Service -ComputerName $Server | Stop-Service -PassThru | Set-Service -StartupType Disabled
    Get-Service -Name $Service -ComputerName $Server| Select-Object @{Label=”Machinename”;Expression={($Server)}}, name, Starttype, Status 
    }
    
    $Result | Out-GridView -PassThru | out-file C:\admin\test.txt

    • Marked as answer by veghotve Tuesday, June 18, 2019 6:37 AM
    Tuesday, June 18, 2019 6:36 AM

All replies

  • Just add the "MachineName" to the select statement.


    \_(ツ)_/

    Monday, June 17, 2019 1:26 PM
  • Found out that this is how we do it. 

    #Settings
    $Service = "xSERVICEx"
    $SrvNames = Get-Content -Path "C:\admin\Servername.txt"
    
    #Script
    $Result = @()
    
    $Result = foreach ($Server in $SrvNames)
    {
    Get-Service -Name $Service -ComputerName $Server | Stop-Service -PassThru | Set-Service -StartupType Disabled
    Get-Service -Name $Service -ComputerName $Server| Select-Object @{Label=”Machinename”;Expression={($Server)}}, name, Starttype, Status 
    }
    
    $Result | Out-GridView -PassThru | out-file C:\admin\test.txt

    • Marked as answer by veghotve Tuesday, June 18, 2019 6:37 AM
    Tuesday, June 18, 2019 6:36 AM
  • It would help if you took some time to learn PowerShell instead of just guessing.

    $Service = 'xSERVICEx'
    Get-Content C:\admin\Servername.txt |
        ForEach-Object{
            Get-Service -Name $Service -ComputerName $_ | 
                Stop-Service -PassThru | 
                Set-Service -StartupType Disabled -PassThru 
        } |
        Select-Object Machinename, name, Starttype, Status |
        Out-GridView

    Microsoft Virtual   Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Tuesday, June 18, 2019 9:56 AM