PowerShell script to fetch all the disposed assets based on portal specific
Steps to use the provided script:
- Ensure that the required modules are installed by executing the following commands:
$ErrorActionPreference = "Stop"
Install-Module -Name 'ImportExcel' -Force -AllowClobber - Import the necessary modules by executing the following command:
Import-Module -Name 'ImportExcel' - Set up the script variables according to your requirements:
- Update the $url variable with the appropriate API URL.
- Replace the $api_key variable with your actual API key.
- Adjust the $Portalid variable to match your desired portal ID.
- Modify the $headers variable if needed.
- Run the script by executing the remaining code. The script will fetch asset data using the specified parameters, loop through the results, and store the relevant information in arrays.
- The script will check if a file named "Disposed_Asset.xlsx" already exists. If it does, it will increment a counter and append the counter value to the filename until a unique filename is found.
- A DataTable is created, and the asset data is populated in the table.
- The script will output a message indicating the filename where the "Disposed_Asset" data was saved.
Powershell script :
This document provides step-by-step instructions for running the provided PowerShell script on both Windows and Linux systems.
Prerequisites:
- PowerShell: Make sure PowerShell is installed on your system. Check the PowerShell version by running the command powershell -version. It should be PowerShell version 5.0 or later.
Windows:
- Open PowerShell: Launch PowerShell by searching for "PowerShell" in the Start menu, or by pressing Win + X and selecting "Windows PowerShell" or "PowerShell" from the menu.
- Set Execution Policy: If your system's execution policy does not allow running scripts, you need to change the execution policy. Run the following command in PowerShell with administrative privileges:
Set-ExecutionPolicy RemoteSigned
- Install Required Module: The script requires the 'ImportExcel' module. Run the following command to install it:
Install-Module -Name 'ImportExcel' -Force -AllowClobber
- Run the Script: Copy the provided script and paste it into the PowerShell window, then press Enter. The script will start running and fetch asset data. It will save the data to an Excel file with a unique name like "Disposed_Asset.xlsx" or "Disposed_Asset_2.xlsx" if the file already exists # Paste the script here
- Verify Output: Once the script completes, it will display the path where the Excel file is saved, such as "Disposed_Asset data saved to C:\Path\to\Disposed_Asset.xlsx".
Linux:
- Open Terminal: Launch the terminal emulator on your Linux distribution. It can usually be found in the applications menu or by pressing Ctrl + Alt + T.
- Install PowerShell: If PowerShell is not installed on your Linux system, you need to install it. The installation steps may vary depending on your Linux distribution. Please refer to the official Microsoft documentation for instructions: Installing PowerShell on Linux
- Set Execution Policy: PowerShell on Linux requires explicitly setting the execution policy to run scripts. Run the following command in the terminal:
sudo pwsh -Command "Set-ExecutionPolicy RemoteSigned"
Enter your password when prompted.
- Install Required Module: The script requires the 'ImportExcel' module. Run the following command in the terminal:
sudo pwsh -Command "Install-Module -Name 'ImportExcel' -Force -AllowClobber"
Enter your password when prompted.
- Run the Script: Copy the provided script and paste it into the terminal, then press Enter. The script will start running and fetch asset data. It will save the data to an Excel file with a unique name like "Disposed_Asset.xlsx" or "Disposed_Asset_2.xlsx" if the file already exists.
# Paste the script here
- Verify Output: Once the script completes, it will display the path where the Excel file is saved, such as "Disposed_Asset data saved to /path/to/Disposed_Asset.xlsx".
MacOS:
- Open Terminal: Launch the Terminal application by going to "Applications" > "Utilities" > "Terminal" or by using Spotlight search (press Cmd + Space and type "Terminal").
- Check PowerShell Installation: Confirm if PowerShell is installed on your macOS system. In the Terminal, type pwsh and press Enter. If PowerShell is installed, the PowerShell prompt will appear. If it is not installed, you will need to install PowerShell for macOS. You can follow the official Microsoft documentation for instructions: Installing PowerShell on macOS
- Install Required Module: The script requires the 'ImportExcel' module. In the Terminal, type the following command and press Enter:
sudo pwsh -Command "Install-Module -Name 'ImportExcel' -Force -AllowClobber"
Enter your password when prompted.
- Run the Script: Copy the provided PowerShell script and paste it into the Terminal window, then press Enter. The script will start running and fetch asset data. It will save the data to an Excel file with a unique name like "Disposed_Asset.xlsx" or "Disposed_Asset_2.xlsx" if the file already exists.
# Paste the script here
- Verify Output: Once the script completes, it will display the path where the Excel file is saved, such as "Disposed_Asset data saved to /path/to/Disposed_Asset.xlsx".
New to ADSelfService Plus?