3

I have got 2 powershell scripts, which I have mentioned below. I am looking for a way to combine both of these scripts. However I am unable to do so as one of the script is using CIM method and the other one is using the WMI method.

What I am trying to accomplish, is to provide the last reboot time and free space available of the same server (User has to enter the server name and on pressing Enter it shows the last reboot time and free space available).

Script 1 (CIM Method) :

$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"

Script 2 (WMI Method) :

$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 Answer 1

1

Store the results of your queries in a variable. Then create a psobject with the values you are interested in from each function. 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"
Sign up to request clarification or add additional context in comments.

3 Comments

@gms0ulman Not sure if I am doing anything wrong, but I am getting this error : + $theThingIActuallyWant = New-Object psobject =Property @{ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand New-Object : A positional parameter cannot be found that accepts argument 'System.Collections.Hashtable'
....... At C:\Users\XXXXXXX\XXXXXX\script.ps1:6 char:26 + $theThingIActuallyWant = New-Object psobject =Property @{ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
@SharatPadmanabhan I think that's my bad; corrected typo from =Property to -Property and $myWmiResp.lastbootuptime to $myWmiResp.Free_Space_GB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.