Last active
December 2, 2015 11:07
-
-
Save rasor/b5baaed8df575132e713 to your computer and use it in GitHub Desktop.
PowerShell script startup project with configfile and logfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rem | |
rem you must run this script as admin | |
c: | |
cd C:\scripts | |
powershell Set-ExecutionPolicy Unrestricted | |
powershell Get-ExecutionPolicy | |
powershell .\'SomeScript1.ps1' .\'SomeScript1.xml' | |
powershell Set-ExecutionPolicy Restricted | |
pause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# SomeScript1.ps1 | |
# V2015-11-24 | |
# Purpose: Scheleton for further content | |
# | |
# Args: | |
# - Config file # e.g. C:\scripts\SomeScript1.xml | |
#region args ----------------------- | |
param | |
( | |
[string]$configFile | |
) | |
[int]$global:someNumber = 0 | |
#endregion args ----------------------- | |
#region funcs ------------------------------------ | |
$logfilename = "SomeScript1.Log" | |
Remove-Item $logfilename -ErrorAction:SilentlyContinue | |
function Log-Msg(){ | |
param( | |
[string]$LogMsg | |
) | |
$LogMsg = (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " " + $LogMsg + "`n`r" | |
Write-Host $LogMsg | |
Add-Content $logfilename $LogMsg | |
} | |
function Exit-IfFalse(){ | |
param | |
( | |
[bool]$Check, | |
[string]$ErrorMsg | |
) | |
if(!$Check){ | |
Log-Msg $ErrorMsg | |
Read-Host $ErrorMsg". Press enter to exit" | |
break #Exit | |
} | |
}#endregion funcs ------------------------------------ | |
#region program ------------------------------------ | |
Log-Msg ("Started " + $logfilename) | |
Log-Msg ("Working Directory: " + ($PWD).ToString()) | |
#Load config file | |
Log-Msg ("Loading config file [" + $configFile + "]") | |
Exit-IfFalse ($configFile.length -gt 0) ("Missing config file") | |
[Bool]$okdir = Test-Path -Path:$configFile -pathType:Leaf | |
Exit-IfFalse $okdir ("Missing config file: " + $configFile) | |
#read from file | |
[xml]$cfgXml = Get-Content $configFile | |
[string]$envir = $cfgXml.root.envir | |
[bool]$someBoolData = [System.Convert]::ToBoolean($cfgXml.root.$envir.somebooldata) | |
[string]$someData = $cfgXml.root.$envir.somedata | |
[string]$strDate = $cfgXml.root.$envir.somedate #2008-03-25 00:00:00 | |
#Use config data | |
Log-Msg ("Envir: " + $envir) | |
if (-not [System.String]::IsNullOrEmpty($someData)) | |
{ | |
Log-Msg ("SomeData: " + $someData) | |
} | |
[int]$yyyy = $strDate.Substring(0,4) | |
[int]$mm = $strDate.Substring(5,2) | |
[int]$dd = $strDate.Substring(8,2) | |
[datetime]$someDate = New-Object System.DateTime -ArgumentList:$yyyy,$mm,$dd | |
#Do whatever else you like | |
#End program with pause and a message | |
Exit-IfFalse $false ("Ended " + $logfilename) | |
#endregion program ------------------------------------ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<root> | |
<envir>dev</envir> | |
<dev> | |
<somedata>ABC</somedata> | |
<somedate>2008-03-25 00:00:00</somedate> | |
<somebooldata>True</somebooldata> | |
<someotherdata>123</someotherdata> | |
</dev> | |
<prod> | |
<somedata>DEF</somedata> | |
<somedate>2015-01-01 00:00:00</somedate> | |
<somebooldata>False</somebooldata> | |
<someotherdata>456</someotherdata> | |
</prod> | |
</root> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment