无法解析使用PowerShell生成的CSV

时间:2015-04-20 22:47:50

标签: python parsing powershell csv

有些奇怪的事情正在发生,所以我要将我的real代码与PowerShell脚本一起粘贴(这可能很糟糕,但是嘿......)

import-module hyper-v

$vms = get-vm | select name
$allvms = @()

function getRAM
{
 param([string]$vmname )
 $ram = get-vm -name $vmname | select -ExpandProperty memorystartup
 return $ram
}

function getState
{
 param([string]$vmname )
 $state = get-vm -name $vmname | select -ExpandProperty state
 return $state
}

function getUptime
{
 param([string]$vmname )
 $uptime = get-vm -name $vmname | select -ExpandProperty uptime
 return $uptime
}

function getCPUS
{
 param([string]$vmname )
 $cpus = get-vmprocessor -vmname $vmname | select -ExpandProperty count
 return $cpus
}

foreach ($vm in $vms)
    {
    $tempvm = New-Object PSObject
    Add-Member -InputObject $tempvm -MemberType NoteProperty -Name cpus -Value ""
    Add-Member -InputObject $tempvm -MemberType NoteProperty -Name state -Value ""
    Add-Member -InputObject $tempvm -MemberType NoteProperty -Name name -Value $vm.name
    Add-Member -InputObject $tempvm -MemberType NoteProperty -Name ram -Value ""
    Add-Member -InputObject $tempvm -MemberType NoteProperty -Name uptime -Value ""

    $tempvm.ram = getRAM($vm.name)
    $tempvm.state = getState($vm.name)
    $tempvm.cpus = getCPUS($vm.name)
    $tempvm.uptime = getUptime($vm.name)

    $allvms += $tempvm
    }

$allvms | export-csv c:/users/my_username/appdata/local/temp/vmi.csv -NoTypeInformation

提升 PowerShell运行此功能会创建可以使用此Python代码解析的CSV文件。

def _parseCSV(self):
        pwd = os.getcwd()
        os.chdir(tempfile.gettempdir())
        print 'currently in dir', os.getcwd()
        try:
            csvfile = open('vmi.csv', 'r')

            reader = csv.reader(csvfile)
            print type(reader), reader
            for row in reader:
                    print row
            os.chdir(pwd)
            print 'currently in dir', pwd
        except Exception as ex:
            raise ex
        finally:
            csvfile.close()

使用常规PowerShell运行它会产生一个空文件,可能是Hyper-V模块的错误。

理想的情况是我的Python代码执行上面的PowerShell脚本,该脚本在以后由同一个Python脚本解析后写入CSV ...除了从Python运行PowerShell作为管理员很棘手,所以我正在使用这个

    def _runPowershell(self):
        sp.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
                                 '-ExecutionPolicy',
                                 'Unrestricted',
                                 'Start-Process',
                                 'powershell',
                                 '-WindowStyle Hidden',
                                 '-verb',
                                 'runas',
                                 '-argumentlist',
                                 '"-file '+self.PSPath+'"'])
    #self.PSPath is c:/users/my_username/appdata/local/temp/vmi.ps1

我的Python脚本无法解析由此怪物产生的CSV文件。

CSV输出

"cpus","state","name","ram","uptime"
"1","Running","vm1","268435456","00:35:46"
"2","Off","vm2","536870912","00:00:00"

1 个答案:

答案 0 :(得分:0)

代码工作正常,但PowerShell需要一些时间来实际关闭(保存)文件。为了解决这个问题,我在time.sleep(1)函数中添加了1秒的暂停(_parseCSV())。