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 \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.