Link to home
Start Free TrialLog in
Avatar of Suresh Kumar
Suresh Kumar

asked on

Powershell script does not exit

Powershell script does not exist when running through bladelogic.But it woks fine till the HTML part but doesnot exit

Please see the script below..
[cmdletbinding()]
param
(
  [Parameter(Position=0,ValuefromPipeline=$true)]
  [string[]]$ComputerName = $env:ComputerName 
)
$cpuload = ((Get-Counter -Counter "\\$ComputerName\Process(*)\% Processor Time" -ea 0).CounterSamples) | Select-Object -Property instancename, cookedvalue | Sort-Object -Property cookedvalue -Descending | Select-Object -First 20 |Select-Object InstanceName,@{L='CPU';E={($_.Cookedvalue/100).toString('P')}}

$a = "<style>"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"
$cpuload | ConvertTo-Html -head $a -body "<H2>CPU LOAD</H2>" | Out-File -FilePath C:\tmp\dis.txt

#Invoke-Expression C:\tmp\dis1.txt
#cat C:\tmp\dis1.txt

Get-Content 'C:\tmp\dis.txt' | Foreach-Object {$_ -replace '^<html.*$', ("<html>")} | Set-Content 'C:\tmp\dis1.txt'

cat C:\tmp\dis1.txt

Open in new window

Do i need to add anything .so that script exit
ASKER CERTIFIED SOLUTION
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you are wrong - the script executes and terminates, but your result file is empty. Correct? That is because you are reading from and writing to the same file in a single pipeline, which does not work.
Line 19 should be
(Get-Content 'C:\tmp\dis.txt') | Foreach-Object {$_ -replace '^<html.*$', ("<html>")} | Set-Content 'C:\tmp\dis1.txt'

Open in new window

(note the added parens) though I don't see  why you shouldn't process the output of ConvertTo-HTML directly without first writing it to a file you then overwrite.
Your code still is wrong. exit does not change that.