Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Convert VBS Script to Powershell?

Reply
 
Old 09-19-2007   #1 (permalink)
Gregor Kurth


 
 

Convert VBS Script to Powershell?

Hello

How convert this vbs script to Powershell?

I hope someone can me help

Thanks Gregor

----------------------------------------------------------
Set objDictionary = CreateObject("Scripting.Dictionary")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colPrinters
objDictionary.Add objPrinter.PortName, objPrinter.PortName
Next
Set colPorts = objWMIService.ExecQuery _
("Select * from Win32_TCPIPPrinterPort")
For Each objPort in colPorts
If objDictionary.Exists(objPort.Name) Then
strUsedPorts = strUsedPorts & _
objDictionary.Item(objPort.Name) & VbCrLf
Else
strFreePorts = strFreePorts & objPort.Name & vbCrLf
End If
Next
Wscript.Echo "The following ports are in use: " & VbCrLf & strUsedPorts
Wscript.Echo "The following ports are available: " & VbCrLf & strFreePorts

My System SpecsSystem Spec
Old 09-19-2007   #2 (permalink)
Shay Levi


 
 

Re: Convert VBS Script to Powershell?

Try this

$strComputer = "."
$colPrinters = get-wmiobject -query "select PortName from Win32_Printer"
-computer $strComputer | select portname
$colPorts = get-wmiobject -query "Select name from Win32_TCPIPPrinterPort"
-computer $strComputer | select name

$colPorts | where {$_.name -match $colPrinters.portname}



Shay
http://scriptolog.blogspot.com


Quote:

> Hello
>
> How convert this vbs script to Powershell?
>
> I hope someone can me help
>
> Thanks Gregor
>
> ----------------------------------------------------------
> Set objDictionary = CreateObject("Scripting.Dictionary")
> strComputer = "."
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer &
> "\root\cimv2")
> Set colPrinters = objWMIService.ExecQuery _
> ("Select * from Win32_Printer")
> For Each objPrinter in colPrinters
> objDictionary.Add objPrinter.PortName, objPrinter.PortName
> Next
> Set colPorts = objWMIService.ExecQuery _
> ("Select * from Win32_TCPIPPrinterPort")
> For Each objPort in colPorts
> If objDictionary.Exists(objPort.Name) Then
> strUsedPorts = strUsedPorts & _
> objDictionary.Item(objPort.Name) & VbCrLf
> Else
> strFreePorts = strFreePorts & objPort.Name & vbCrLf
> End If
> Next
> Wscript.Echo "The following ports are in use: " & VbCrLf &
> strUsedPorts Wscript.Echo "The following ports are available: " &
> VbCrLf & strFreePorts
>

My System SpecsSystem Spec
Old 09-19-2007   #3 (permalink)
Gregor Kurth


 
 

Re: Convert VBS Script to Powershell?

Hello Shay Levi

Very thanks for you help

The output is not the same? Do you have a idea, why?

Thanks Gregor

Output form PS:

name
----
IP_172.20.1.50
IP_172.20.1.23


Ouput from VBS Script:

The following ports are in use:
IP_172.20.1.50

The following ports are available:
IP_172.20.1.23





"Shay Levi" <no@xxxxxx> wrote in message
news:8766a94479208c9c8d5e5a8e70a@xxxxxx
Quote:

> Try this
>
> $strComputer = "." $colPrinters = get-wmiobject -query "select PortName
> from Win32_Printer" -computer $strComputer | select portname
> $colPorts = get-wmiobject -query "Select name from
> Win32_TCPIPPrinterPort" -computer $strComputer | select name
>
> $colPorts | where {$_.name -match $colPrinters.portname}
>
>
>
> Shay
> http://scriptolog.blogspot.com
>
>
>
Quote:

>> Hello
>>
>> How convert this vbs script to Powershell?
>>
>> I hope someone can me help
>>
>> Thanks Gregor
>>
>> ----------------------------------------------------------
>> Set objDictionary = CreateObject("Scripting.Dictionary")
>> strComputer = "."
>> Set objWMIService = GetObject("winmgmts:" _
>> & "{impersonationLevel=impersonate}!\\" & strComputer &
>> "\root\cimv2")
>> Set colPrinters = objWMIService.ExecQuery _
>> ("Select * from Win32_Printer")
>> For Each objPrinter in colPrinters
>> objDictionary.Add objPrinter.PortName, objPrinter.PortName
>> Next
>> Set colPorts = objWMIService.ExecQuery _
>> ("Select * from Win32_TCPIPPrinterPort")
>> For Each objPort in colPorts
>> If objDictionary.Exists(objPort.Name) Then
>> strUsedPorts = strUsedPorts & _
>> objDictionary.Item(objPort.Name) & VbCrLf
>> Else
>> strFreePorts = strFreePorts & objPort.Name & vbCrLf
>> End If
>> Next
>> Wscript.Echo "The following ports are in use: " & VbCrLf &
>> strUsedPorts Wscript.Echo "The following ports are available: " &
>> VbCrLf & strFreePorts
>>
>
>
My System SpecsSystem Spec
Old 09-19-2007   #4 (permalink)
Jon


 
 

