Here’s a PowerShell script that performs a software inventory, exports the data to an Excel document, and then sends it to a remote share:

1. PowerShell Script Breakdown

Here’s the breakdown of each major function in order:

a. Setting Log File and Paths

At the start, the script sets paths for a log file, the exported Excel file, and the remote share where the Excel file will be sent.

$LogFilePath = "C:\Path\to\LogFile.log"
$ExportFilePath = "C:\Path\to\SoftwareInventory.xlsx"
$RemoteSharePath = "\Server\Share\SoftwareInventory.xlsx"

b. Writing Log Messages

The Write-Log function is used to write log messages to the log file. It accepts two parameters: a mandatory message and an optional log level (which defaults to “Info”).

function Write-Log { … }

3. Handling Errors

The Handle-Error function is used to handle errors. It logs the error message and then throws an exception.

function Handle-Error { … }

c. Installing NuGet

If the Install-NuGet function does not find the NuGet package manager or detects an older version, it installs NuGet. In case of a failed installation, the function logs an error message and throws an exception.

function Install-NuGet { … }

d. Installing ImportExcel Module

If the ImportExcel module isn’t present, the Install-ImportExcelModule function installs it. Upon a failed installation, this function logs an error message and throws an exception.

function Install-ImportExcelModule { … }

e. Main Script Execution

The main script logic begins by confirming the installation of NuGet and its version. If NuGet doesn’t exist or the version is older than required, the script installs NuGet using the Install-NuGet function.

Next, the script verifies if the ImportExcelModule is installed. If not, the Install-ImportExcelModule function steps in to install the module.

Subsequently, the script executes a software inventory using the Get-CimInstance cmdlet with the Win32_Product class.

After performing the software inventory, the script verifies the existence of the data. If the data exists, the script exports it to an Excel file using the Export-Excel cmdlet. The script saves the exported Excel file at the path that the $ExportFilePath variable specifies.

Once the script exports the software inventory to Excel, it verifies the existence of the Excel file. If the file exists, the script copies it to the remote share path. The $RemoteSharePath variable specifies this path, and the script uses the Copy-Item cmdlet for copying.

During the software inventory, exporting, or file copying operations, if any error occurs, the script writes suitable log messages and throws an error using the Handle-Error function.

It’s important to set the $LogFilePath, $ExportFilePath, and $RemoteSharePath variables to the desired locations for the log file, exported Excel file, and remote share path, respectively.

Ensure the installation of the ImportExcel module in PowerShell to utilize the Export-Excel cmdlet.

The script is customizable according to your requirements.

End Results

You should now have an exported .xlsx file with your inventory.

Software Inventory Export