none
Can't write output from get-service to my log function, can't use select-string RRS feed

  • General discussion

  • I want to run this in a script to get the status of the service:

    $asdf = get-service -displayname 'Certificate Propagation'

    $asdf looks like this:

    Status   Name               DisplayName                           
    ------   ----               -----------                           
    Stopped  CertPropSvc        Certificate Propagation  


    Below is my script

    <#
    To run LoggerFunction.ps1
    -Place LoggerFunction.ps1 into a directory of your choice that does not have a subdirectory named \LogFiles
    -Start Powershell, not PowerShellISE because sometimes it keeps a file open until ISE is closed.
    -In Powershell, make that directory your working directory.
    -Run the script
    -The output file will be found in <Your Directory of Choice>\LogFiles
    #>

    $StartTime = get-date
    $Year = $StartTime.year
    $Month = $StartTime.month
    $Day = $StartTime.day

    $LogFileDir = join-path -path (get-location) -childpath LogFiles
    New-Item -ItemType Directory -Path $LogFileDir -Force | out-null
    $LogFileName = $LogFileDir + '\Log_' + $Year + '-' + $Month + '-' + $Day + '.log'

    Try
        {
        Function Submit-LogEntry
       {
       Param(
         $message
        )

            #Modify the standard u formatted date, for efficiency.  
            #$TimeStamp contains the timestamp written to at the beginninf of each line inside the log file.
            $TimeStamp = get-date -f u
            $TimeStamp = $TimeStamp -replace 'Z',''
            $TimeStamp = $TimeStamp -replace ' ','_'
            
            '{0}  {1}' -f $TimeStamp,$message | Out-File -filepath $LogFileName -Append
                
       } #Function Submit-Log Entry
        
        write-host (' ')
        write-host('Please allow the script to run until you see SCRIPT DONE on the screen.')
        
        Submit-LogEntry('First entry written to the log file.')

        #Start-Sleep 2
        Submit-LogEntry('This entry should be 2 seconds after the first entry.')
        
        #Start-Sleep 3
        Submit-LogEntry('This entry should be 3 seconds after the second entry.')
        
        write-host (' ')
        write-host ('SCRIPT DONE')


                #Retrieve service status
    $asdf = get-service -displayname 'Certificate Propagation'
                #(Get-Content $asdf -Raw).Replace("`r`n","`n") | Set-Content $asdf -Force
                
                #If ($asdf -contains 'Stopped' -AND $asdf -contains 'CertPropSvc')
                    
                    Submit-LogEntry ($asdf)
                    
                
    The last line above sends $asdf to my Submit-LogEntry but the only log written to the log file is "CertPropSvc" or nothing.  The other entries above are faithfully written to the log file.

    ----------------------------------------------------------------------------------

    Since that wouldn't work (and I'll bet it could be accomplished) I tried to use Select-String to pull the strings from the output, but no success.

    select-string -quiet([switch]$Output) -simplematch -pattern 'Stopped' -InputObject $asdf

    -quiet([switch]$Output) is supposed to get the cmdlet to return true or false, but I am pretty sure I don't handle the switch correctly.

    -InputObject $asdf is to allow me to input a variable as opposed to a file.  I want to avoid writing $asdf to the hard drive if I can.

    I am not able to parse the contents of $asdf that could then be passed onto the logfile.

    OS: Win 7 Pro 64, PowershellISE, ISESteroids
    Any help would be greatly appreciated.
    Monday, February 22, 2016 11:03 PM

All replies

  • Start simple and work you way up to more complicated things.  This works as expected.

    Function Submit-LogEntry {
    	Param (
    		$message
    	)
    	$msg = '[{0}] {1}' -f [datetime]::Now.ToUniversalTime(), $message
    	write-host $msg -fore Green
    	$msg | Out-File mylog.log -Append
    	
    }
    
    Submit-LogEntry('First entry written to the log file.')
    #Start-Sleep 2
    Submit-LogEntry('This entry should be 2 seconds after the first entry.')
    #Start-Sleep 3
    Submit-LogEntry('This entry should be 3 seconds after the second entry.')
    
    


    \_(ツ)_/

    Tuesday, February 23, 2016 12:26 AM
  • Yes, the function and calls to it work as expected and have worked for several days.  I am needing to move beyond that now.  For instance, I need to get the output from

    get-service -displayname 'Certificate Propagation'

    into my function and into a log file.  Is this possible?  It seems almost certain to me that it would be possible.

    The last line above sends $asdf to my Submit-LogEntry but the only log written to the log file is "CertPropSvc" or nothing.  The other entries above are faithfully written to the log file.

    These are the next steps I need to overcome to proceed with this script, those above and below.  Help would be appreciated.

    Since that wouldn't work (and I'll bet it could be accomplished) I tried to use Select-String to pull the strings from the output, but no success.

    select-string -quiet([switch]$Output) -simplematch -pattern 'Stopped' -InputObject $asdf

    -quiet([switch]$Output) is supposed to get the cmdlet to return true or false, but I am pretty sure I don't handle the switch correctly.

    -InputObject $asdf is to allow me to input a variable as opposed to a file.  I want to avoid writing $asdf to the hard drive if I can.

    I am not able to parse the contents of $asdf that could then be passed onto the logfile.

    OS: Win 7 Pro 64, PowershellISE, ISESteroids
    Any help would be greatly appreciated.

    Tuesday, February 23, 2016 3:42 AM
  • I can no longer help anyone in this forum because the code her does not work. We repeatedly have posts that fail because of bugs in the software. I and others have repeatedly asked for this to be fixed.  Microsoft has made it clear that they want to remove this and otter forums.  This one is about completely abandoned.  Thank you Microsoft.

    I highly recommend that you and others find a new forum.  This one is slowly becoming unusable.  Too bad.


    \_(ツ)_/

    Tuesday, February 23, 2016 4:02 AM
  • Really!  This forum, and in particular you, have been very helpful to me.

    Thank you for your good replies.

    BigRedWeather

    Tuesday, February 23, 2016 12:02 PM
  • You are welcome.  I wasn't sure that you would understand what I was trying to demonstrate.  It is not the whole answer but it should point you in a useful direction.

    \_(ツ)_/

    Tuesday, February 23, 2016 12:07 PM