URLGenie.GenerateConfigFromList.POSH.WA (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData
OutputTypeMicrosoft.Windows.SerializedObjectData

Member Modules:

ID Module Type TypeId RunAs 
POSH WriteAction Microsoft.Windows.PowerShellWriteAction Default

Overrideable Parameters:

IDParameterTypeSelector
Watchersstring$Config/Watchers$
TimeoutSecondsint$Config/TimeoutSeconds$
StatusThresholdint$Config/StatusThreshold$
ResponseTimeoutint$Config/ResponseTimeout$
GroupIDstring$Config/GroupID$
Intervalint$Config/Interval$
WarningTimeint$Config/WarningTime$
ErrorTimeint$Config/ErrorTime$
IndexBeginAtint$Config/IndexBeginAt$
WriteToEventLogbool$Config/WriteToEventLog$
OutputPathstring$Config/OutputPath$
FileMaskstring$Config/FileMask$
InputPathstring$Config/InputPath$

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="&lt;" + "![CDATA["
$Z="]]" +"&gt;"
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= @"
&lt;request&gt;
&lt;url&gt;$($A)$($Item)$($Z)&lt;/url&gt;
&lt;index&gt;$($Global:Index)&lt;/index&gt;
&lt;watchers&gt;$($Watchers)&lt;/watchers&gt;
&lt;verb&gt;GET&lt;/verb&gt;
&lt;groupid&gt;$GroupID&lt;/groupid&gt;
&lt;retrycount&gt;0&lt;/retrycount&gt;
&lt;interval&gt;$Interval&lt;/interval&gt;
&lt;statusthreshold&gt;$StatusThreshold&lt;/statusthreshold&gt;
&lt;responsetimeout&gt;$ResponseTimeout&lt;/responsetimeout&gt;
&lt;contentmatch&gt;h&lt;/contentmatch&gt;
&lt;warningtime&gt;$WarningTime&lt;/warningtime&gt;
&lt;errortime&gt;$ErrorTime&lt;/errortime&gt;
&lt;authuser&gt;&lt;/authuser&gt;
&lt;headercontenttype&gt;text/xml;charset=utf-8&lt;/headercontenttype&gt;
&lt;header1name&gt;Accept-Language&lt;/header1name&gt;
&lt;header1value&gt;en-US&lt;/header1value&gt;
&lt;header2name&gt;Accept-Charset&lt;/header2name&gt;
&lt;header2value&gt;utf-8&lt;/header2value&gt;
&lt;header3name&gt;From&lt;/header3name&gt;
&lt;header3value&gt;[email protected]&lt;/header3value&gt;
&lt;authenticationscheme&gt;None&lt;/authenticationscheme&gt;
&lt;proxy&gt;&lt;/proxy&gt;
&lt;proxyauthenticationscheme&gt;&lt;/proxyauthenticationscheme&gt;
&lt;proxyuser&gt;&lt;/proxyuser&gt;
&lt;certthumbprint&gt;&lt;/certthumbprint&gt;
&lt;body&gt;$($A)$($Z)&lt;/body&gt;
&lt;useragent&gt;$($A)Mozilla/5.0 (compatible; MSIE 9.0; Windows NT; Windows NT 6.1; en-US)$($Z)&lt;/useragent&gt;
&lt;wiki&gt;$($A)No link provided$($Z)&lt;/wiki&gt;
&lt;description&gt;$($A)URL address to monitor.$($Z)&lt;/description&gt;
&lt;/request&gt;


"@

$Requestsblock+=$TempBlock
$Global:Index++
}

$HeaderBlock = @"
&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;URLGenieconfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
&lt;requests&gt;

"@

$FooterBlock = @"
&lt;/requests&gt;
&lt;/URLGenieconfig&gt;

"@
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>