This PowerShell script is designed for the State Board for Community and Technical Colleges (SBCTC) to update SharePoint list items based on inventory data from a CSV file. The script uses a configuration file to map CSV columns to SharePoint fields, handle date conversions, and update only the fields that have changed. Detailed logging is provided to track the script's execution and any errors encountered.
- Reads a CSV file containing inventory data.
- Maps CSV columns to SharePoint fields using a configuration file (config.json).
- Converts date strings to SharePoint date format.
- Logs script execution details and errors to a log file.
- Only updates SharePoint list items when changes are detected.
- PowerShell 7.2 or later
- PnP.PowerShell module
- Install PowerShell 7.2 or later:
- Follow the PowerShell Installation Guide.
- During installation, make sure to check the box that adds the PowerShell 7 right-click menu option.
- Install the PnP.PowerShell module:
Install-Module -Name PnP.PowerShell -Scope CurrentUser
- Download the script from GitHub:
- Navigate to the GitHub repository.
- Click on the Code button.
- Select Download ZIP.
- Unzip the downloaded file.
- Move
SBCTC.InventoryUpdater.ps1
to the desired location.
- Place the
SBCTC.InventoryUpdater.ps1
script in the desired directory. - Run the script. If the
config.json
file does not exist, it will be created automatically. - Modify the
config.json
file as needed after it is created. - Run the script again to update SharePoint list items based on the CSV file.
- Open PowerShell 7 and navigate to the script directory.
- Run the script:
./SBCTC.InventoryUpdater.ps1
- A file dialog will prompt you to select the CSV file containing the inventory data.
- After selecting the CSV file, you will be asked to authenticate to the SharePoint server. Use the email associated with your account on the SBCTC SharePoint server.
If you have the right-click menu option installed for PowerShell 7:
- Right-click the
SBCTC.InventoryUpdater.ps1
script. - Select Run with PowerShell 7.
- A file dialog will prompt you to select the CSV file containing the inventory data.
- After selecting the CSV file, you will be asked to authenticate to the SharePoint server. Use the email associated with your account on the SBCTC SharePoint server.
The config.json
file contains a FieldMappings
section that maps CSV columns to the corresponding SharePoint fields. The key is the SharePoint field name, and the value is the CSV column name.
Example FieldMappings
section:
{
"FieldMappings": {
"Title": "asset_id",
"AssetType": "asset_type",
"Manufacturer": "manufacturer",
"Model": "model",
"SerialNumber": "serial_number",
"Status": "status",
"DOCID_x0028_currentowner_x0029_": "doc_number",
"LastName_x0028_currentowner_x002": "last_name",
"FirstName_x0028_currentowner_x00": "first_name",
"DueDate": "transaction_timestamp",
"AgreementForm": "agreement_signed",
"OriginatingSite": "origin_site",
"CurrentSite": "current_site",
"PurchasePrice": "asset_cost",
"Color": "color",
"Housing": "housing_cell",
"Unit": "housing_unit",
"IncidentInvolvement": null,
"PurchaseDate": null,
"OrderNumber": null,
"Program": null,
"ConditionNotes": null,
"OMNI": null
}
}
To edit the field mappings:
- Open the
config.json
file in a text editor. - Locate the
FieldMappings
section. - Modify the values to match the column names in your CSV file. Ensure that the keys (SharePoint field names) remain unchanged.
- Save the changes to the
config.json
file.
Example:
If your CSV file has a column named device_type
that should map to the SharePoint field AssetType
, you would update the AssetType
mapping as follows:
"AssetType": "device_type"
The script logs its execution details to SBCTC.InventoryUpdater.log
in the same directory as the script. The log level can be adjusted by changing the logLevel
setting in the config.json
file.
- Set to
DEBUG
for detailed logging. - Set to
INFO
for general logging.
SBCTC Inventory Updater is licensed under the GNU General Public License v3.0.
Brandon Henness
Email: brandon.henness@doc1.wa.gov
v2024051500
- Ensure the CSV file is properly formatted.
- The script checks for required PowerShell version and PnP.PowerShell module.
- Handle errors gracefully and log them appropriately.
© 2024 Brandon Henness