I am learning about functions. For some reason the scripts work as individual pieces but when placed in a function and run nothing happens. Any help would be greatly appreciated. Here is what I have so far:
#Clears
the screen
cls
#Generates
a prompt that asks the user to input which computer to access
$computer= Read-Host "Enter the
computer name:"
#Create
a function that list the software installed on the computer specified by input
argument
function getSoftwareInfo ($softwareInfo)
{
#Gets
the Programs installed on the computer
$softwareInfo = gwmi Win32_Product -computername
$computer |
Sort-Object Vendor, Name | Format-Table Vendor, Name
}
#Create
a function that lists the free disk space and percentage of disk space used on
the computer
function diskCheck {
#Gets
the freespace of the C: Drive
$freeSpace = gwmi -class win32_logicaldisk
-computername $computer | where {$_.deviceid -eq"C:"} |
select-object {$_.freespace / 1GB}
#Gets
the total space of the C: Drive
$totalSpace = gwmi -class win32_logicaldisk
-computername $computer
| where {$_.deviceid -eq "C:"}
| select-object
{$_.size
/ 1GB}
#Gets
percentage of disk space used
$percentUsed = (($totalSpace
- $freeSpace)/$totalSpace)
* 100
}
#calls
the functions
getSoftwareInfo
diskCheck
#Create
readable texts for both functions
$installed = "The programs
that are installed in the " + $computer + " are as follows: " + $softwareInfo
$diskfree = "Free space
available on C: drive:" + $freeSpace +" GB"
$diskused = "Percent of
disk space used on C:" + $percentUsed +"%"
#Exports
softwareInfo to CSV file
Export-Csv -path C:\Scripts\Computer_Software.csv
-inputObject $installed
#Exports
diskCheck to CSV file
Export-Csv -path C:\Scripts\Computer_hdspace.csv
-inputObject $diskfree, $diskused
#Writes
output to the screen
Write-Host $installed
Write-Host $diskfree
Write-Host $diskused