简体   繁体   中英

PowerShell Add-Content with Get-Service

$Service = Get-Service -ComputerName SYNESIGNSVR -Name SynAutoImportService
$ServiceDName = $Service.DisplayName
$ServiceStat = $Service.Status

Add-Content "c:\temp\logtemp - Services Status.log" "$(Get-Date -Format 
"yyyy-MM-dd HH:mm:ss") - $ServiceDName service on server SYNESIGNSVR current 
status: $ServiceStat"

I am writing a PS script that will create a log file with several different services and their current status from a few different windows servers. The above code works and returns:

2017-04-19 12:01:17 - Synergy Document AutoImport service on server SYNESIGNSVR current status: Running

This seems like sloppy code, the way I've pulled the Get-Service and made the Status and DisplayName pull in.

I'm looking for ways I could make this code cleaner and or more efficient.

What about:

$Computer = 'SYNESIGNSVR'
$Service = Get-Service -ComputerName $Computer -Name SynAutoImportService

$LogLine = "{0} - {1} service on server {2} current status: {3}" -f [DateTime]::Now,
                                                                    $Service.DisplayName,
                                                                    $Computer,
                                                                    $Service.Status

Add-Content -Path "C:\temp\logtemp - Services Status.log" -Value $LogLine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM