INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Arrys in Powershell

Arrys in Powershell

(OP)
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 --> VBSCRIPT

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 

RE: Arrys in Powershell

Here is the comparision:

Vbscript: array.vbs

CODE

' run:
' cscript /NoLogo array.vbs

dim arrErrors 
arrErrors = Array("APP0", "APP1", "APP2", "APP3", "APP4", _
"APP5", "APP6", "APP7", "APP8", "APP9", "APP10", "APP11", "APP12")

strErrorLine = "APP5 Error Line!"
strComputer  = "PC1"

for i = 0 to ubound(arrErrors)
  strError = arrErrors(i)
  if (left(lcase(strErrorLine), len(strError)) = lcase(strError)) then
     Wscript.Echo strErrorLine & vbTab & strComputer & _
     vbTab & "Found Error number" & vbTab & i
     exit for
  end If
next 

PowerShell: array.ps1

CODE

# run:
# powershell -ExecutionPolicy RemoteSigned .\array.ps1
 
$arrErrors = @("APP0", "APP1", "APP2", "APP3", "APP4", 
 "APP5", "APP6", "APP7", "APP8", "APP9", "APP10", "APP11", "APP12")

$strErrorLine = "APP5 Error Line!"
$strComputer  = "PC1"

for ($i = 0; $i -lt $arrErrors.length; $i++ ) {
  $strError = $arrErrors[$i]
  if ($strErrorLine.ToLower().StartsWith($strError.ToLower())) {
    echo "$strErrorLine`t$strComputer`tFound Error number`t$i"
    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 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close