我是PowerShell的新手,我正在尝试使用脚本来列出我的并行端口。我从here获取了PowerShell脚本:
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ParallelPort")
For Each objItem in colItems
Wscript.Echo "Availability: " & objItem.Availability
For Each strCapability in objItem.Capabilities
Wscript.Echo "Capability: " & strCapability
Next
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Device ID: " & objItem.DeviceID
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "OS Auto Discovered: " & objItem.OSAutoDiscovered
Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID
Wscript.Echo "Protocol Supported: " & objItem.ProtocolSupported
Next
执行脚本后我得到了:
PS C:\> .\script.ps1
Unexpected token '_' in expression or statement.
At C:\script.ps1:4 char:44
+ Set objWMIService = GetObject("winmgmts:" _ <<<<
+ CategoryInfo : ParserError: (_:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
我甚至尝试按&
更改$
,因为我在this脚本中看到的也不起作用。我的PowerShell有问题,还是造成问题的脚本?
PS:我在Windows XP 32位中使用PowerShell v2.0
提前谢谢!
修改
根据答案,它不是PowerShell脚本,而是vbscript。所以我现在的问题是,如果它确实是一个PowerShell脚本,这个脚本不起作用的原因:
$strComputer = "."
$colItems = get-wmiobject -class "Win32_ParallelPort" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItems) {
write-host "Availability: " $objItem.Availability
write-host "Capabilities: " $objItem.Capabilities
write-host "Capability Descriptions: " $objItem.CapabilityDescriptions
write-host "Caption: " $objItem.Caption
write-host "Configuration Manager Error Code: " $objItem.ConfigManagerErrorCode
write-host "Configuration Manager User Configuration: " $objItem.ConfigManagerUserConfig
write-host "Creation Class Name: " $objItem.CreationClassName
write-host "Description: " $objItem.Description
write-host "Device ID: " $objItem.DeviceID
write-host "DMA Support: " $objItem.DMASupport
write-host "Error Cleared: " $objItem.ErrorCleared
write-host "Error Description: " $objItem.ErrorDescription
write-host "Installation Date: " $objItem.InstallDate
write-host "Last Error Code: " $objItem.LastErrorCode
write-host "Maximum Number Controlled: " $objItem.MaxNumberControlled
write-host "Name: " $objItem.Name
write-host "Operating System Auto-Discovered: " $objItem.OSAutoDiscovered
write-host "PNP DeviceID: " $objItem.PNPDeviceID
write-host "Powe rManagement Capabilities: " $objItem.PowerManagementCapabilities
write-host "Power Management Supported: " $objItem.PowerManagementSupported
write-host "ProtocolS upported: " $objItem.ProtocolSupported
write-host "Status: " $objItem.Status
write-host "Status Information: " $objItem.StatusInfo
write-host "System Creation Class Name: " $objItem.SystemCreationClassName
write-host "System Name: " $objItem.SystemName
write-host "Time Of Last Reset: " $objItem.TimeOfLastReset
write-host
}
我收到此错误:
Missing expression after unary operator '-'.
At C:\script.ps1:4 char:2
+ - <<<< computername $strComputer
+ CategoryInfo : ParserError: (-:String) [], ParseException
+ FullyQualifiedErrorId : MissingExpressionAfterOperator
答案 0 :(得分:1)
在修改过的脚本中,第3行的反引号后面有一个空格,因此无法识别延续。在PowerShell中使用反引号进行续行时,it can't be followed by a whitespace character。消除那个空间,或者将它全部排成一行:
$colItems = get-wmiobject -class "Win32_ParallelPort" -namespace "root\CIMV2" -computername $strComputer
答案 1 :(得分:0)
您的示例脚本不是PowerShell,而是vbscript。
PowerShell要简单得多,你会使用类似的东西:
Get-WmiObject Win32_ParallelPort