在运行以下脚本时,我在极少数情况下遇到错误。我编写了从 SCVMM 服务器获取数据并将其复制到 CSV 文件的脚本。该脚本每 15 分钟运行一次。我们已经使用我们的调度程序安排了这个。但这里的问题是,有时它会抛出警告,并且不会将所需的数据复制到 CSV 文件中。大多数时候它都在运行。在极少数情况下它会引发错误:
Get-SCCloud:VMM 无法连接到 VMM 管理服务器 calo-infvmm-01.calocosn.local,因为无法解析指定的计算机名称。
2016-05-15 19:01:30,334 INFO 运行系统命令:C:\cc-working\scripts\PowerShell\VMHours\Sydney\GetStorageDetails.bat -d 20160515 > C:\cc-working\usage_files\VMUphours\Sydney\输出\VMHours_20160515.csv 2016-05-15 19:01:33,874 WARN Get-SCCloud:VMM 无法连接到 VMM 管理服务器 2016-05-15 19:01:33,874 警告 calo-infvmm-01.calocosn.local 因为指定的计算机名称不能 2016-05-15 19:01:33,890 警告已解决。(错误 ID:1601) 2016-05-15 19:01:33,890 警告 2016-05-15 19:01:33,890 WARN 确保计算机名称正确,然后再次尝试操作。如果 2016-05-15 19:01:33,890 警告问题仍然存在,请联系您的网络管理员。 2016-05-15 19:01:33,890 警告在 C:\cc-working\scripts\PowerShell\VMHours\Sydney\usage.ps1:7 char:11 2016-05-15 19:01:33,890 WARN + $clouds = Get-SCCloud -VMMServer $VMMServer 2016-05-15 19:01:33,890 警告+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2016-05-15 19:01:33,890 WARN + CategoryInfo : ReadError: (:) [Get-SCCloud], CarmineException 2016-05-15 19:01:33,890 WARN + FullyQualifiedErrorId:1601,Microsoft.SystemCenter.VirtualMachineManage 2016-05-15 19:01:33,890 警告 r.Cmdlets.GetSCCloudCmdlet 2016-05-15 19:01:34,373 INFO 命令成功完成,退出值为“0” 2016-05-15 19:01:34,389 来自自动检测的信息阅读器字符集:ASCII 2016-05-15 19:01:34,389 信息输入:文件 [C:\cc-working\usage_files\VMUphours\Sydney\Output\VMHours_20160515.csv] 2016-05-15 19:01:34,404 信息收集提要:Sydney-VMHours-Collection
下面是我的脚本:
$VMMServer = "calo-infvmm-10001.calocosn.local"
$a = ((Get-Date).ToUniversalTime()).ToString("yyyyMMddTHHmmss")
$clouds = Get-SCCloud -VMMServer $VMMServer
$vmHeader = [string]::Concat("VMId",",","SubscriptionId",",","VMName",",","OperatingSystem",",","Memory(MB)",",","CPUCount",",","DiskUsed(GB)",",","DiskAllocated(GB)",",","StorageClassification",",","Owner",",","CreationTime",",","SampleTime",",","Status",",","StartAction",",","StopAction")
Write-Output $vmHeader
foreach ($cloud in $clouds) {
$VMs = Get-SCVirtualMachine -Cloud $cloud
foreach ($VM in $VMs) {
$size = 0
$maxSize = 0
$classification = "Standard"
foreach ($disk in $VM.VirtualHardDisks) {
$classification = $disk.Classification
$size += $disk.Size / 1gb
$parentDisk = $disk.ParentDisk
while ($parentDisk) {
$size += $parentDisk.Size / 1gb
$parentDisk = $parentDisk.ParentDisk
}
$maxSize += $disk.MaximumSize / 1gb
}
$vmText = [string]::Concat($vm.ID,",",$vm.UserRoleID,",",$vm.Name,",",$vm.OperatingSystem,",",$vm.Memory,",",$vm.CPUCount,",",$size,",",$maxSize,",",$classification,",",$vm.Owner,",",$vm.CreationTime,",",$a,",",$vm.Status,",",$vm.StartAction,",",$vm.StopAction)
Write-Output $vmText
}
}