Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

PowerShell output saved to file location

$
0
0

Hi everyone.
Hoping someone could offer me some advice in regards to an issue I am stuck on.

My script checks the Tempdb status , I would like the results to a .csv and also display on screen if possible.
If possible could someone advise me on what I need to change in my script , how the options work etc.

Any comments on how the script is working , and how I can change it would be most appreciated as I am trying to learn PowerShell.

My current script:

Clear-Host
foreach ($svr in get-content "C:\PowerShell\Servers.txt")
{

function Test-SQLConnection{
param([parameter(mandatory=$true)][string[]] $Instances)

$return = @()
foreach($InstanceName in $Instances){
$row = New-Object –TypeName PSObject –Prop @{'InstanceName'=$InstanceName;'StartupTime'=$null}
try{
$check=Invoke-Sqlcmd -ServerInstance $InstanceName -Database TempDB -Query "SELECT @@SERVERNAME as Name,Create_Date FROM sys.databases WHERE name = 'TempDB'" -ErrorAction Stop -ConnectionTimeout 3
$row.InstanceName = $check.Name
$row.StartupTime = $check.Create_Date
}
catch{
#do nothing on the catch
}
finally{
$return += $row
}
}
return $return
}

Test-SQLConnection -Instances $svr

}


thank you in advance for any advice offered.


Viewing all articles
Browse latest Browse all 6937

Trending Articles