我无法从.cmd调用.ps1。
powershell脚本可以自行运行(它从.txt文件中的计算机列表中获取CPU利用率和正常运行时间,并将信息输出为“.. \ $ Server”+“。txt”,每个文件一个文件参数;它作为后台作业同时针对参数列表运行以加速它。)
问题不在于powershell脚本本身 - 它只是在从example.cmd调用或作为计划任务(实际上是同一件事)时出现问题,这会导致PowerShell崩溃,因为“Powershell已经停止工作”对话框。
是否有一个技巧可以从dos提示符调用powershell脚本而不会崩溃?
仅仅是因为powershell脚本作为后台作业运行导致问题吗?
Powershell V2.0是原因吗?
提前致谢。
$Scriptblock = {
param
(
$Server,
$ExportCSV
)
$Uptime = (get-date)-([System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -ComputerName $Server win32_operatingsystem).lastbootuptime))
$uptimeval = "$($Uptime.days)d $($Uptime.Hours)h $($Uptime.Minutes)m"
$Counter = Get-Counter -ComputerName $Server "\Processor(_Total)\% Processor Time" -MaxSamples 1
$CounterSamples = $Counter | % {$_.CounterSamples}
$CounterSamplesDecimal = "{0:N0}" -f $CounterSamples.cookedvalue
$MasterArray = @()
$MasterArray += $CounterSamplesDecimal
$MasterArray += $CounterSamplesDecimal
$MasterArray += $uptimeval
$MasterArray += $Server
$MasterArray | Out-file -Filepath $ExportCSV
}
$Servers = get-content -Path "D:\SERVERS.txt"
foreach ($Server in $Servers)
{
$ExportCSVFile = "D:\$Server" + ".txt"
Start-Job -ScriptBlock $ScriptBlock -ArgumentList @($Server, $ExportCSVFile)
}