0

SCVMMの「get-vm」コマンドからの情報をExcelブックに入力するPowerShellスクリプトを作成しています。

ファイルの保存パスは現在進行中の作業です。スクリプトを毎日または毎週実行し、生成されたExcelワークブックをその日の日付をファイル名として使用して保存したいと思います。これは可能ですか?日付を生成し、Excel出力を保存するためのファイル名として使用するにはどうすればよいですか?どんな助けでも素晴らしいでしょう。

#run below line once and then comment out if not in VMM Command Shell. Will import modules for ISE/Powershell.
#Import-Module "C:\Program Files\Microsoft System Center 2012\Virtual Machine Manager\bin\psModules\virtualmachinemanager\virtualmachinemanager"

$server = Get-VMMServer -ComputerName "server"

$vminfo = Get-VM -VMMServer $server 

$xl=New-Object -ComObject "Excel.Application"
$wb=$xl.Workbooks.Add()
$ws=$wb.ActiveSheet
$cells=$ws.Cells
$xl.Visible=$True

$cells.item(1,1)="{0} VMM Server Report" -f $server.Name
$cells.item(1,1).font.bold=$True
$cells.item(1,1).font.size=18

#define some variables to control navigation
$row=3
$col=1

#insert column headings
"Name", "Description", "OperatingSystem", "CPUCount","Memory (GB)", "Status", "Hostname" | foreach {
$cells.item($row,$col)=$_
$cells.item($row,$col).font.bold=$True
$col++
}

foreach ($vm in $vminfo) {
$row++
$col=1
$cells.item($row,$col)=$vm.Name
$col++
$cells.item($row,$col)=$vm.Description
$col++
$cells.item($row,$col)=$vm.OperatingSystem.Name
$col++
$cells.item($row,$col)=$vm.CPUCount
$col++
$cells.item($row,$col)=$vm.Memory/1024
$col++
$cells.item($row,$col)=$vm.Status
$col++
$cells.item($row,$col)=$vm.HostName
}
$objRange = $ws.UsedRange 
$objRange.EntireColumn.Autofit() 

$date = get-date -DisplayHint date 

$filepath="C:\Users\paulm\Documents\"

if ($filepath) {
$wb.SaveAs($filepath)

}

完成した保存スクリプトは次のようになります。

$date = Get-Date -Format yyyy-MM-dd 

$wb.SaveAs("C:\Users\paulm\Documents\$date")+ ".xls"
4

1 に答える 1

1

そのための関数はGet-Date. 引数なしで呼び出すと、現在の日付が返されます。このようなもの

(Get-Date -Format yyyy-MM-dd) + ".xls"

使用可能なファイル名を提供します。

于 2012-09-05T01:02:27.160 に答える