![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| | 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 Specs![]() |
| | #8 (permalink) |
| | 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 Specs![]() |
| | #9 (permalink) |
| | 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 Specs![]() |
| | #10 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||