Skip to content

Instantly share code, notes, and snippets.

View tobyqin's full-sized avatar
☁️
Enter the cloud.

Toby Qin tobyqin

☁️
Enter the cloud.
View GitHub Profile
@tobyqin
tobyqin / Send-Email.ps1
Created November 12, 2015 07:24
PowerShell to send email
function Send-Email($to, $cc, $subject, $body)
{
$From = "you@domain.com"
$SmtpHost = "smtp.domain.com"
Send-MailMessage -From $From -Subject $subject -To $to -Cc $cc -Body $body -BodyAsHtml -SmtpServer $SmtpHost
}
$To = "user1@domain.com", "user2@domain.com"
$Cc="boss@domain.com"
$Subject = "Don't worry, it is a test."
@tobyqin
tobyqin / Trap-Catch.ps1
Created November 12, 2015 07:26
PowerShell trap to get error info
$ErrorActionPreference="Stop"
$DebugPreference="Continue"
trap
{
Write-Debug "=== Enter trap ==="
$funcName = (Get-PSCallStack)[1].Position.Text
$detail = $_ | Out-String
## Initialization
$script_info = Get-Item $MyInvocation.MyCommand.Definition
$script_dir =$script_info.Directory
. "$script_dir\GlobalFunctions.ps1"
$DebugPreference = "Continue"
$ErrorActionPreference = "Stop"
function Execute-command($commandPath)
{
Set-Location $env:TEMP
Log-Info "Command: $commandPath" $logFile
cmd /c "$commandPath"
}
## To read the config file with same name as script file in same folder
function Read-ConfigFile($scriptItem)
{
$configFile = $scriptItem.BaseName + ".xml"
$configFilePath = Join-Path $scriptItem.Directory $configFile
Log-Info "Config: $configFilePath"
Get-Content $configFilePath
}
function Get-LogDir($env)
{
$dir = $globalConf.LogDir
New-Item -Path $dir -ItemType "Directory" -Force | Out-Null
return $dir
}
function Init-LogFile($env, $stepName)
{
$logDir = Get-LogDir $env
function Start-TheService([string]$svc)
{
$STOP = "Stopped"
$RUN = "Running"
"Starting $svc..."
$s = get-service -name $svc
"Original status is : " + $s.Status
if($s.Status -eq $STOP)
:: Restore nuget packages
set nuget="%~dp0\Tools\nuget.exe"
pushd "%~dp0"
%nuget% restore MyApp.sln
popd
function Block-Processes ($pattern)
{
# $pattern=@("Spirit.*")
$proc = Get-Process -Name $pattern
foreach($p in $proc)
{
if($p -ne $null)
{
Stop-Process $p.Id -Force
}
#Add TimeStamp
$thedate = Get-Date -format "yyyy-MM-dd"
$logfile = "OpenWebPage" + "$thedate" + ".txt"
$thefiledate = Get-Date -format "yyyy-MM-dd HH:mm:ss"
Add-Content $logfile "`nTime Started(yyyyMMddHHmm) $thefiledate "
$urlsplit = "http://weburl:8629/path".Split(':')
$portsplit = ":8629/path"
$urlport = $urlsplit[0] + ":" + $urlsplit[1] + $portsplit
$fport = $urlport + "query?value=test"