我的任务是在我们的基础架构中获取PowerShell的版本和数量。我决定使用VBScript而不是试图考虑哪些PoSh版本将运行哪些命令,执行策略等。
我写的脚本非常简单,并且它返回了我想要的版本信息。但是,在获取信息并退出脚本(CMD窗口关闭)之后,在某些计算机上(并非全部,并且没有测试),有一个孤立的cscript
进程占用了所有剩余的CPU时间。第二次运行脚本会留下第二个cscript
进程,该进程现在分割CPU时间,但仍然无限制地将其固定为100%。这在SQL服务器上最为明显,因为它们必须以高CPU使用率开始。
我已经解决了手头的问题,但我在这里寻找的是可能的原因,因此我将来不再这样做了。
这是我的剧本:
On Error Resume Next
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
filTxt = "C:\psver.txt"
'This command calls powershell, runs the $PSVersion command and outputs to
'a text file
cmd = "powershell.exe -Command " & _
"[string]$PSVersionTable.PSVersion.Major +'.'+ " & _
"[string]$PSVersionTable.PSVersion.Minor > " & filTxt
'Delete the file C:\psver.txt if it exists
If objFSO.FileExists(filTxt) Then
objFSO.DeleteFile(filTxt)
End if
'This runs the command we set above
Set psv = objShell.Exec(cmd)
'Sleep for 6 seconds to allow the file to finish writing
WScript.Sleep 6000
'This section reads the new file
Set objFile = objFSO.GetFile("C:\psver.txt")
Set objFile = objFSO.OpenTextFile(filTxt, 1, False, -1)
Do Until objFile.AtEndOfStream
strVersion = objFile.ReadLine
Loop
objFile.Close
失控的过程发生在Windows 2008 R2到2016年.PowerShell版本2.0 - 5.1。
我在想,因为我没有很好地退出PowerShell,它会使cscript
会话保持打开状态,但为什么会消耗这么多资源呢?就此而言,有没有办法使用命令行很好地退出PowerShell,或者我应该创建一个.ps1文件并将其与我的.vbs文件捆绑在一起?
答案 0 :(得分:0)
过度复杂。这是一个批处理脚本,它的功能更少。
File.cmd
@ECHO OFF
SET "PS=%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Bypass"
SET "LOG=C:\Temp\PSVersion.txt"
%PS% -Command "$PSVersionTable.PSVersion.ToString() | Out-File -FilePath %LOG% -Force"
REM to view the new file:
REM notepad.exe %LOG%
无需担心ExecutionPolicy,Profiles,只需将完整版本号输出到您可以使用基础架构工具获取的文件中(尽管使用任何不错的工具,您应该能够直接调用cmd而无需脚本)。