Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrys in Powershell

Status
Not open for further replies.

Hun9865

Technical User
Oct 23, 2012
23
GB
I used to do array in VBSCRIPT like below… I am not sure how I should do it in PowerShell… Can any help me out…?

Code:
dim arrErrors(12)
arrErrors(0) = "APP0"
arrErrors(1) = " APP1"
arrErrors(2) = " APP2"
arrErrors(3) = " APP3”
arrErrors(4) = "APP4"
arrErrors(5) = "APP5"
arrErrors(6) = "APP6"
arrErrors(7) = "APP7"
arrErrors(8) = "APP8"
arrErrors(9) = "APP9"
arrErrors(10) = "APP10"
arrErrors(11) = "APP11"
arrErrors(12) = "APP12"
for i = 0 to ubound(arrErrors)
	strError = arrErrors(i)
	if (left(lcase(strErrorLine), len(strError)) = lcase(strError)) then
	objErrorLog.WriteLine strErrorLine & vbTab & strComputer & vbTab & "Found Error number" & vbTab & i
	exit for
	end If
 
Here is the comparision:

Vbscript: array.vbs
Code:
[COLOR=#0000ff]' run:[/color]
[COLOR=#0000ff]' cscript /NoLogo array.vbs[/color]

[COLOR=#804040][b]dim[/b][/color] arrErrors 
arrErrors [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]Array[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"APP0"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"APP1"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"APP2"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"APP3"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"APP4"[/color][COLOR=#804040][b], _[/b][/color]
[COLOR=#ff00ff]"APP5"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"APP6"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"APP7"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"APP8"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"APP9"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"APP10"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"APP11"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"APP12"[/color][COLOR=#804040][b])[/b][/color]

strErrorLine [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"APP5 Error Line!"[/color]
strComputer  [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"PC1"[/color]

[COLOR=#804040][b]for[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color] [COLOR=#804040][b]to[/b][/color] [COLOR=#008080]ubound[/color][COLOR=#804040][b]([/b][/color]arrErrors[COLOR=#804040][b])[/b][/color]
  strError [COLOR=#804040][b]=[/b][/color] arrErrors[COLOR=#804040][b]([/b][/color]i[COLOR=#804040][b])[/b][/color]
  [COLOR=#804040][b]if[/b][/color] [COLOR=#804040][b]([/b][/color][COLOR=#008080]left[/color][COLOR=#804040][b]([/b][/color][COLOR=#008080]lcase[/color][COLOR=#804040][b]([/b][/color]strErrorLine[COLOR=#804040][b]),[/b][/color] [COLOR=#804040][b]len[/b][/color][COLOR=#804040][b]([/b][/color]strError[COLOR=#804040][b]))[/b][/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]lcase[/color][COLOR=#804040][b]([/b][/color]strError[COLOR=#804040][b]))[/b][/color] [COLOR=#804040][b]then[/b][/color]
     Wscript[COLOR=#804040][b].[/b][/color]Echo strErrorLine [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]vbTab[/color] [COLOR=#804040][b]&[/b][/color] strComputer [COLOR=#804040][b]& _[/b][/color]
     [COLOR=#ff00ff]vbTab[/color] [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"Found Error number"[/color] [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]vbTab[/color] [COLOR=#804040][b]&[/b][/color] i
     [COLOR=#804040][b]exit[/b][/color] [COLOR=#804040][b]for[/b][/color]
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]If[/b][/color]
[COLOR=#804040][b]next[/b][/color]

PowerShell: array.ps1
Code:
[COLOR=#0000ff]# run:[/color]
[COLOR=#0000ff]# powershell -ExecutionPolicy RemoteSigned .\array.ps1[/color]
 
$arrErrors = @([COLOR=#ff00ff]"APP0"[/color], [COLOR=#ff00ff]"APP1"[/color], [COLOR=#ff00ff]"APP2"[/color], [COLOR=#ff00ff]"APP3"[/color], [COLOR=#ff00ff]"APP4"[/color], 
 [COLOR=#ff00ff]"APP5"[/color], [COLOR=#ff00ff]"APP6"[/color], [COLOR=#ff00ff]"APP7"[/color], [COLOR=#ff00ff]"APP8"[/color], [COLOR=#ff00ff]"APP9"[/color], [COLOR=#ff00ff]"APP10"[/color], [COLOR=#ff00ff]"APP11"[/color], [COLOR=#ff00ff]"APP12"[/color])

$strErrorLine = [COLOR=#ff00ff]"APP5 Error Line!"[/color]
$strComputer  = [COLOR=#ff00ff]"PC1"[/color]

for ($i = 0; $i -lt $arrErrors.length; $i++ ) {
  $strError = $arrErrors[$i]
  if ($strErrorLine.ToLower().StartsWith($strError.ToLower())) {
    echo [COLOR=#ff00ff]"$strErrorLine`t$strComputer`tFound Error number`t$i"[/color]
    exit
  }
}

The output
Code:
C:\Work\PowerShell>cscript /NoLogo array.vbs
APP5 Error Line!        PC1     Found Error number      5

C:\Work\PowerShell>powershell -ExecutionPolicy RemoteSigned .\array.ps1
APP5 Error Line!        PC1     Found Error number      5
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top