search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Using StringBuilder in PowerShell for better performance and faster processing
Posted in Windows Powershell | No Comment | 1,238 views | 04/09/2015 17:15

This is an example for StringBuilder in PowerShell. This is fast :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# IIS: Application Pools
 
# PowerShell String Builder
$HostIISAppPoolsStringBuilder = New-Object System.Text.StringBuilder
 
# Append to String Builder
$Null = $HostIISAppPoolsStringBuilder.Append("[")
 
# Get IIS Application Pools
$IISServerManager.ApplicationPools | % {
 
	# Clear Variables
	$HostIISAppPoolName = $Null;
	$HostIISAppPoolStartMode = $Null;
	$HostIISAppPoolEnable32BitAppOnWin64 = $Null;
	$HostIISAppPoolManagedPipelineMode = $Null;
	$HostIISAppPoolManagedRuntimeVersion = $Null;
	$HostIISAppPoolQueueLength = $Null;
	$HostIISAppPoolAutoStart = $Null;
	$HostIISAppPoolState = $Null;
	$HostIISAppPoolCpuAction = $Null;
	$HostIISAppPoolCpuLimit = $Null;
	$HostIISAppPoolCpuResetInterval = $Null;
	$HostIISAppPoolRapidFailProtection = $Null;
	$HostIISAppPoolRapidFailProtectionInterval = $Null;
	$HostIISAppPoolRapidFailProtectionMaxCrashes = $Null;
	$HostIISAppPoolIdentityType = $Null;
	$HostIISAppPoolIdleTimeout = $Null;
	$HostIISAppPoolIdleTimeoutAction = $Null;
	$HostIISAppPoolShutdownTimeLimit = $Null;
	$HostIISAppPoolStartupTimeLimit = $Null;
	$HostIISAppPoolMaxProcesses = $Null;
	$HostIISAppPoolPingingEnabled = $Null;
	$HostIISAppPoolPingInterval = $Null;
	$HostIISAppPoolPingResponseTime = $Null;
	$HostIISAppPoolLogEventOnProcessModel = $Null;
	$HostIISAppPoolLogEventOnRecycle = $Null;
	$HostIISAppPoolRecycleMemory = $Null;
	$HostIISAppPoolRecyclePrivateMemory = $Null;
	$HostIISAppPoolRecycleRequests = $Null;
	$HostIISAppPoolRecycleTime = $Null;
 
	# Get IIS AppPool Properties
	$HostIISAppPoolName = $_.Name;
	$HostIISAppPoolStartMode = $_.StartMode;
	$HostIISAppPoolEnable32BitAppOnWin64 = $_.Enable32BitAppOnWin64;
	$HostIISAppPoolManagedPipelineMode = $_.ManagedPipelineMode;
	$HostIISAppPoolManagedRuntimeVersion = $_.ManagedRuntimeVersion;
	$HostIISAppPoolQueueLength = $_.QueueLength;
	$HostIISAppPoolAutoStart = $_.AutoStart;
	$HostIISAppPoolState = $_.State;
	$HostIISAppPoolCpuAction = $_.Cpu.Action;
	$HostIISAppPoolCpuLimit = $_.Cpu.Limit;
	$HostIISAppPoolCpuResetInterval = $_.Cpu.ResetInterval.ToString();
	$HostIISAppPoolRapidFailProtection = $_.Failure.RapidFailProtection;
	$HostIISAppPoolRapidFailProtectionInterval = $_.Failure.RapidFailProtectionInterval.ToString();
	$HostIISAppPoolRapidFailProtectionMaxCrashes = $_.Failure.RapidFailProtectionMaxCrashes;
	$HostIISAppPoolIdentityType = $_.ProcessModel.IdentityType;
	$HostIISAppPoolIdleTimeout = $_.ProcessModel.IdleTimeout.ToString();
	$HostIISAppPoolIdleTimeoutAction = $_.ProcessModel.IdleTimeoutAction;
	$HostIISAppPoolShutdownTimeLimit = $_.ProcessModel.ShutdownTimeLimit.ToString();
	$HostIISAppPoolStartupTimeLimit = $_.ProcessModel.StartupTimeLimit.ToString();
	$HostIISAppPoolMaxProcesses = $_.ProcessModel.MaxProcesses;
	$HostIISAppPoolPingingEnabled = $_.ProcessModel.PingingEnabled;
	$HostIISAppPoolPingInterval = $_.ProcessModel.PingInterval.ToString();
	$HostIISAppPoolPingResponseTime = $_.ProcessModel.PingResponseTime.ToString();
	$HostIISAppPoolLogEventOnProcessModel = $_.ProcessModel.LogEventOnProcessModel;
	$HostIISAppPoolLogEventOnRecycle = $_.Recycling.LogEventOnRecycle;
	$HostIISAppPoolRecycleMemory = $_.Recycling.PeriodicRestart.Memory;
	$HostIISAppPoolRecyclePrivateMemory = $_.Recycling.PeriodicRestart.PrivateMemory;
	$HostIISAppPoolRecycleRequests = $_.Recycling.PeriodicRestart.Requests;
	$HostIISAppPoolRecycleTime = $_.Recycling.PeriodicRestart.Time.ToString();
 
	# Parameter Validation
	if($HostIISAppPoolName -eq $Null) { $HostIISAppPoolName = "Unknown"; }
	if($HostIISAppPoolStartMode -eq $Null) { $HostIISAppPoolStartMode = "Unknown"; }
	if($HostIISAppPoolEnable32BitAppOnWin64 -eq $Null) { $HostIISAppPoolEnable32BitAppOnWin64 = "Unknown"; }
	if($HostIISAppPoolManagedPipelineMode -eq $Null) { $HostIISAppPoolManagedPipelineMode = "Unknown"; }
	if($HostIISAppPoolManagedRuntimeVersion -eq $Null) { $HostIISAppPoolManagedRuntimeVersion = "Unknown"; }
	if($HostIISAppPoolQueueLength -eq $Null) { $HostIISAppPoolQueueLength = "-1"; }
	if($HostIISAppPoolAutoStart -eq $Null) { $HostIISAppPoolAutoStart = "Unknown"; }
	if($HostIISAppPoolState -eq $Null) { $HostIISAppPoolState = "Unknown"; }
	if($HostIISAppPoolCpuAction -eq $Null) { $HostIISAppPoolCpuAction = "Unknown"; }
	if($HostIISAppPoolCpuLimit -eq $Null) { $HostIISAppPoolCpuLimit = "Unknown"; }
	if($HostIISAppPoolCpuResetInterval -eq $Null) { $HostIISAppPoolCpuResetInterval = "Unknown"; }
	if($HostIISAppPoolRapidFailProtection -eq $Null) { $HostIISAppPoolRapidFailProtection = "Unknown"; }
	if($HostIISAppPoolRapidFailProtectionInterval -eq $Null) { $HostIISAppPoolRapidFailProtectionInterval = "Unknown"; }
	if($HostIISAppPoolRapidFailProtectionMaxCrashes -eq $Null) { $HostIISAppPoolRapidFailProtectionMaxCrashes = "-1"; }
	if($HostIISAppPoolIdentityType -eq $Null) { $HostIISAppPoolIdentityType = "Unknown"; }
	if($HostIISAppPoolIdleTimeout -eq $Null) { $HostIISAppPoolIdleTimeout = "Unknown"; }
	if($HostIISAppPoolIdleTimeoutAction -eq $Null) { $HostIISAppPoolIdleTimeoutAction = "Unknown"; }
	if($HostIISAppPoolShutdownTimeLimit -eq $Null) { $HostIISAppPoolShutdownTimeLimit = "Unknown"; }
	if($HostIISAppPoolStartupTimeLimit -eq $Null) { $HostIISAppPoolStartupTimeLimit = "Unknown"; }
	if($HostIISAppPoolMaxProcesses -eq $Null) { $HostIISAppPoolMaxProcesses = "-1"; }
	if($HostIISAppPoolPingingEnabled -eq $Null) { $HostIISAppPoolPingingEnabled = "Unknown"; }
	if($HostIISAppPoolPingInterval -eq $Null) { $HostIISAppPoolPingInterval = "Unknown"; }
	if($HostIISAppPoolPingResponseTime -eq $Null) { $HostIISAppPoolPingResponseTime = "Unknown"; }
	if($HostIISAppPoolLogEventOnProcessModel -eq $Null) { $HostIISAppPoolLogEventOnProcessModel = "Unknown"; }
	if($HostIISAppPoolLogEventOnRecycle -eq $Null) { $HostIISAppPoolLogEventOnRecycle = "Unknown"; }
	if($HostIISAppPoolRecycleMemory -eq $Null) { $HostIISAppPoolRecycleMemory = "-1"; }
	if($HostIISAppPoolRecyclePrivateMemory -eq $Null) { $HostIISAppPoolRecyclePrivateMemory = "-1"; }
	if($HostIISAppPoolRecycleRequests -eq $Null) { $HostIISAppPoolRecycleRequests = "-1"; }
	if($HostIISAppPoolRecycleTime -eq $Null) { $HostIISAppPoolRecycleTime = "Unknown"; }
 
	# Create Host IIS App Pool Json Data
	$HostIISAppPoolJson = @{
		ReportDate = "$VMReportDate"
		HostID = "$VirtualMetricHostID"
		HostName = "$VirtualMetricHostName"
		FQDN = "$VirtualMetricHostFQDN"
		Name = "$HostIISAppPoolName"
		StartMode = "$HostIISAppPoolStartMode"
		Enable32BitAppOnWin64 = "$HostIISAppPoolEnable32BitAppOnWin64"
		ManagedPipelineMode = "$HostIISAppPoolManagedPipelineMode"
		ManagedRuntimeVersion = "$HostIISAppPoolManagedRuntimeVersion"
		QueueLength = "$HostIISAppPoolQueueLength"
		AutoStart = "$HostIISAppPoolAutoStart"
		State = "$HostIISAppPoolState"
		CpuAction = "$HostIISAppPoolCpuAction"
		CpuLimit = "$HostIISAppPoolCpuLimit"
		CpuResetInterval = "$HostIISAppPoolCpuResetInterval"
		RapidFailProtection = "$HostIISAppPoolRapidFailProtection"
		RapidFailProtectionInterval = "$HostIISAppPoolRapidFailProtectionInterval"
		RapidFailProtectionMaxCrashes = "$HostIISAppPoolRapidFailProtectionMaxCrashes"
		IdentityType = "$HostIISAppPoolIdentityType"
		IdleTimeout = "$HostIISAppPoolIdleTimeout"
		IdleTimeoutAction = "$HostIISAppPoolIdleTimeoutAction"
		ShutdownTimeLimit = "$HostIISAppPoolShutdownTimeLimit"
		StartupTimeLimit = "$HostIISAppPoolStartupTimeLimit"
		MaxProcesses = "$HostIISAppPoolMaxProcesses"
		PingingEnabled = "$HostIISAppPoolPingingEnabled"
		PingInterval = "$HostIISAppPoolPingInterval"
		PingResponseTime = "$HostIISAppPoolPingResponseTime"
		LogEventOnProcessModel = "$HostIISAppPoolLogEventOnProcessModel"
		LogEventOnRecycle = "$HostIISAppPoolLogEventOnRecycle"
		RecycleMemory = "$HostIISAppPoolRecycleMemory"
		RecyclePrivateMemory = "$HostIISAppPoolRecyclePrivateMemory"
		RecycleRequests = "$HostIISAppPoolRecycleRequests"
		RecycleTime = "$HostIISAppPoolRecycleTime"
	}| ConvertTo-Json -Compress
 
	# Append to String Builder
	$Null = $HostIISAppPoolsStringBuilder.Append("$HostIISAppPoolJson`r`n")
 
	# Append to String Builder
	$Null = $HostIISAppPoolsStringBuilder.Append(",")
}
 
# Append to String Builder
$Null = $HostIISAppPoolsStringBuilder.Append("{}]")
 
# Get Output String
$HostIISAppPoolsOutputString = $HostIISAppPoolsStringBuilder.ToString()

You will get all string output using with .ToString() for StringBuilder.



Leave a Reply