我认为,就像@Viet Hoang 一样,这是一个简单的海拔问题(因为这通常是 90% 的问题)。因此,在盲目的系统管理员信心下,我决定对其进行测试。让我们以管理员身份启动 PowerShell,然后运行:
PS C:\> (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
True
好的。好的。我们有一个真正提升的 PowerShell 会话。让我们启动 Python,一劳永逸地证明 @Lolman 不知道他在做什么,并证明 Python 不知道如何启动适当的提升 PowerShell 会话:
PS C:\> python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess, io
>>> p = subprocess.Popen(["powershell.exe", "(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)"], stdout=subprocess.PIPE)
>>> for line in io.TextIOWrapper(p.stdout, encoding="utf-8"):
... line = " ".join(line.split())
... print(line)
...
True
太棒了!正如预期的那样,它返回了......嗯......呃 - 哦......True ........嗯......嗯......坚果。有了这个想法。 @Lolman 实际上确实知道如何尝试。
好的。现在这需要一些创造性的思考。让我们比较一下我得到的一些条目:
PowerShell
PS C:\> Get-Process | Where-Object { $_.MainWindowTitle } | Format-List Id,Name,Path
...
Id : 2660
Name : powershell
Path : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Id : 31200
Name : Code
Path : C:\Users\HAL9256\AppData\Local\Programs\Microsoft VS Code\Code.exe
Id : 22804
Name : devenv
Path : C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe
...
Python
PS C:\> python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess, io
>>> p = subprocess.Popen(["powershell.exe", "Get-Process | Where-Object { $_.MainWindowTitle } | Format-List Id,Name,Path"], stdout=subprocess.PIPE)
>>> for line in io.TextIOWrapper(p.stdout, encoding="utf-8"):
... line = " ".join(line.split())
... print(line)
...
Id : 2660
Name : powershell
Path :
Id : 31200
Name : Code
Path :
Id : 22804
Name : devenv
Path : C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe
...
那么,让我们看看显示路径信息的进程与不显示路径信息的进程之间的区别。我看到很多 Program Files (x86),但没有看到 Program Files...嗯...这看起来像是 32 位与 64 位的问题。
为了证明这一点,我以管理员身份启动了 32 位 Windows PowerShell (x86),并运行了相同的命令:
Windows PowerShell (x86)
PS C:\> (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
True
PS C:\> Get-Process | Where-Object { $_.MainWindowTitle } | Format-List Id,Name,Path
...
Id : 2660
Name : powershell
Path :
Id : 31200
Name : Code
Path :
Id : 22804
Name : devenv
Path : C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe
...
啊哈! 确实看起来像是 32 位与 64 位的问题。行。所以,让我们证明这一点。让我们下载 Python 64 位再试一次:
PS C:\> C:\Users\HAL9256\AppData\Local\Programs\Python\Python38\python.exe
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess, io
>>> p = subprocess.Popen(["powershell.exe", "Get-Process | Where-Object { $_.MainWindowTitle } | Format-List Id,Name,Path"], stdout=subprocess.PIPE)
>>> for line in io.TextIOWrapper(p.stdout, encoding="utf-8"):
... line = " ".join(line.split())
... print(line)
...
Id : 2660
Name : powershell
Path : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Id : 31200
Name : Code
Path : C:\Users\HAL9256\AppData\Local\Programs\Microsoft VS Code\Code.exe
Id : 22804
Name : devenv
Path : C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe
...
是的!我们现在得到了所有的信息。 PowerShell 控制台以 64 位运行,因此可以查看完整路径和进程信息。 Python 默认是 32 位的,因此看不到 64 位进程的完整路径和进程信息。只有通过显式运行 64 位版本的 Python(来自提升的会话),我们才能看到所有路径和进程信息。