URLGenie.GenerateConfigFromList.POSH.WA (WriteActionModuleType)
Element properties: Member Modules:
Overrideable Parameters:
Source Code: <WriteActionModuleType ID="URLGenie.GenerateConfigFromList.POSH.WA" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="InputPath" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="FileMask" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="OutputPath" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="WriteToEventLog" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="IndexBeginAt" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="ErrorTime" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="WarningTime" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="Interval" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="GroupID" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="ResponseTimeout" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="StatusThreshold" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="0" name="Watchers" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="InputPath" Selector="$Config/InputPath$" ParameterType="string"/>
<OverrideableParameter ID="FileMask" Selector="$Config/FileMask$" ParameterType="string"/>
<OverrideableParameter ID="OutputPath" Selector="$Config/OutputPath$" ParameterType="string"/>
<OverrideableParameter ID="WriteToEventLog" Selector="$Config/WriteToEventLog$" ParameterType="bool"/>
<OverrideableParameter ID="IndexBeginAt" Selector="$Config/IndexBeginAt$" ParameterType="int"/>
<OverrideableParameter ID="ErrorTime" Selector="$Config/ErrorTime$" ParameterType="int"/>
<OverrideableParameter ID="WarningTime" Selector="$Config/WarningTime$" ParameterType="int"/>
<OverrideableParameter ID="Interval" Selector="$Config/Interval$" ParameterType="int"/>
<OverrideableParameter ID="GroupID" Selector="$Config/GroupID$" ParameterType="string"/>
<OverrideableParameter ID="ResponseTimeout" Selector="$Config/ResponseTimeout$" ParameterType="int"/>
<OverrideableParameter ID="StatusThreshold" Selector="$Config/StatusThreshold$" ParameterType="int"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="Watchers" Selector="$Config/Watchers$" ParameterType="string"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="POSH" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>GenerateConfigFromList.ps1</ScriptName>
<ScriptBody><Script> # GenerateConfigFromList.ps1
# This script will find all matching files in a given directory and generate URLGenie config files based on those found files.
# The source files should contain URL addresses, one per line. "http://" is not required.
# Author: Tyson Paul ( https://blogs.msdn.microsoft.com/tysonpaul/ )
# 2017.11.20 Check for improper/impossible URL length. Eliminates problems caused by blank lines, typically at end of input file(s).
# 2015.4.21 Fixed URL instance index numbering issue
# 2015.3.30 Added ability to specify beginning index number for instances.
# Added ability to generate empty config file by leaving the InputPath blank/empty. OutputPath is required.
# 2014.8.8 First version.
Param(
[int]$ErrorTime,
[string]$FileMask = "*.txt",
[string]$GroupID,
[int]$IndexBeginAt=1,
[int]$Interval,
[string]$InputPath,
[string]$OutputPath="C:\",
[int]$ResponseTimeout,
[int]$StatusThreshold,
[int]$WarningTime,
[string]$Watchers="MS",
[string]$WriteToEventLog='true'
)
[bool]$WriteToEventLog = [System.Convert]::ToBoolean($WriteToEventLog)
New-Variable -Name StartupVariables -Force -Value (Get-Variable -Scope Global | Select -ExpandProperty Name)
######## FOR MANUAL TESTING ########
# $InputPath="C:\Test"
# $OutputPath="C:\Test"
#
# $WriteToEventLog=0
# $Global:Index=100
# $Error.clear()
# cls
######## TEST ########
#######################################################
Function Cleanup(){
$ErrorActionPreference = "SilentlyContinue" #Depending on when this is called, some variables may not be initialized and clearing could throw benign error. Supress.
#Cleanup
Get-Variable | Where-Object { $StartupVariables -notcontains $_.Name } | % { Remove-Variable -Name "$($_.Name)" -Force -Scope 1 }
}
#######################################################
Function LogIt {
Param(
[int]$EventID,
[int]$Type=2,
[string]$Message="No message specified." ,
[int]$proceed=$false
)
$TimeStamp = (get-date -format "yyyyMMddHHmmssfff")
$output = @"
Message: $Message
ThisScript: GenerateConfigFromList.ps1
FileMask:$FileMask
InputPath:$InputPath
OutputPath:$OutputPath
Global:Index:$Global:Index
Interval:$Interval
ErrorTime:$ErrorTime
WarningTime:$WarningTime
StatusThreshold:$StatusThreshold
GroupID:$GroupID
ResponseTimeout:$ResponseTimeout
Watchers:$Watchers
Any errors will appear here:
$error
"@
If ($proceed) {
$oEvent = New-Object -comObject "MOM.ScriptAPI"
$oEvent.LogScriptEvent("$ThisScript",$EventID,$Type,$output)
}
}
#######################################################
Function Generate {
Param(
[string]$InputFile,
[string]$OutputFile
)
$A="<" + "![CDATA["
$Z="]]" +">"
If ($InputFile){
$Items = (Get-Content $InputFile)
}
Else{
$Items = "HTTP://YOUR_URL_HERE"
LogIt -EventID 9995 -Message "Input file does not exist." -Type $Warn -Proceed $true
}
ForEach ($Item in $Items) {
# $Items | ForEach {
If ($Item.Length -le 4) {
continue;
}
$TempBlock= @"
<request>
<url>$($A)$($Item)$($Z)</url>
<index>$($Global:Index)</index>
<watchers>$($Watchers)</watchers>
<verb>GET</verb>
<groupid>$GroupID</groupid>
<retrycount>0</retrycount>
<interval>$Interval</interval>
<statusthreshold>$StatusThreshold</statusthreshold>
<responsetimeout>$ResponseTimeout</responsetimeout>
<contentmatch>h</contentmatch>
<warningtime>$WarningTime</warningtime>
<errortime>$ErrorTime</errortime>
<authuser></authuser>
<headercontenttype>text/xml;charset=utf-8</headercontenttype>
<header1name>Accept-Language</header1name>
<header1value>en-US</header1value>
<header2name>Accept-Charset</header2name>
<header2value>utf-8</header2value>
<header3name>From</header3name>
<header3value>[email protected] </header3value>
<authenticationscheme>None</authenticationscheme>
<proxy></proxy>
<proxyauthenticationscheme></proxyauthenticationscheme>
<proxyuser></proxyuser>
<certthumbprint></certthumbprint>
<body>$($A)$($Z)</body>
<useragent>$($A)Mozilla/5.0 (compatible; MSIE 9.0; Windows NT; Windows NT 6.1; en-US)$($Z)</useragent>
<wiki>$($A)No link provided$($Z)</wiki>
<description>$($A)URL address to monitor.$($Z)</description>
</request>
"@
$Requestsblock+=$TempBlock
$Global:Index++
}
$HeaderBlock = @"
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<URLGenieconfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<requests>
"@
$FooterBlock = @"
</requests>
</URLGenieconfig>
"@
If (Test-Path -PathType Leaf -Path $OutputFile) {
Write-Host ""
Write-Host "Warning: Destination file already exists: ($OutputFile). "
Write-Host "Creating unique file name for new output file..."
# Create unique filename. NoClobber.
$FileName = "$($Global:OutputFileName)" + "." + (get-date -format "_yyyy.MM.dd_HHmmss_fff")
Start-Sleep -Milliseconds 50
$FileName = $FileName.Replace('.xml','')
$Global:OutputFileName = $FileName + ".xml"
$OutputFile = (Join-Path $OutputPath $Global:OutputFileName)
}
$HeaderBlock | Out-File -FilePath $OutputFile -Encoding utf8
$Requestsblock | Out-File -FilePath $OutputFile -Append -Encoding utf8
$FooterBlock | Out-File -FilePath $OutputFile -Append -Encoding utf8
} #End Function
#######################################################
# --------------------------BEGIN---------------------------------------------------
LogIt -EventID 9990 -Message "Begin Script..." -Type $warn -Proceed $WriteToEventLog
$Global:Index=$IndexBeginAt
$Requestsblock=""
[int]$info=0
[int]$warn=2
[int]$critical=1
try {
$Test1 = Test-Path $InputPath
} catch {
LogIt -EventID 9995 -Message "InputPath does not exist: [$($InputPath)]." -Type $critical -Proceed $true
}
try {
$Test2 = Test-Path $OutputPath
} catch {
LogIt -EventID 9999 -Message "OutputPath does not exist: [$($OutputPath)]." -Type $critical -Proceed $true
}
If ($Test2) {
$InputFiles = (Get-ChildItem -File -Path $InputPath -Filter $FileMask)
If ($InputFiles.Count -gt 0){
$i=1
ForEach ($InputFile in $InputFiles ){
$End = ($InputFile.Name.Length - 4)
$Global:OutputFileName = "Requests_" + $InputFile.Name.Substring(0,$End) + ".xml"
Generate -InputFile $InputFile.FullName -OutputFile (Join-Path $OutputPath $Global:OutputFileName)
Write-Host "Output File $($i): $(Join-Path $OutputPath $Global:OutputFileName) "
$i++
}
}
# If no input files exist, create generic file/template.
Else {
$Global:OutputFileName = "Requests_Template.xml.INACTIVE"
$Global:OutputFile = (Join-Path $OutputPath $Global:OutputFileName)
Generate -OutputFile $Global:OutputFile
$Message = "No input files found. Writing generic configuration template to: [$($Global:OutputFile)]. You will have to copy/paste the Request element to manually configure your URL instances."
LogIt -EventID 9995 -Message $Message -Type $Warn -Proceed $WriteToEventLog
Write-Host $Message
}
}
Cleanup
</Script> </ScriptBody>
<Parameters>
<Parameter>
<Name>ErrorTime</Name>
<Value>$Config/ErrorTime$</Value>
</Parameter>
<Parameter>
<Name>FileMask</Name>
<Value>$Config/FileMask$</Value>
</Parameter>
<Parameter>
<Name>GroupID</Name>
<Value>$Config/GroupID$</Value>
</Parameter>
<Parameter>
<Name>IndexBeginAt</Name>
<Value>$Config/IndexBeginAt$</Value>
</Parameter>
<Parameter>
<Name>Interval</Name>
<Value>$Config/Interval$</Value>
</Parameter>
<Parameter>
<Name>InputPath</Name>
<Value>$Config/InputPath$</Value>
</Parameter>
<Parameter>
<Name>OutputPath</Name>
<Value>$Config/OutputPath$</Value>
</Parameter>
<Parameter>
<Name>ResponseTimeout</Name>
<Value>$Config/ResponseTimeout$</Value>
</Parameter>
<Parameter>
<Name>StatusThreshold</Name>
<Value>$Config/StatusThreshold$</Value>
</Parameter>
<Parameter>
<Name>WarningTime</Name>
<Value>$Config/WarningTime$</Value>
</Parameter>
<Parameter>
<Name>Watchers</Name>
<Value>$Config/Watchers$</Value>
</Parameter>
<Parameter>
<Name>WriteToEventLog</Name>
<Value>$Config/WriteToEventLog$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="POSH"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>Windows!Microsoft.Windows.SerializedObjectData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>