Re: Convert VBS Script to Powershell?

"Gregor Kurth" <nospam@xxxxxx> wrote in message
news:CF40A687-B4D2-4BD1-B128-CE8197A17948@xxxxxx
Quote:

> Hello
>
> How convert this vbs script to Powershell?
>
> I hope someone can me help
>
> Thanks Gregor
>
> ----------------------------------------------------------
> Set objDictionary = CreateObject("Scripting.Dictionary") strComputer = "."
> Set objWMIService = GetObject("winmgmts:" _ &
> "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set
> colPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer")
> For Each objPrinter in colPrinters objDictionary.Add objPrinter.PortName,
> objPrinter.PortName Next Set colPorts = objWMIService.ExecQuery _ ("Select
> * from Win32_TCPIPPrinterPort") For Each objPort in colPorts If
> objDictionary.Exists(objPort.Name) Then strUsedPorts = strUsedPorts & _
> objDictionary.Item(objPort.Name) & VbCrLf Else strFreePorts = strFreePorts
> & objPort.Name & vbCrLf End If Next Wscript.Echo "The following ports are
> in use: " & VbCrLf & strUsedPorts Wscript.Echo "The following ports are
> available: " & VbCrLf & strFreePorts




There's a useful guide here to converting vbs to Powershell, including a
downloadable document.

Converting VBScript Commands to Windows PowerShell Commands
http://www.microsoft.com/technet/scr...t/default.mspx

--
Jon



My System SpecsSystem Spec
Old 09-19-2007   #5 (permalink)
Shay Levi


 
 

Re: Convert VBS Script to Powershell?

try to use -notmatch

Shay
http://scriptolog.blogspot.com


Quote:

> Hello Shay Levi
>
> Very thanks for you help
>
> The output is not the same? Do you have a idea, why?
>
> Thanks Gregor
>
> Output form PS:
>
> name
> ----
> IP_172.20.1.50
> IP_172.20.1.23
> Ouput from VBS Script:
>
> The following ports are in use:
> IP_172.20.1.50
> The following ports are available:
> IP_172.20.1.23
> "Shay Levi" <no@xxxxxx> wrote in message
> news:8766a94479208c9c8d5e5a8e70a@xxxxxx
>
Quote:

>> Try this
>>
>> $strComputer = "." $colPrinters = get-wmiobject -query "select
>> PortName
>> from Win32_Printer" -computer $strComputer | select portname
>> $colPorts = get-wmiobject -query "Select name from
>> Win32_TCPIPPrinterPort" -computer $strComputer | select name
>> $colPorts | where {$_.name -match $colPrinters.portname}
>>
>> Shay
>> http://scriptolog.blogspot.com
Quote:

>>> Hello
>>>
>>> How convert this vbs script to Powershell?
>>>
>>> I hope someone can me help
>>>
>>> Thanks Gregor
>>>
>>> ----------------------------------------------------------
>>> Set objDictionary = CreateObject("Scripting.Dictionary")
>>> strComputer = "."
>>> Set objWMIService = GetObject("winmgmts:" _
>>> & "{impersonationLevel=impersonate}!\\" & strComputer &
>>> "\root\cimv2")
>>> Set colPrinters = objWMIService.ExecQuery _
>>> ("Select * from Win32_Printer")
>>> For Each objPrinter in colPrinters
>>> objDictionary.Add objPrinter.PortName, objPrinter.PortName
>>> Next
>>> Set colPorts = objWMIService.ExecQuery _
>>> ("Select * from Win32_TCPIPPrinterPort")
>>> For Each objPort in colPorts
>>> If objDictionary.Exists(objPort.Name) Then
>>> strUsedPorts = strUsedPorts & _
>>> objDictionary.Item(objPort.Name) & VbCrLf
>>> Else
>>> strFreePorts = strFreePorts & objPort.Name & vbCrLf
>>> End If
>>> Next
>>> Wscript.Echo "The following ports are in use: " & VbCrLf &
>>> strUsedPorts Wscript.Echo "The following ports are available: " &
>>> VbCrLf & strFreePorts

My System SpecsSystem Spec
Old 09-19-2007   #6 (permalink)
Gregor Kurth


 
 

Re: Convert VBS Script to Powershell?

Hello

I cannot convert it, where is the error?

$strComputer = "."
$colPrinters = get-wmiobject -query "select PortName from
Win32_Printer" -computer $strComputer | select portname
$colPorts = get-wmiobject -query "Select name from
Win32_TCPIPPrinterPort" -computer $strComputer | select name


#$colPorts | where {$_.name -match $colPrinters.portname}

foreach ($objPrinter in $colPrinters)
{
$objprinter.portname
}
foreach ($objPorts in $colPorts)
{
$objPorts.name
}

Now - How Check the two foreach?

Thanks
Gregor




"Shay Levi" <no@xxxxxx> wrote in message
news:8766a94479208c9c8d5e5a8e70a@xxxxxx
Quote:

> Try this
>
> $strComputer = "." $colPrinters = get-wmiobject -query "select PortName
> from Win32_Printer" -computer $strComputer | select portname
> $colPorts = get-wmiobject -query "Select name from
> Win32_TCPIPPrinterPort" -computer $strComputer | select name
>
> $colPorts | where {$_.name -match $colPrinters.portname}
>
>
>
> Shay
> http://scriptolog.blogspot.com
>
>
>
Quote:

>> Hello
>>
>> How convert this vbs script to Powershell?
>>
>> I hope someone can me help
>>
>> Thanks Gregor
>>
>> ----------------------------------------------------------
>> Set objDictionary = CreateObject("Scripting.Dictionary")
>> strComputer = "."
>> Set objWMIService = GetObject("winmgmts:" _
>> & "{impersonationLevel=impersonate}!\\" & strComputer &
>> "\root\cimv2")
>> Set colPrinters = objWMIService.ExecQuery _
>> ("Select * from Win32_Printer")
>> For Each objPrinter in colPrinters
>> objDictionary.Add objPrinter.PortName, objPrinter.PortName
>> Next
>> Set colPorts = objWMIService.ExecQuery _
>> ("Select * from Win32_TCPIPPrinterPort")
>> For Each objPort in colPorts
>> If objDictionary.Exists(objPort.Name) Then
>> strUsedPorts = strUsedPorts & _
>> objDictionary.Item(objPort.Name) & VbCrLf
>> Else
>> strFreePorts = strFreePorts & objPort.Name & vbCrLf
>> End If
>> Next
>> Wscript.Echo "The following ports are in use: " & VbCrLf &
>> strUsedPorts Wscript.Echo "The following ports are available: " &
>> VbCrLf & strFreePorts
>>
>
>
My System SpecsSystem Spec
Old 09-19-2007   #7 (permalink)
Gregor Kurth


 
 

Re: Convert VBS Script to Powershell?

With -notmatch - I get null?


"Shay Levi" <no@xxxxxx> wrote in message
news:8766a944795f8c9c8e9a69a0634@xxxxxx
Quote:

> try to use -notmatch
>
> Shay
> http://scriptolog.blogspot.com
>
>
>
Quote:

>> Hello Shay Levi
>>
>> Very thanks for you help
>>
>> The output is not the same? Do you have a idea, why?
>>
>> Thanks Gregor
>>
>> Output form PS:
>>
>> name
>> ----
>> IP_172.20.1.50
>> IP_172.20.1.23
>> Ouput from VBS Script:
>>
>> The following ports are in use:
>> IP_172.20.1.50
>> The following ports are available:
>> IP_172.20.1.23
>> "Shay Levi" <no@xxxxxx> wrote in message
>> news:8766a94479208c9c8d5e5a8e70a@xxxxxx
>>
Quote:

>>> Try this
>>>
>>> $strComputer = "." $colPrinters = get-wmiobject -query "select
>>> PortName
>>> from Win32_Printer" -computer $strComputer | select portname
>>> $colPorts = get-wmiobject -query "Select name from
>>> Win32_TCPIPPrinterPort" -computer $strComputer | select name
>>> $colPorts | where {$_.name -match $colPrinters.portname}
>>>
>>> Shay
>>> http://scriptolog.blogspot.com
>>>> Hello
>>>>
>>>> How convert this vbs script to Powershell?
>>>>
>>>> I hope someone can me help
>>>>
>>>> Thanks Gregor
>>>>
>>>> ----------------------------------------------------------
>>>> Set objDictionary = CreateObject("Scripting.Dictionary")
>>>> strComputer = "."
>>>> Set objWMIService = GetObject("winmgmts:" _
>>>> & "{impersonationLevel=impersonate}!\\" & strComputer &
>>>> "\root\cimv2")
>>>> Set colPrinters = objWMIService.ExecQuery _
>>>> ("Select * from Win32_Printer")
>>>> For Each objPrinter in colPrinters
>>>> objDictionary.Add objPrinter.PortName, objPrinter.PortName
>>>> Next
>>>> Set colPorts = objWMIService.ExecQuery _
>>>> ("Select * from Win32_TCPIPPrinterPort")
>>>> For Each objPort in colPorts
>>>> If objDictionary.Exists(objPort.Name) Then
>>>> strUsedPorts = strUsedPorts & _
>>>> objDictionary.Item(objPort.Name) & VbCrLf
>>>> Else
>>>> strFreePorts = strFreePorts & objPort.Name & vbCrLf
>>>> End If
>>>> Next
>>>> Wscript.Echo "The following ports are in use: " & VbCrLf &
>>>> strUsedPorts Wscript.Echo "The following ports are available: " &
>>>> VbCrLf & strFreePorts
>
>
My System SpecsSystem Spec
Old 09-19-2007   #8 (permalink)
Shay Levi


 
 

Re: Convert VBS Script to Powershell?

Does this help?

Compare-Object $colPorts $colPrinters | foreach {$_.InputObject}

Shay
http://scriptolog.blogspot.com


Quote:

> With -notmatch - I get null?
>
> "Shay Levi" <no@xxxxxx> wrote in message
> news:8766a944795f8c9c8e9a69a0634@xxxxxx
>
Quote:

>> try to use -notmatch
>>
>> Shay
>> http://scriptolog.blogspot.com
Quote:

>>> Hello Shay Levi
>>>
>>> Very thanks for you help
>>>
>>> The output is not the same? Do you have a idea, why?
>>>
>>> Thanks Gregor
>>>
>>> Output form PS:
>>>
>>> name
>>> ----
>>> IP_172.20.1.50
>>> IP_172.20.1.23
>>> Ouput from VBS Script:
>>> The following ports are in use:
>>> IP_172.20.1.50
>>> The following ports are available:
>>> IP_172.20.1.23
>>> "Shay Levi" <no@xxxxxx> wrote in message
>>> news:8766a94479208c9c8d5e5a8e70a@xxxxxx
>>>> Try this
>>>>
>>>> $strComputer = "." $colPrinters = get-wmiobject -query "select
>>>> PortName
>>>> from Win32_Printer" -computer $strComputer | select portname
>>>> $colPorts = get-wmiobject -query "Select name from
>>>> Win32_TCPIPPrinterPort" -computer $strComputer | select name
>>>> $colPorts | where {$_.name -match $colPrinters.portname}
>>>> Shay
>>>> http://scriptolog.blogspot.com
>>>>> Hello
>>>>>
>>>>> How convert this vbs script to Powershell?
>>>>>
>>>>> I hope someone can me help
>>>>>
>>>>> Thanks Gregor
>>>>>
>>>>> ----------------------------------------------------------
>>>>> Set objDictionary = CreateObject("Scripting.Dictionary")
>>>>> strComputer = "."
>>>>> Set objWMIService = GetObject("winmgmts:" _
>>>>> & "{impersonationLevel=impersonate}!\\" & strComputer &
>>>>> "\root\cimv2")
>>>>> Set colPrinters = objWMIService.ExecQuery _
>>>>> ("Select * from Win32_Printer")
>>>>> For Each objPrinter in colPrinters
>>>>> objDictionary.Add objPrinter.PortName, objPrinter.PortName
>>>>> Next
>>>>> Set colPorts = objWMIService.ExecQuery _
>>>>> ("Select * from Win32_TCPIPPrinterPort")
>>>>> For Each objPort in colPorts
>>>>> If objDictionary.Exists(objPort.Name) Then
>>>>> strUsedPorts = strUsedPorts & _
>>>>> objDictionary.Item(objPort.Name) & VbCrLf
>>>>> Else
>>>>> strFreePorts = strFreePorts & objPort.Name & vbCrLf
>>>>> End If
>>>>> Next
>>>>> Wscript.Echo "The following ports are in use: " & VbCrLf &
>>>>> strUsedPorts Wscript.Echo "The following ports are available: " &
>>>>> VbCrLf & strFreePorts

My System SpecsSystem Spec
Old 09-19-2007   #9 (permalink)
Gregor Kurth


 
 

Re: Convert VBS Script to Powershell?

Sorry - no



"Shay Levi" <no@xxxxxx> wrote in message
news:8766a94479808c9c8f2dfb59308@xxxxxx
Quote:

> Does this help?
>
> Compare-Object $colPorts $colPrinters | foreach {$_.InputObject}
>
> Shay
> http://scriptolog.blogspot.com
>
>
>
Quote:

>> With -notmatch - I get null?
>>
>> "Shay Levi" <no@xxxxxx> wrote in message
>> news:8766a944795f8c9c8e9a69a0634@xxxxxx
>>
Quote:

>>> try to use -notmatch
>>>
>>> Shay
>>> http://scriptolog.blogspot.com
>>>> Hello Shay Levi
>>>>
>>>> Very thanks for you help
>>>>
>>>> The output is not the same? Do you have a idea, why?
>>>>
>>>> Thanks Gregor
>>>>
>>>> Output form PS:
>>>>
>>>> name
>>>> ----
>>>> IP_172.20.1.50
>>>> IP_172.20.1.23
>>>> Ouput from VBS Script:
>>>> The following ports are in use:
>>>> IP_172.20.1.50
>>>> The following ports are available:
>>>> IP_172.20.1.23
>>>> "Shay Levi" <no@xxxxxx> wrote in message
>>>> news:8766a94479208c9c8d5e5a8e70a@xxxxxx
>>>>> Try this
>>>>>
>>>>> $strComputer = "." $colPrinters = get-wmiobject -query "select
>>>>> PortName
>>>>> from Win32_Printer" -computer $strComputer | select portname
>>>>> $colPorts = get-wmiobject -query "Select name from
>>>>> Win32_TCPIPPrinterPort" -computer $strComputer | select name
>>>>> $colPorts | where {$_.name -match $colPrinters.portname}
>>>>> Shay
>>>>> http://scriptolog.blogspot.com
>>>>>> Hello
>>>>>>
>>>>>> How convert this vbs script to Powershell?
>>>>>>
>>>>>> I hope someone can me help
>>>>>>
>>>>>> Thanks Gregor
>>>>>>
>>>>>> ----------------------------------------------------------
>>>>>> Set objDictionary = CreateObject("Scripting.Dictionary")
>>>>>> strComputer = "."
>>>>>> Set objWMIService = GetObject("winmgmts:" _
>>>>>> & "{impersonationLevel=impersonate}!\\" & strComputer &
>>>>>> "\root\cimv2")
>>>>>> Set colPrinters = objWMIService.ExecQuery _
>>>>>> ("Select * from Win32_Printer")
>>>>>> For Each objPrinter in colPrinters
>>>>>> objDictionary.Add objPrinter.PortName, objPrinter.PortName
>>>>>> Next
>>>>>> Set colPorts = objWMIService.ExecQuery _
>>>>>> ("Select * from Win32_TCPIPPrinterPort")
>>>>>> For Each objPort in colPorts
>>>>>> If objDictionary.Exists(objPort.Name) Then
>>>>>> strUsedPorts = strUsedPorts & _
>>>>>> objDictionary.Item(objPort.Name) & VbCrLf
>>>>>> Else
>>>>>> strFreePorts = strFreePorts & objPort.Name & vbCrLf
>>>>>> End If
>>>>>> Next
>>>>>> Wscript.Echo "The following ports are in use: " & VbCrLf &
>>>>>> strUsedPorts Wscript.Echo "The following ports are available: " &
>>>>>> VbCrLf & strFreePorts
>
>
My System SpecsSystem Spec
Old 09-19-2007   #10 (permalink)
Kiron


 
 

Re: Convert VBS Script to Powershell?

Complemented Shay's script a bit using the -contains operator to compare
each $colPrinters element against the $colPorts collection. Also used a
different approach with Get-WmiObject for demonstration

$strComputer = "."
$colPrinters = get-wmiObject Win32_Printer -computer $strComputer | foreach
{$_.portName}
$colPorts = get-wmiobject Win32_TCPIPPrinterPort" -computer $strComputer |
foreach {$_.name}
$used = $free = ''
$colPorts | foreach {if ($colPrinters -contains $_) {[array]$used += $_}
else {[array]$free += $_}}
if ($used) {write-host "The following ports are in use:" -noNew; $used}
if ($free) {write-host "The following ports are available:" -noNew; $free}

--
Kiron

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
I've got .bat files that I'd like to convert to powershell. PowerShell
Problem with convert from WSH-script PowerShell
Convert CDOEXM connection from vbscript to powershell PowerShell
when run powershell script as windows service ,powershell fail PowerShell
HowTo: Convert Function to script PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

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