PowerShell输出在手动和程序化执行之间有所不同
当手动运行vs编程时,我得到了来自同一命令的两个不同输出,我不明白为什么。PowerShell输出在手动和程序化执行之间有所不同
问题::命令powershell -Command "Get-Module -ListAvailable"
当在命令提示符下手动运行,我得到这个输出::
U:\>powershell -Command "Get-Module -ListAvailable" Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Binary 1.0.0.1 PackageManagement {Find-Package, Get-Package, Get-PackageProvider, Get-PackageSource...}
Script 1.0.0.1 PowerShellGet {Install-Module, Find-Module, Save-Module, Update-Module...}
Binary 6.5.1.6... VMware.DeployAutomation {Add-DeployRule, Add-ProxyServer, Add-ScriptBundle, Copy-DeployRule...}
Binary 6.5.1.6... VMware.ImageBuilder {Add-EsxSoftwareDepot, Add-EsxSoftwarePackage, Compare-EsxImageProfile, Export-EsxImageProfile...}
Manifest 6.5.4.7... VMware.PowerCLI
Binary 6.5.4.6... VMware.VimAutomation.Cis.Core {Connect-CisServer, Disconnect-CisServer, Get-CisService}
Binary 6.5.1.5... VMware.VimAutomation.Cloud {Add-CIDatastore, Connect-CIServer, Disconnect-CIServer, Get-Catalog...}
Manifest 6.5.4.6... VMware.VimAutomation.Common
Binary 6.5.2.6... VMware.VimAutomation.Core {Add-PassthroughDevice, Add-VirtualSwitchPhysicalNetworkAdapter, Add-VMHost, Add-VMHostNtpServer...}
Binary 6.5.4.7... VMware.VimAutomation.HA Get-DrmInfo
Binary 7.1.0.5... VMware.VimAutomation.HorizonView {Connect-HVServer, Disconnect-HVServer}
Binary 6.5.1.5... VMware.VimAutomation.License Get-LicenseDataManager
Binary 2.0.0.6... VMware.VimAutomation.Nsxt {Connect-NsxtServer, Disconnect-NsxtServer, Get-NsxtService}
Binary 6.5.1.5... VMware.VimAutomation.PCloud {Connect-PIServer, Disconnect-PIServer, Get-PIComputeInstance, Get-PIDatacenter}
Manifest 1.0.0.5... VMware.VimAutomation.Sdk {Get-PSVersion, Get-InstallPath}
Binary 6.5.1.5... VMware.VimAutomation.Srm {Connect-SrmServer, Disconnect-SrmServer}
Binary 6.5.4.7... VMware.VimAutomation.Storage {Add-KeyManagementServer, Copy-VDisk, Export-SpbmStoragePolicy, Get-KeyManagementServer...}
Script 1.1 VMware.VimAutomation.StorageUtility Update-VmfsDatastore
Binary 6.5.1.5... VMware.VimAutomation.Vds {Add-VDSwitchPhysicalNetworkAdapter, Add-VDSwitchVMHost, Export-VDPortGroup, Export-VDSwitch...}
Binary 6.5.4.7... VMware.VimAutomation.Vmc {Connect-Vmc, Disconnect-Vmc, Get-VmcService, Connect-VmcServer...}
Binary 6.5.1.5... VMware.VimAutomation.vROps {Connect-OMServer, Disconnect-OMServer, Get-OMAlert, Get-OMAlertDefinition...}
Binary 6.5.1.5... VMware.VumAutomation {Add-EntityBaseline, Copy-Patch, Get-Baseline, Get-Compliance...}
Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.0.0.0 AppLocker {Set-AppLockerPolicy, Get-AppLockerPolicy, Test-AppLockerPolicy, Get-AppLockerFileInformation...}
Manifest 1.0.0.0 BitsTransfer {Add-BitsFile, Remove-BitsTransfer, Complete-BitsTransfer, Get-BitsTransfer...}
Manifest 1.0.0.0 CimCmdlets {Get-CimAssociatedInstance, Get-CimClass, Get-CimInstance, Get-CimSession...}
Script 1.0.0.0 ISE {New-IseSnippet, Import-IseSnippet, Get-IseSnippet}
Manifest 1.0.0.0 Microsoft.PowerShell.Archive {Compress-Archive, Expand-Archive}
Manifest 3.0.0.0 Microsoft.PowerShell.Diagnostics {Get-WinEvent, Get-Counter, Import-Counter, Export-Counter...}
Manifest 3.0.0.0 Microsoft.PowerShell.Host {Start-Transcript, Stop-Transcript}
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Content, Clear-Content, Clear-ItemProperty, Join-Path...}
Script 1.0 Microsoft.PowerShell.ODataUtils Export-ODataEndpointProxy
Manifest 3.0.0.0 Microsoft.PowerShell.Security {Get-Acl, Set-Acl, Get-PfxCertificate, Get-Credential...}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Format-List, Format-Custom, Format-Table, Format-Wide...}
Manifest 3.0.0.0 Microsoft.WSMan.Management {Disable-WSManCredSSP, Enable-WSManCredSSP, Get-WSManCredSSP, Set-WSManQuickConfig...}
Manifest 1.0.0.0 NetworkSwitchManager {Disable-NetworkSwitchEthernetPort, Enable-NetworkSwitchEthernetPort, Get-NetworkSwitchEthernetPort, Remove-NetworkSwitchEthernetPortIPAddress...}
Manifest 1.1 PSDesiredStateConfiguration {Set-DscLocalConfigurationManager, Start-DscConfiguration, Test-DscConfiguration, Publish-DscConfiguration...}
Script 1.0.0.0 PSDiagnostics {Disable-PSTrace, Disable-PSWSManCombinedTrace, Disable-WSManTrace, Enable-PSTrace...}
Binary 1.1.0.0 PSScheduledJob {New-JobTrigger, Add-JobTrigger, Remove-JobTrigger, Get-JobTrigger...}
Manifest 2.0.0.0 PSWorkflow {New-PSWorkflowExecutionOption, New-PSWorkflowSession, nwsn}
Manifest 1.0.0.0 PSWorkflowUtility Invoke-AsWorkflow
Manifest 1.0.0.0 TroubleshootingPack {Get-TroubleshootingPack, Invoke-TroubleshootingPack}
Directory: C:\opscode\chefdk\modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 0.0 chef {chef-apply, chef-client, chef-service-manager, chef-shell...}
当编程方式运行(通过Java),我得到这样的输出::
ModuleType Name ExportedCommands ---------- ---- ----------------
Binary PackageManagement {Find-Package, Get-Package, Get-PackageProvider, Get-PackageSource...}
Script PowerShellGet {Install-Module, Find-Module, Save-Module, Update-Module...}
Manifest BitsTransfer {Add-BitsFile, Remove-BitsTransfer, Complete-BitsTransfer, Get-BitsTrans...
Manifest CimCmdlets {Get-CimAssociatedInstance, Get-CimClass, Get-CimInstance, Get-CimSessio...
Script ISE {New-IseSnippet, Import-IseSnippet, Get-IseSnippet}
Manifest Microsoft.PowerShell.A... {Compress-Archive, Expand-Archive}
Manifest Microsoft.PowerShell.D... {Get-WinEvent, Get-Counter, Import-Counter, Export-Counter...}
Manifest Microsoft.PowerShell.Host {Start-Transcript, Stop-Transcript}
Manifest Microsoft.PowerShell.M... {Add-Content, Clear-Content, Clear-ItemProperty, Join-Path...}
Script Microsoft.PowerShell.O... Export-ODataEndpointProxy
Manifest Microsoft.PowerShell.S... {Get-Acl, Set-Acl, Get-PfxCertificate, Get-Credential...}
Manifest Microsoft.PowerShell.U... {Format-List, Format-Custom, Format-Table, Format-Wide...}
Manifest Microsoft.WSMan.Manage... {Disable-WSManCredSSP, Enable-WSManCredSSP, Get-WSManCredSSP, Set-WSManQ...
Manifest PSDesiredStateConfigur... {Set-DscLocalConfigurationManager, Start-DscConfiguration, Test-DscConfi...
Manifest PSDiagnostics {Start-Trace, Stop-Trace, Enable-WSManTrace, Disable-WSManTrace...}
Binary PSScheduledJob {New-JobTrigger, Add-JobTrigger, Remove-JobTrigger, Get-JobTrigger...}
Manifest TroubleshootingPack {Get-TroubleshootingPack, Invoke-TroubleshootingPack}
Script chef {chef-apply, chef-client, chef-service-manager, chef-shell...}
编程运行i的输出s缺少“版本”列,并且只是可用模块的一个子集。
我想也许不知何故,我调用了两个不同的PowerShell可执行文件,所以我手动和编程运行powershell -Command "$PSVersionTable"
。
从powershell -Command "$PSVersionTable"
手动运行::
Name Value ---- -----
PSVersion 5.0.10586.117
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.10586.117
CLRVersion 4.0.30319.18444
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
从powershell -Command "$PSVersionTable"
的纲领性运行::
Name Value ---- -----
PSVersion 5.0.10586.117
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.10586.117
CLRVersion 4.0.30319.18444
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
所以我觉得我运行的输出输出相同的PowerShell可执行文件。
任何人有什么想法,为什么我运行相同的命令时,我得到2完全不同的输出?
UPDATE :: 在@EBGreen的要求下,我包含了用于以编程方式运行命令的实际Java代码。
public class Example { private void myMethod(String command) throws IOException {
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String output = "";
String line;
while((line = bufferedReader.readLine()) != null){
output += (line + "\n");
}
System.out.println((output.isEmpty() ? "No output was received!!!" : output));
BufferedReader errorBufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String errorOutput = "";
while((line = errorBufferedReader.readLine()) != null){
errorOutput += (line + "\n");
}
System.out.println((errorOutput.isEmpty() ? "Nothing in the error output stream." : errorOutput));
}
public static void main(String[] args) throws IOException {
new Example().myMethod("powershell -Command \"Get-Module -ListAvailable\"");
}
}
回答:
我99.9%肯定这是我问题的真正原因。
当我手动运行命令来检查可用模块列表时,我使用的是Powershell的64位实例。
我的Java程序运行在32位上下文中(是针对32位JDK构建的)。当我的程序调用cmd提示符时,它得到一个32位cmd提示符。当cmd提示符调用Powershell时,它得到一个32位Powershell。 PowerShell的32位和64位版本具有独特的安装路径,用于存储模块。
所以,当我手动安装模块时,我总是使用64位Powershell。我从来没有手动打开32位Powershell并安装任何模块。这就是为什么Get-Module -ListAvailable
的编程输出非常小,与我手动运行相同命令时看到的相比较。
感谢@BaconBits为我提供了帮助我识别发生了什么的线索。该技巧是执行[Environment]::Is64BitProcess
来验证我的Powershell实际运行的操作系统架构上下文。
回答:
我的猜测是,你只是没有得到从Java编程调用时PowerShell的格式化输出,或不同的版本有一些不同的原因默认格式配置。
首先,一些背景:
你在命令行中运行PowerShell中得到的输出是通过在格式文件的格式决定。这些都存储在$PSHome\*.format.ps1xml
。 Get-Module
返回System.Management.Automation.PSModuleInfo
类型的对象。如果您运行Select-String -Pattern 'PSModuleInfo' -Path "$PSHome\*.format.ps1xml"
,则可以找到默认格式的指定位置。 Format-Table
将会有Table
,这是您通常得到的默认值。 Format-Wide
也有Wide
,List
也有Format-List
。您可以在Get-Help about_Format.ps1xml
上了解有关控制台输出格式的更多信息。
由于为PSModuleInfo
默认的输出是使用表格式,你应该能够通过调用使Java比赛的PowerShell:
new Example().myMethod("powershell -Command \"Get-Module -ListAvailable | Format-Table\"");
或者可能:
new Example().myMethod("powershell -Command \"Get-Module -ListAvailable | Format-Table | Out-String\"");
,或者甚至只是:
new Example().myMethod("powershell -Command \"Get-Module -ListAvailable | Out-String\"");
至于什么Java做的不同或为什么它是n格式类似,恐怕我不知道。
您也可以随时做这样的事情:
new Example().myMethod("powershell -Command \"Get-Module -ListAvailable | Out-String\"");
你也可以有成功与Get-Module -ListAvailable | ConvertTo-Json -Compress -Depth 1
或Get-Module -ListAvailable | ConvertTo-Xml -NoTypeInformation -As String -Depth 1
或者,如果你想你的序列相似的输出。请记住,默认深度为ConvertTo-Json
为2,默认深度为ConvertTo-Xml
为1,而深度为3以上的任何内容都将为,真的很慢。
以上是 PowerShell输出在手动和程序化执行之间有所不同 的全部内容, 来源链接: utcz.com/qa/266660.html