Categories
Sponsors
Archive
Blogroll
Badges
Community
|
Posted in Windows Powershell, Windows Server | No Comment | 2,211 views | 28/02/2014 11:57
If you want to use FCAdapter WMI Class to see Physical and Virtual Port WWPNs, this function will make WWPNs readable.
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
| function ConvertTo-WWPN
{
<#
.SYNOPSIS
Converts WWPN Integer Array to WWPN
.EXAMPLE
ConvertTo-WWPN -WWPNArray (Get-WmiObject -Namespace "root\wmi" -Class MSFC_FCAdapterHBAAttributes).NodeWWN
.NOTES
Author: Yusuf Ozturk
Website: http://www.yusufozturk.info
Email: ysfozy[at]gmail.com
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(
Mandatory = $true,
HelpMessage = 'WWPN Array')]
$WWPNArray,
[Parameter(
Mandatory = $false,
HelpMessage = 'Debug Mode')]
[switch]$DebugMode = $false
)
# Enable Debug Mode
if ($DebugMode)
{
$DebugPreference = "Continue"
}
else
{
$ErrorActionPreference = "silentlycontinue"
}
# Clear Values
$WWPN = $Null;
$WWPNInt = $Null;
$WWPNIntArray = $Null;
$WWPNObjArray = $Null;
$CheckWWPNArray = $Null;
# Check WWPN Array
$CheckWWPNArray = $WWPNArray -as [int[]]
if ($CheckWWPNArray)
{
# Get Length
$Length = $WWPNArray.Length;
# Create WWPN Object Array
for ($WWPNInt = 0; $WWPNInt -lt $Length; $WWPNInt++)
{
[int[]]$WWPNIntArray = $WWPNIntArray + [int] $WWPNArray[$WWPNInt];
[Object[]]$WWPNObjArray = $WWPNObjArray + "{0:X}" -f $WWPNIntArray[$WWPNInt];
}
# Create WWPN Address
for($WWPNInt = 0; $WWPNInt -lt $Length; $WWPNInt++)
{
if ($WWPNObjArray[$WWPNInt].Length -eq 1)
{
$WWPN = $WWPN + "0" + $WWPNObjArray[$WWPNInt];
}
else
{
$WWPN = $WWPN + $WWPNObjArray[$WWPNInt];
}
if ($WWPNInt -lt $Length - 1)
{
$WWPN = $WWPN + ":"
}
if ($WWPN.Length -eq "23")
{
# Output WWPN
[String]$WWPN;
}
elseif ($WWPN.Length -eq "24")
{
# Fix WWPN Value
$WWPN = $WWPN.Substring(0,23)
# Output WWPN
[String]$WWPN;
# Clear WWPN
$WWPN = $Null;
}
}
}
} |
function ConvertTo-WWPN
{
<#
.SYNOPSIS
Converts WWPN Integer Array to WWPN
.EXAMPLE
ConvertTo-WWPN -WWPNArray (Get-WmiObject -Namespace "root\wmi" -Class MSFC_FCAdapterHBAAttributes).NodeWWN
.NOTES
Author: Yusuf Ozturk
Website: http://www.yusufozturk.info
Email: ysfozy[at]gmail.com
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(
Mandatory = $true,
HelpMessage = 'WWPN Array')]
$WWPNArray,
[Parameter(
Mandatory = $false,
HelpMessage = 'Debug Mode')]
[switch]$DebugMode = $false
)
# Enable Debug Mode
if ($DebugMode)
{
$DebugPreference = "Continue"
}
else
{
$ErrorActionPreference = "silentlycontinue"
}
# Clear Values
$WWPN = $Null;
$WWPNInt = $Null;
$WWPNIntArray = $Null;
$WWPNObjArray = $Null;
$CheckWWPNArray = $Null;
# Check WWPN Array
$CheckWWPNArray = $WWPNArray -as [int[]]
if ($CheckWWPNArray)
{
# Get Length
$Length = $WWPNArray.Length;
# Create WWPN Object Array
for ($WWPNInt = 0; $WWPNInt -lt $Length; $WWPNInt++)
{
[int[]]$WWPNIntArray = $WWPNIntArray + [int] $WWPNArray[$WWPNInt];
[Object[]]$WWPNObjArray = $WWPNObjArray + "{0:X}" -f $WWPNIntArray[$WWPNInt];
}
# Create WWPN Address
for($WWPNInt = 0; $WWPNInt -lt $Length; $WWPNInt++)
{
if ($WWPNObjArray[$WWPNInt].Length -eq 1)
{
$WWPN = $WWPN + "0" + $WWPNObjArray[$WWPNInt];
}
else
{
$WWPN = $WWPN + $WWPNObjArray[$WWPNInt];
}
if ($WWPNInt -lt $Length - 1)
{
$WWPN = $WWPN + ":"
}
if ($WWPN.Length -eq "23")
{
# Output WWPN
[String]$WWPN;
}
elseif ($WWPN.Length -eq "24")
{
# Fix WWPN Value
$WWPN = $WWPN.Substring(0,23)
# Output WWPN
[String]$WWPN;
# Clear WWPN
$WWPN = $Null;
}
}
}
}
You can test this function with following script:
ConvertTo-WWPN -WWPNArray (Get-WmiObject -Namespace "root\wmi" -Class MSFC_FCAdapterHBAAttributes).NodeWWN |
ConvertTo-WWPN -WWPNArray (Get-WmiObject -Namespace "root\wmi" -Class MSFC_FCAdapterHBAAttributes).NodeWWN
As a result, you will see WWPNs of FC Adapters.
Posted in Windows Powershell, Windows Server | 1 Comment | 7,374 views | 23/02/2014 21:33
We can use WMI to get CPU temperature via PowerShell:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| # Target Server
$Server = "localhost"
# Get Thermal Information
$ThermalInformation = Get-WmiObject -ComputerName $Server -Namespace "root\wmi" -Class "MSAcpi_ThermalZoneTemperature"
# Calculate CPU Temperature
if ($ThermalInformation)
{
$HostCPUTemperature = [math]::round((($ThermalInformation.CurrentTemperature - 2732) / 10), 0)
}
else
{
$HostCPUTemperature = "Unknown"
}
Write-Host CPU Temp: $HostCPUTemperature °C |
# Target Server
$Server = "localhost"
# Get Thermal Information
$ThermalInformation = Get-WmiObject -ComputerName $Server -Namespace "root\wmi" -Class "MSAcpi_ThermalZoneTemperature"
# Calculate CPU Temperature
if ($ThermalInformation)
{
$HostCPUTemperature = [math]::round((($ThermalInformation.CurrentTemperature - 2732) / 10), 0)
}
else
{
$HostCPUTemperature = "Unknown"
}
Write-Host CPU Temp: $HostCPUTemperature °C
In my case, CPU Temp is always 8 °C, I don’t know, Why :)
Posted in Windows Powershell | No Comment | 2,758 views | 21/02/2014 22:35
If you have special characters in your VM names, you may need to modify your XML files to fix reporting page.
Some characters (like &) may be special in XML, so you may need to change them to fix reporting page.
This is a simple script to search for “&” character in all XML files and replace them via “&”.
Thanks to Bill Stewart for Replace-FileString cmdlet. I made it a function to use in my case:
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
| function Replace-FileString
{
# Replace-FileString.ps1
# Written by Bill Stewart (bstewart@iname.com)
#
# Replaces strings in files using a regular expression. Supports
# multi-line searching and replacing.
#requires -version 2
<#
.SYNOPSIS
Replaces strings in files using a regular expression.
.DESCRIPTION
Replaces strings in files using a regular expression. Supports
multi-line searching and replacing.
.PARAMETER Pattern
Specifies the regular expression pattern.
.PARAMETER Replacement
Specifies the regular expression replacement pattern.
.PARAMETER Path
Specifies the path to one or more files. Wildcards are permitted. Each
file is read entirely into memory to support multi-line searching and
replacing, so performance may be slow for large files.
.PARAMETER LiteralPath
Specifies the path to one or more files. The value of the this
parameter is used exactly as it is typed. No characters are interpreted
as wildcards. Each file is read entirely into memory to support
multi-line searching and replacing, so performance may be slow for
large files.
.PARAMETER CaseSensitive
Specifies case-sensitive matching. The default is to ignore case.
.PARAMETER Multiline
Changes the meaning of ^ and $ so they match at the beginning and end,
respectively, of any line, and not just the beginning and end of the
entire file. The default is that ^ and $, respectively, match the
beginning and end of the entire file.
.PARAMETER UnixText
Causes $ to match only linefeed (\n) characters. By default, $ matches
carriage return+linefeed (\r\n). (Windows-based text files usually use
\r\n as line terminators, while Unix-based text files usually use only
\n.)
.PARAMETER Overwrite
Overwrites a file by creating a temporary file containing all
replacements and then replacing the original file with the temporary
file. The default is to output but not overwrite.
.PARAMETER Force
Allows overwriting of read-only files. Note that this parameter cannot
override security restrictions.
.PARAMETER Encoding
Specifies the encoding for the file when -Overwrite is used. Possible
values are: ASCII, BigEndianUnicode, Unicode, UTF32, UTF7, or UTF8. The
default value is ASCII.
.INPUTS
System.IO.FileInfo.
.OUTPUTS
System.String without the -Overwrite parameter, or nothing with the
-Overwrite parameter.
.LINK
about_Regular_Expressions
.EXAMPLE
C:\>Replace-FileString.ps1 '(Ferb) and (Phineas)' '$2 and $1' Story.txt
This command replaces the string 'Ferb and Phineas' with the string
'Phineas and Ferb' in the file Story.txt and outputs the file. Note
that the pattern and replacement strings are enclosed in single quotes
to prevent variable expansion.
.EXAMPLE
C:\>Replace-FileString.ps1 'Perry' 'Agent P' Ferb.txt -Overwrite
This command replaces the string 'Perry' with the string 'Agent P' in
the file Ferb.txt and overwrites the file.
#>
[CmdletBinding(DefaultParameterSetName="Path",
SupportsShouldProcess=$TRUE)]
param(
[parameter(Mandatory=$TRUE,Position=0)]
[String] $Pattern,
[parameter(Mandatory=$TRUE,Position=1)]
[String] [AllowEmptyString()] $Replacement,
[parameter(Mandatory=$TRUE,ParameterSetName="Path",
Position=2,ValueFromPipeline=$TRUE)]
[String[]] $Path,
[parameter(Mandatory=$TRUE,ParameterSetName="LiteralPath",
Position=2)]
[String[]] $LiteralPath,
[Switch] $CaseSensitive,
[Switch] $Multiline,
[Switch] $UnixText,
[Switch] $Overwrite,
[Switch] $Force,
[String] $Encoding="ASCII"
)
begin {
# Throw an error if $Encoding is not valid.
$encodings = @("ASCII","BigEndianUnicode","Unicode","UTF32","UTF7",
"UTF8")
if ($encodings -notcontains $Encoding) {
throw "Encoding must be one of the following: $encodings"
}
# Extended test-path: Check the parameter set name to see if we
# should use -literalpath or not.
function test-pathEx($path) {
switch ($PSCmdlet.ParameterSetName) {
"Path" {
test-path $path
}
"LiteralPath" {
test-path -literalpath $path
}
}
}
# Extended get-childitem: Check the parameter set name to see if we
# should use -literalpath or not.
function get-childitemEx($path) {
switch ($PSCmdlet.ParameterSetName) {
"Path" {
get-childitem $path -force
}
"LiteralPath" {
get-childitem -literalpath $path -force
}
}
}
# Outputs the full name of a temporary file in the specified path.
function get-tempname($path) {
do {
$tempname = join-path $path ([IO.Path]::GetRandomFilename())
}
while (test-path $tempname)
$tempname
}
# Use '\r$' instead of '$' unless -UnixText specified because
# '$' alone matches '\n', not '\r\n'. Ignore '\$' (literal '$').
if (-not $UnixText) {
$Pattern = $Pattern -replace '(?<!\\)\$', '\r$'
}
# Build an array of Regex options and create the Regex object.
$opts = @()
if (-not $CaseSensitive) { $opts += "IgnoreCase" }
if ($MultiLine) { $opts += "Multiline" }
if ($opts.Length -eq 0) { $opts += "None" }
$regex = new-object Text.RegularExpressions.Regex $Pattern, $opts
}
process {
# The list of items to iterate depends on the parameter set name.
switch ($PSCmdlet.ParameterSetName) {
"Path" { $list = $Path }
"LiteralPath" { $list = $LiteralPath }
}
# Iterate the items in the list of paths. If an item does not exist,
# continue to the next item in the list.
foreach ($item in $list) {
if (-not (test-pathEx $item)) {
write-error "Unable to find '$item'."
continue
}
# Iterate each item in the path. If an item is not a file,
# skip all remaining items.
foreach ($file in get-childitemEx $item) {
if ($file -isnot [IO.FileInfo]) {
write-error "'$file' is not in the file system."
break
}
# Get a temporary file name in the file's directory and create
# it as a empty file. If set-content fails, continue to the next
# file. Better to fail before than after reading the file for
# performance reasons.
if ($Overwrite) {
$tempname = get-tempname $file.DirectoryName
set-content $tempname $NULL -confirm:$FALSE
if (-not $?) { continue }
write-verbose "Created file '$tempname'."
}
# Read all the text from the file into a single string. We have
# to do it this way to be able to search across line breaks.
try {
write-verbose "Reading '$file'."
$text = [IO.File]::ReadAllText($file.FullName)
write-verbose "Finished reading '$file'."
}
catch [Management.Automation.MethodInvocationException] {
write-error $ERROR[0]
continue
}
# If -Overwrite not specified, output the result of the Replace
# method and continue to the next file.
if (-not $Overwrite) {
$regex.Replace($text, $Replacement)
continue
}
# Do nothing further if we're in 'what if' mode.
if ($WHATIFPREFERENCE) { continue }
try {
write-verbose "Writing '$tempname'."
[IO.File]::WriteAllText("$tempname", $regex.Replace($text,
$Replacement), [Text.Encoding]::$Encoding)
write-verbose "Finished writing '$tempname'."
write-verbose "Copying '$tempname' to '$file'."
copy-item $tempname $file -force:$Force -erroraction Continue
if ($?) {
write-verbose "Finished copying '$tempname' to '$file'."
}
remove-item $tempname
if ($?) {
write-verbose "Removed file '$tempname'."
}
}
catch [Management.Automation.MethodInvocationException] {
write-error $ERROR[0]
}
} # foreach $file
} # foreach $item
} # process
end { }
}
$Files = Get-ChildItem -Path "C:\Program Files\VirtualMetric\webserver\http\xml\01" -Filter *.xml -Recurse
foreach ($File in $Files)
{
$Path = $File.FullName
$FileContent = Replace-FileString -Pattern "&" -Replacement "&" -Path $Path
$FileContent | Set-Content $Path
Write-Host $Path is OK!
} |
function Replace-FileString
{
# Replace-FileString.ps1
# Written by Bill Stewart (bstewart@iname.com)
#
# Replaces strings in files using a regular expression. Supports
# multi-line searching and replacing.
#requires -version 2
<#
.SYNOPSIS
Replaces strings in files using a regular expression.
.DESCRIPTION
Replaces strings in files using a regular expression. Supports
multi-line searching and replacing.
.PARAMETER Pattern
Specifies the regular expression pattern.
.PARAMETER Replacement
Specifies the regular expression replacement pattern.
.PARAMETER Path
Specifies the path to one or more files. Wildcards are permitted. Each
file is read entirely into memory to support multi-line searching and
replacing, so performance may be slow for large files.
.PARAMETER LiteralPath
Specifies the path to one or more files. The value of the this
parameter is used exactly as it is typed. No characters are interpreted
as wildcards. Each file is read entirely into memory to support
multi-line searching and replacing, so performance may be slow for
large files.
.PARAMETER CaseSensitive
Specifies case-sensitive matching. The default is to ignore case.
.PARAMETER Multiline
Changes the meaning of ^ and $ so they match at the beginning and end,
respectively, of any line, and not just the beginning and end of the
entire file. The default is that ^ and $, respectively, match the
beginning and end of the entire file.
.PARAMETER UnixText
Causes $ to match only linefeed (\n) characters. By default, $ matches
carriage return+linefeed (\r\n). (Windows-based text files usually use
\r\n as line terminators, while Unix-based text files usually use only
\n.)
.PARAMETER Overwrite
Overwrites a file by creating a temporary file containing all
replacements and then replacing the original file with the temporary
file. The default is to output but not overwrite.
.PARAMETER Force
Allows overwriting of read-only files. Note that this parameter cannot
override security restrictions.
.PARAMETER Encoding
Specifies the encoding for the file when -Overwrite is used. Possible
values are: ASCII, BigEndianUnicode, Unicode, UTF32, UTF7, or UTF8. The
default value is ASCII.
.INPUTS
System.IO.FileInfo.
.OUTPUTS
System.String without the -Overwrite parameter, or nothing with the
-Overwrite parameter.
.LINK
about_Regular_Expressions
.EXAMPLE
C:\>Replace-FileString.ps1 '(Ferb) and (Phineas)' '$2 and $1' Story.txt
This command replaces the string 'Ferb and Phineas' with the string
'Phineas and Ferb' in the file Story.txt and outputs the file. Note
that the pattern and replacement strings are enclosed in single quotes
to prevent variable expansion.
.EXAMPLE
C:\>Replace-FileString.ps1 'Perry' 'Agent P' Ferb.txt -Overwrite
This command replaces the string 'Perry' with the string 'Agent P' in
the file Ferb.txt and overwrites the file.
#>
[CmdletBinding(DefaultParameterSetName="Path",
SupportsShouldProcess=$TRUE)]
param(
[parameter(Mandatory=$TRUE,Position=0)]
[String] $Pattern,
[parameter(Mandatory=$TRUE,Position=1)]
[String] [AllowEmptyString()] $Replacement,
[parameter(Mandatory=$TRUE,ParameterSetName="Path",
Position=2,ValueFromPipeline=$TRUE)]
[String[]] $Path,
[parameter(Mandatory=$TRUE,ParameterSetName="LiteralPath",
Position=2)]
[String[]] $LiteralPath,
[Switch] $CaseSensitive,
[Switch] $Multiline,
[Switch] $UnixText,
[Switch] $Overwrite,
[Switch] $Force,
[String] $Encoding="ASCII"
)
begin {
# Throw an error if $Encoding is not valid.
$encodings = @("ASCII","BigEndianUnicode","Unicode","UTF32","UTF7",
"UTF8")
if ($encodings -notcontains $Encoding) {
throw "Encoding must be one of the following: $encodings"
}
# Extended test-path: Check the parameter set name to see if we
# should use -literalpath or not.
function test-pathEx($path) {
switch ($PSCmdlet.ParameterSetName) {
"Path" {
test-path $path
}
"LiteralPath" {
test-path -literalpath $path
}
}
}
# Extended get-childitem: Check the parameter set name to see if we
# should use -literalpath or not.
function get-childitemEx($path) {
switch ($PSCmdlet.ParameterSetName) {
"Path" {
get-childitem $path -force
}
"LiteralPath" {
get-childitem -literalpath $path -force
}
}
}
# Outputs the full name of a temporary file in the specified path.
function get-tempname($path) {
do {
$tempname = join-path $path ([IO.Path]::GetRandomFilename())
}
while (test-path $tempname)
$tempname
}
# Use '\r$' instead of '$' unless -UnixText specified because
# '$' alone matches '\n', not '\r\n'. Ignore '\$' (literal '$').
if (-not $UnixText) {
$Pattern = $Pattern -replace '(?<!\\)\$', '\r$'
}
# Build an array of Regex options and create the Regex object.
$opts = @()
if (-not $CaseSensitive) { $opts += "IgnoreCase" }
if ($MultiLine) { $opts += "Multiline" }
if ($opts.Length -eq 0) { $opts += "None" }
$regex = new-object Text.RegularExpressions.Regex $Pattern, $opts
}
process {
# The list of items to iterate depends on the parameter set name.
switch ($PSCmdlet.ParameterSetName) {
"Path" { $list = $Path }
"LiteralPath" { $list = $LiteralPath }
}
# Iterate the items in the list of paths. If an item does not exist,
# continue to the next item in the list.
foreach ($item in $list) {
if (-not (test-pathEx $item)) {
write-error "Unable to find '$item'."
continue
}
# Iterate each item in the path. If an item is not a file,
# skip all remaining items.
foreach ($file in get-childitemEx $item) {
if ($file -isnot [IO.FileInfo]) {
write-error "'$file' is not in the file system."
break
}
# Get a temporary file name in the file's directory and create
# it as a empty file. If set-content fails, continue to the next
# file. Better to fail before than after reading the file for
# performance reasons.
if ($Overwrite) {
$tempname = get-tempname $file.DirectoryName
set-content $tempname $NULL -confirm:$FALSE
if (-not $?) { continue }
write-verbose "Created file '$tempname'."
}
# Read all the text from the file into a single string. We have
# to do it this way to be able to search across line breaks.
try {
write-verbose "Reading '$file'."
$text = [IO.File]::ReadAllText($file.FullName)
write-verbose "Finished reading '$file'."
}
catch [Management.Automation.MethodInvocationException] {
write-error $ERROR[0]
continue
}
# If -Overwrite not specified, output the result of the Replace
# method and continue to the next file.
if (-not $Overwrite) {
$regex.Replace($text, $Replacement)
continue
}
# Do nothing further if we're in 'what if' mode.
if ($WHATIFPREFERENCE) { continue }
try {
write-verbose "Writing '$tempname'."
[IO.File]::WriteAllText("$tempname", $regex.Replace($text,
$Replacement), [Text.Encoding]::$Encoding)
write-verbose "Finished writing '$tempname'."
write-verbose "Copying '$tempname' to '$file'."
copy-item $tempname $file -force:$Force -erroraction Continue
if ($?) {
write-verbose "Finished copying '$tempname' to '$file'."
}
remove-item $tempname
if ($?) {
write-verbose "Removed file '$tempname'."
}
}
catch [Management.Automation.MethodInvocationException] {
write-error $ERROR[0]
}
} # foreach $file
} # foreach $item
} # process
end { }
}
$Files = Get-ChildItem -Path "C:\Program Files\VirtualMetric\webserver\http\xml\01" -Filter *.xml -Recurse
foreach ($File in $Files)
{
$Path = $File.FullName
$FileContent = Replace-FileString -Pattern "&" -Replacement "&" -Path $Path
$FileContent | Set-Content $Path
Write-Host $Path is OK!
}
Please backup your XML directory first to avoid data corruption.
Posted in Virtual Machine Manager, Windows Powershell | No Comment | 2,241 views | 21/02/2014 10:49
Following script could be used to find similar virtual machines in same Cluster or Hyper-V Host.
This is very useful to split virtual machines if they have same workloads.
For example if you have two virtual machines with same workload like:
IISServer01
IISServer02
This script will find them and list them with a useful output. You should use it on SCVMM 2012 R2 Console:
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
| function Get-SimilarVM
{
<#
.SYNOPSIS
Gets similar virtual machines in same cluster, host or cloud
.EXAMPLE
Get-SimilarVM
.EXAMPLE
Get-SimilarVM -ClusterName "VMHOSTCluster01"
.EXAMPLE
Get-SimilarVM -VMHostName "VMHOST01"
.NOTES
Author: Yusuf Ozturk
Website: http://www.yusufozturk.info
Email: ysfozy@gmail.com
Date created: 20-Feb-2014
Last modified: 21-Feb-2014
Version: 1.1
.LINK
http://www.yusufozturk.info
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(
Mandatory = $false,
HelpMessage = 'Cluster Name')]
[string]$ClusterName,
[Parameter(
Mandatory = $false,
HelpMessage = 'Hyper-V Host')]
[string]$VMHostName,
[Parameter(
Mandatory = $false,
HelpMessage = 'Debug Mode')]
[switch]$DebugMode = $false
)
# Enable Debug Mode
if ($DebugMode)
{
$DebugPreference = "Continue"
}
else
{
$ErrorActionPreference = "silentlycontinue"
}
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
if ($VMHostName)
{
# Get VMs
$VMs = Get-SCVMHost -ComputerName $VMHostName | Get-VM
# Clear VM Array
$VMArray.Clear();
foreach ($VM in $VMs)
{
# Get VM Subname
$VMName = $VM.Name
$VMSubName = $VMName.Substring(0,(($VMName.Length)-2))
# Find Similar VMs
$SimilarVMs = $VMs | Where Name -like "$VMSubName*"
# Similar VM Count
$SimilarVMCount = $SimilarVMs.Count
if ($SimilarVMCount -gt "1")
{
foreach ($SimilarVM in $SimilarVMs)
{
$SimilarVMName = $SimilarVM.Name
if ($VMArray.Contains("$SimilarVMName") -ne $True)
{
# Update VM Array
$UpdateVMArray = $VMArray.Add("$SimilarVMName")
# VM Total Size
$VMTotalSize = [math]::round(($SimilarVM.TotalSize/1GB), 0)
$Properties = New-Object Psobject
$Properties | Add-Member Noteproperty Name $SimilarVM.Name
$Properties | Add-Member Noteproperty VMHost $SimilarVM.VMHost.Name
$Properties | Add-Member Noteproperty Cluster $Cluster.Name
$Properties | Add-Member Noteproperty Cloud $SimilarVM.Cloud.Name
$Properties | Add-Member Noteproperty UserRole $SimilarVM.UserRole.Name
$Properties | Add-Member Noteproperty CPUCount $SimilarVM.CPUCount
$Properties | Add-Member Noteproperty Memory $SimilarVM.Memory
$Properties | Add-Member Noteproperty TotalSize $VMTotalSize
$Properties | Add-Member Noteproperty BootDiskResource $SimilarVM.DiskResources[0].Name
$Properties | Add-Member Noteproperty Status $SimilarVM.Status
$Properties | Add-Member Noteproperty HasVirtualFibreChannelAdapters $SimilarVM.HasVirtualFibreChannelAdapters
$Properties | Add-Member Noteproperty HasPassthroughDisk $SimilarVM.HasPassthroughDisk
$Properties | Add-Member Noteproperty HasSharedStorage $SimilarVM.HasSharedStorage
Write-Output $Properties
}
}
}
}
}
else
{
if ($ClusterName)
{
# Get Clusters
$Clusters = Get-SCVMHostCluster -Name "$ClusterName"
}
else
{
# Get Clusters
$Clusters = Get-SCVMHostCluster
}
foreach ($Cluster in $Clusters)
{
# Get VMs
$VMs = Get-SCVMHost -VMHostCluster $Cluster | Get-VM
# Clear VM Array
$VMArray.Clear();
foreach ($VM in $VMs)
{
# Get VM Subname
$VMName = $VM.Name
$VMSubName = $VMName.Substring(0,(($VMName.Length)-2))
# Find Similar VMs
$SimilarVMs = $VMs | Where Name -like "$VMSubName*"
# Similar VM Count
$SimilarVMCount = $SimilarVMs.Count
if ($SimilarVMCount -gt "1")
{
foreach ($SimilarVM in $SimilarVMs)
{
$SimilarVMName = $SimilarVM.Name
if ($VMArray.Contains("$SimilarVMName") -ne $True)
{
# Update VM Array
$UpdateVMArray = $VMArray.Add("$SimilarVMName")
# VM Total Size
$VMTotalSize = [math]::round(($SimilarVM.TotalSize/1GB), 0)
$Properties = New-Object Psobject
$Properties | Add-Member Noteproperty Name $SimilarVM.Name
$Properties | Add-Member Noteproperty VMHost $SimilarVM.VMHost.Name
$Properties | Add-Member Noteproperty Cluster $Cluster.Name
$Properties | Add-Member Noteproperty Cloud $SimilarVM.Cloud.Name
$Properties | Add-Member Noteproperty UserRole $SimilarVM.UserRole.Name
$Properties | Add-Member Noteproperty CPUCount $SimilarVM.CPUCount
$Properties | Add-Member Noteproperty Memory $SimilarVM.Memory
$Properties | Add-Member Noteproperty TotalSize $VMTotalSize
$Properties | Add-Member Noteproperty BootDiskResource $SimilarVM.DiskResources[0].Name
$Properties | Add-Member Noteproperty Status $SimilarVM.Status
$Properties | Add-Member Noteproperty HasVirtualFibreChannelAdapters $SimilarVM.HasVirtualFibreChannelAdapters
$Properties | Add-Member Noteproperty HasPassthroughDisk $SimilarVM.HasPassthroughDisk
$Properties | Add-Member Noteproperty HasSharedStorage $SimilarVM.HasSharedStorage
Write-Output $Properties
}
}
}
}
}
}
} |
function Get-SimilarVM
{
<#
.SYNOPSIS
Gets similar virtual machines in same cluster, host or cloud
.EXAMPLE
Get-SimilarVM
.EXAMPLE
Get-SimilarVM -ClusterName "VMHOSTCluster01"
.EXAMPLE
Get-SimilarVM -VMHostName "VMHOST01"
.NOTES
Author: Yusuf Ozturk
Website: http://www.yusufozturk.info
Email: ysfozy@gmail.com
Date created: 20-Feb-2014
Last modified: 21-Feb-2014
Version: 1.1
.LINK
http://www.yusufozturk.info
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(
Mandatory = $false,
HelpMessage = 'Cluster Name')]
[string]$ClusterName,
[Parameter(
Mandatory = $false,
HelpMessage = 'Hyper-V Host')]
[string]$VMHostName,
[Parameter(
Mandatory = $false,
HelpMessage = 'Debug Mode')]
[switch]$DebugMode = $false
)
# Enable Debug Mode
if ($DebugMode)
{
$DebugPreference = "Continue"
}
else
{
$ErrorActionPreference = "silentlycontinue"
}
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
if ($VMHostName)
{
# Get VMs
$VMs = Get-SCVMHost -ComputerName $VMHostName | Get-VM
# Clear VM Array
$VMArray.Clear();
foreach ($VM in $VMs)
{
# Get VM Subname
$VMName = $VM.Name
$VMSubName = $VMName.Substring(0,(($VMName.Length)-2))
# Find Similar VMs
$SimilarVMs = $VMs | Where Name -like "$VMSubName*"
# Similar VM Count
$SimilarVMCount = $SimilarVMs.Count
if ($SimilarVMCount -gt "1")
{
foreach ($SimilarVM in $SimilarVMs)
{
$SimilarVMName = $SimilarVM.Name
if ($VMArray.Contains("$SimilarVMName") -ne $True)
{
# Update VM Array
$UpdateVMArray = $VMArray.Add("$SimilarVMName")
# VM Total Size
$VMTotalSize = [math]::round(($SimilarVM.TotalSize/1GB), 0)
$Properties = New-Object Psobject
$Properties | Add-Member Noteproperty Name $SimilarVM.Name
$Properties | Add-Member Noteproperty VMHost $SimilarVM.VMHost.Name
$Properties | Add-Member Noteproperty Cluster $Cluster.Name
$Properties | Add-Member Noteproperty Cloud $SimilarVM.Cloud.Name
$Properties | Add-Member Noteproperty UserRole $SimilarVM.UserRole.Name
$Properties | Add-Member Noteproperty CPUCount $SimilarVM.CPUCount
$Properties | Add-Member Noteproperty Memory $SimilarVM.Memory
$Properties | Add-Member Noteproperty TotalSize $VMTotalSize
$Properties | Add-Member Noteproperty BootDiskResource $SimilarVM.DiskResources[0].Name
$Properties | Add-Member Noteproperty Status $SimilarVM.Status
$Properties | Add-Member Noteproperty HasVirtualFibreChannelAdapters $SimilarVM.HasVirtualFibreChannelAdapters
$Properties | Add-Member Noteproperty HasPassthroughDisk $SimilarVM.HasPassthroughDisk
$Properties | Add-Member Noteproperty HasSharedStorage $SimilarVM.HasSharedStorage
Write-Output $Properties
}
}
}
}
}
else
{
if ($ClusterName)
{
# Get Clusters
$Clusters = Get-SCVMHostCluster -Name "$ClusterName"
}
else
{
# Get Clusters
$Clusters = Get-SCVMHostCluster
}
foreach ($Cluster in $Clusters)
{
# Get VMs
$VMs = Get-SCVMHost -VMHostCluster $Cluster | Get-VM
# Clear VM Array
$VMArray.Clear();
foreach ($VM in $VMs)
{
# Get VM Subname
$VMName = $VM.Name
$VMSubName = $VMName.Substring(0,(($VMName.Length)-2))
# Find Similar VMs
$SimilarVMs = $VMs | Where Name -like "$VMSubName*"
# Similar VM Count
$SimilarVMCount = $SimilarVMs.Count
if ($SimilarVMCount -gt "1")
{
foreach ($SimilarVM in $SimilarVMs)
{
$SimilarVMName = $SimilarVM.Name
if ($VMArray.Contains("$SimilarVMName") -ne $True)
{
# Update VM Array
$UpdateVMArray = $VMArray.Add("$SimilarVMName")
# VM Total Size
$VMTotalSize = [math]::round(($SimilarVM.TotalSize/1GB), 0)
$Properties = New-Object Psobject
$Properties | Add-Member Noteproperty Name $SimilarVM.Name
$Properties | Add-Member Noteproperty VMHost $SimilarVM.VMHost.Name
$Properties | Add-Member Noteproperty Cluster $Cluster.Name
$Properties | Add-Member Noteproperty Cloud $SimilarVM.Cloud.Name
$Properties | Add-Member Noteproperty UserRole $SimilarVM.UserRole.Name
$Properties | Add-Member Noteproperty CPUCount $SimilarVM.CPUCount
$Properties | Add-Member Noteproperty Memory $SimilarVM.Memory
$Properties | Add-Member Noteproperty TotalSize $VMTotalSize
$Properties | Add-Member Noteproperty BootDiskResource $SimilarVM.DiskResources[0].Name
$Properties | Add-Member Noteproperty Status $SimilarVM.Status
$Properties | Add-Member Noteproperty HasVirtualFibreChannelAdapters $SimilarVM.HasVirtualFibreChannelAdapters
$Properties | Add-Member Noteproperty HasPassthroughDisk $SimilarVM.HasPassthroughDisk
$Properties | Add-Member Noteproperty HasSharedStorage $SimilarVM.HasSharedStorage
Write-Output $Properties
}
}
}
}
}
}
}
You can define Cluster and Hyper-V Host in function to minimize scope.
For Hyper-V Host Specific Search:
Get-SimilarVM -VMHostName "VMHOST01" |
Get-SimilarVM -VMHostName "VMHOST01"
For Cluster Specific Search:
Get-SimilarVM -ClusterName "VMHOSTCluster01" |
Get-SimilarVM -ClusterName "VMHOSTCluster01"
Searching in all clusters:
You can customize output to see what you need.
Posted in Virtual Machine Manager, Windows Powershell | No Comment | 5,117 views | 19/02/2014 18:21
You can use following script to get VMs with Passthrough Disks on SCVMM 2012 R2.
$VMs = Get-VM | Where HasPassthroughDisk |
$VMs = Get-VM | Where HasPassthroughDisk
Then you can get virtual machine hardware information:
$VMs | ft Name,Status,CpuCount,Memory,TotalSize |
$VMs | ft Name,Status,CpuCount,Memory,TotalSize
You will see pretty good output :)
Posted in Virtual Machine Manager, Windows Powershell | No Comment | 1,699 views | 19/02/2014 18:16
You can use following script to get VMs with vHBA on SCVMM 2012 R2.
$VMs = Get-VM | Where HasVirtualFibreChannelAdapters |
$VMs = Get-VM | Where HasVirtualFibreChannelAdapters
Then you can get virtual machine hardware information:
$VMs | ft Name,Status,CpuCount,Memory,TotalSize |
$VMs | ft Name,Status,CpuCount,Memory,TotalSize
You will see pretty good output :)
Posted in Windows Powershell, Windows Server | No Comment | 1,519 views | 19/02/2014 17:44
This is a simple Telnet Function for switch management purposes.
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
| # Telnet Function
Function Get-Telnet
{ param (
[Parameter(ValueFromPipeline=$true)]
[String[]]$Commands,
[string]$RemoteHost,
[string]$Port = "23",
[int]$WaitTime = 1000
)
# Attach to the remote device, setup streaming requirements
$Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Port)
if ($Socket)
{ $Stream = $Socket.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.AsciiEncoding
# Now start issuing the commands
foreach ($Command in $Commands)
{ $Writer.WriteLine($Command)
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
}
# All commands issued, but since the last command is usually going to be
# the longest let's wait a little longer for it to finish
Start-Sleep -Milliseconds ($WaitTime * 4)
$Result = ""
# Save all the results
while($Stream.DataAvailable)
{ $Read = $Stream.Read($Buffer, 0, 1024)
$Result += ($Encoding.GetString($Buffer, 0, $Read))
}
}
else
{ $Result = "Unable to connect to host: $($RemoteHost):$Port"
}
# Done
$Result
} |
# Telnet Function
Function Get-Telnet
{ param (
[Parameter(ValueFromPipeline=$true)]
[String[]]$Commands,
[string]$RemoteHost,
[string]$Port = "23",
[int]$WaitTime = 1000
)
# Attach to the remote device, setup streaming requirements
$Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Port)
if ($Socket)
{ $Stream = $Socket.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.AsciiEncoding
# Now start issuing the commands
foreach ($Command in $Commands)
{ $Writer.WriteLine($Command)
$Writer.Flush()
Start-Sleep -Milliseconds $WaitTime
}
# All commands issued, but since the last command is usually going to be
# the longest let's wait a little longer for it to finish
Start-Sleep -Milliseconds ($WaitTime * 4)
$Result = ""
# Save all the results
while($Stream.DataAvailable)
{ $Read = $Stream.Read($Buffer, 0, 1024)
$Result += ($Encoding.GetString($Buffer, 0, $Read))
}
}
else
{ $Result = "Unable to connect to host: $($RemoteHost):$Port"
}
# Done
$Result
}
I got the source code from this web page:
Modified a few parts to use in my environment.
|