2017-05-30 20 views
2

私は2つのpowershellスクリプトを持っています。私はこれらのスクリプトを組み合わせる方法を探しています。しかし、スクリプトの1つがCIMメソッドを使用していて、もう1つがWMIメソッドを使用しているので、私はこれを行うことができません。Get-CimInstance関数とGet-WMIObject関数の出力を結合する方法

私が達成しようとしているのは、同じサーバーの最後の再起動時間と空き容量を提供することです(ユーザーはサーバー名を入力してください。Enterを押すと、最後の再起動時間と空き容量が表示されます)。

スクリプト1(CIM方法):

$Server = Read-Host -Prompt 'Input your server name' 
Get-CimInstance -ClassName win32_operatingsystem -ComputerName $Server | select csname, lastbootuptime 
Read-Host -Prompt "Press Enter to exit" 

スクリプト2(WMI法):

$Server = Read-Host -Prompt 'Input your server name' 
Get-WMIObject Win32_Logicaldisk -ComputerName $Server | Select PSComputername,DeviceID, @{Name="Total_Size_GB";Expression={$_.Size/1GB -as [int]}}, @{Name="Free_Space_GB";Expression={[math]::Round($_.Freespace/1GB,2)}} 
Read-Host -Prompt "Press Enter to exit" 

答えて

1

ストア変数にクエリの結果。次に、各機能から興味のある値を持つpsobjectを作成します。 + $ theThingIActuallyWant =新オブジェクトpsobject =プロパティ@ { + ~~~:You can find out more about New-Object psobject here.

$Server = Read-Host -Prompt 'Input your server name' 

$myCimResp = Get-CimInstance -ClassName win32_operatingsystem -ComputerName $Server | select csname, lastbootuptime 
$myWmiResp = Get-WMIObject Win32_Logicaldisk -ComputerName $Server | Select PSComputername,DeviceID, @{Name="Total_Size_GB";Expression={$_.Size/1GB -as [int]}}, @{Name="Free_Space_GB";Expression={[math]::Round($_.Freespace/1GB,2)}} 

$theThingIActuallyWant = New-Object psobject -Property @{ 
    LastRebootTime = $myCimResp.lastbootuptime 
    FreeSpace  = $myWmiResp.Free_Space_GB 
} 

# A couple of ways to print; delete 1, keep 1 
Write-Output $theThingIActuallyWant  # Print to screen with implicit rules 
$theThingIActuallyWant | Format-Table # Print to screen with explicit table formatting 


Read-Host -Prompt "Press Enter to exit" 
+0

@Clijstersのおかげで、私は何も悪いことをしていますが、私はこのエラーを取得していた場合gms0ulmanわからない@ – gms0ulman

+0

を編集しました~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidArgument:(:) [新オブジェクト]、ParameterBindingException + FullyQualifiedErrorId:PositionalParameterNotFound 、Microsoft.PowerShell.Commands.NewObjectCommand 新オブジェクト:位置パラメータは、引数「System.Collections.Hashtable」 –

+0

.......を受け入れることを見つけることができませんCで :\ Users \ユーザーXXXXXXX \ XXXXXX \ script.ps1:6文字:26 + $ theThingIActuallyWant =新オブジェクトpsobject =プロパティ@ { + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidArgument:(:) [新オブジェクト]、ParameterBindingException + FullyQualifiedErrorId:PositionalParameterNotFound、Microsoft.PowerShell.Commands.NewObjectCommand –

関連する問題