Advanced Windows PowerShell Scripting Video Training

Tuesday, February 24, 2015

Today on PowerShell.com, I helped out a new user to PowerShell extract information from his remote clients. Two things that struck me.

1. He needed to use the legacy Get-WMIObject cmdlet. That means no PowerShell remoting, which is what I prefer.

2. He needed to be able to write the information to a CSV file. Normally there is nothing wrong with that except that one piece of information had the potential to return multiple MAC addresses. A CSV could not handle that on its own so I had to write up some simple code to address this data and put it in a form that Export-CSV could utilize.

First off, the code.

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

FunctionGet-ComputerInfo

{

[CmdletBinding()]

Param(

[parameter(Mandatory=$true,

ValueFromPipeline=$true)]

[String[]]

$ComputerName

)

BEGIN

{

# Creates a custome object that can be easily modified.

# Since you want a CSV file, this will create the maximum

# opportunities for reuse of the data. You can see how to

# add memory for more adapters.Also look in the

# SWITCH statement if you add more adapters.

FunctionNew-ComputerObject

{

$Obj=New-Object-TypeNamepsobject-Property@{

'ComputerName'=$computer

'Model'=$cs.model

'Serial'=$ser.serialnumber

'User'=$cs.username

'Online'=$False

'TotalNICAdapters'=0

'MAC0'=$Null

'MAC1'=$Null

'MAC2'=$Null

'MAC3'=$Null

'MAC4'=$Null

'MAC5'=$Null

'MAC6'=$Null

'MAC7'=$Null

'MAC8'=$Null

'MAC9'=$Null

'MAC10'=$Null

'MAC11'=$Null

'MAC12'=$Null

'MAC13'=$Null

'MAC14'=$Null

'MAC15'=$Null

}

# Send the Object to the calling statement.

Write-Output$Obj

}

}# END: BEGIN BLOCK

PROCESS

{

# Grab a copy of the custom object.

$CO=New-ComputerObject

ForEach($Cin$ComputerName)

{

# Place a fresh compy of the custom object in memory.

$Obj=$CO

# Assign the computer name.

$Obj.ComputerName=$c

Try

{

# If you cannot connect to the client, it will still record the attempt

To make the CSV requirement work, I had to add a property to the output object for each potential network adapter. You can see those properties as MAC1, MAC2, etc… Also take a look at the Switch statement starting on line 89. I am sure that I could have found a unique way to code that one better. Hey, it was free help. Anyway, I instructed the user to add code if they needed to go beyond 16 NICs in these two areas. Looking forward to his feedback.